<template>
    <div class="focus-reply">
      <div class="clear-both top">
        <span class="title">自动回复</span>
        <el-button type="success" plain style="float: right" @click="add" v-if="!$store.state.readonly">添加回复</el-button>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        max-height="800">
        <el-table-column
          fixed
          label="类型"
          width="150">
          <template slot-scope="scope">
            {{scope.row.type | typeFilter}}
          </template>
        </el-table-column>
        <el-table-column
          label="内容">
          <template slot-scope="scope">
            <div v-if="scope.row.type === 'text'">
              {{scope.row.content}}
            </div>
            <a v-else-if="scope.row.type === 'image'"  :href="scope.row.content" target="_blank">
              <img class="shotcut" :src="scope.row.content">
            </a>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          v-if="!$store.state.readonly"
          width="200">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editRow(scope.$index, list)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, list)"
              type="text"
              size="small">
              移除
            </el-button>
            <el-button
              v-if="scope.$index!==0"
              @click.native.prevent="moveRow(scope.$index-1,scope.$index)"
              type="text"
              size="small">
              上移
            </el-button>
            <el-button
              v-if="scope.$index!==list.length-1"
              @click.native.prevent="moveRow(scope.$index,scope.$index + 1)"
              type="text"
              size="small">
              下移
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <dialog-com v-if="dialogObj.show" :dialogObj="dialogObj"  @changeShow="changeShow" @reflash="getList"/>
    </div>
</template>

<script>
    import dialogCom from './focusReplyDialog'
    import {updateConfigApi,saveConfigApi,getMediaListApi,getConfigListApi} from "../../service/api";
    import page from '../framework/page'
    export default {
      name: "focusReply",
      data(){
        return{
          loading:false,
          total:0,
          nowPage:1,
          id: null,
          list: [],
          form:{
            key:'focus_reply',
            value:'focus_reply',
            desc:''
          },
          content: '',
          imageContent: '',
          type: 'text',
          mediaList: [],
          mediaListMock:  [
          {
            "media_id": "hQb3Pbdb4E5Ivxi2sagL5sTdtW5W9pKJNA6Z8nBo6Ao",
            "name": "xx.jpg",
            "update_time": 1535351431,
            "url": "http://mmbiz.qpic.cn/mmbiz_jpg/qNgYSw5sicibUGtiaRYRY9QEZUoqgGdvkTE5Zvg58tUciaAXFslmwuiadgU6turtsF7mXFeicKa9RQTTG1gKSLqPiabpA/0?wx_fmt=jpeg"
          },
          {
            "media_id": "hQb3Pbdb4E5Ivxi2sagL5p2poL7GllXYm4SETNmf210",
            "name": "z.jpg",
            "update_time": 1535351316,
            "url": "http://mmbiz.qpic.cn/mmbiz_jpg/qNgYSw5sicibUGtiaRYRY9QEZUoqgGdvkTE5mTOB0jnmfdH30s54N5FIr2Tsbd9QcBFDiapicYWJ6sCZRMGTIlj179g/0?wx_fmt=jpeg"
          }
          ],
          showMedia: false,
          rules:{
            value:[
              { required: true, message: '请输入规则名称', trigger: 'change' }
            ],
            desc:[
              { required: true, message: '请输入回复内容', trigger: 'change' }
            ],
            qr:[
              { required: true, message: '请输入二维码', trigger: 'change' }
            ]
          },
          dialogObj:{
            show:false,
            id:'',
            index: -1,
            list: []
          }
        }
      },
      components:{
        page,
        dialogCom
      },
      mounted(){
        this.getList()
      },
      filters: {
        typeFilter(value){
          if(!value) return '';
          if(value === 'text') {
              return '文字'
          } else if(value === 'image') {
            return '图片'
          } else {
              return val
          }
        },
        contentFilter(row){
          if (row.type === 'text') {
              return row.content
          } else if (row.type === 'image'){
            let content = `<img src='${row.content}'>`
            return content;
          }
        }
      },
      methods:{
        save(){
          let json = this.form
          let _desc = [];
          if (this.type === 'text') {
            if (!this.content) {
              this.$message({
                showClose: true,
                message: '请输入文本内容'
              });
              return;
            }
            _desc[0] = {
              type : this.type,
              content: this.content
            }
          } else if (this.type === 'image') {
            if (!this.imageContent) {
              this.$message({
                showClose: true,
                message: '请选择图片'
              });
              return
            }
            _desc[0] = this.imageContent
          }
          json.desc = JSON.stringify(_desc)
          if (this.id) {
            updateConfigApi(this.id,json).then(res=>{
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
            })
          } else {
            saveConfigApi(json).then(res=>{
              this.$message({
                type: 'success',
                message: '保存成功!'
              });
            })
          }
        },
        getMediaList(type){
          let json = {
            type: type,
            page: this.nowPage
          };
          this.loading = true;
          getMediaListApi(json).then(res=>{
            this.loading = false;
            this.type = type;
            this.total = res.total_count;
            if (res.item.length === 0 ) {
              this.$message({
                showClose: true,
                message: '暂无数据'
              });
              this.showMedia = false;
            } else {
              this.showMedia = true;
              this.mediaList = res.item;
            }
          }).catch(() => {
            this.loading = false;
            this.showMedia = false;
          })
        },
        onChooseMedia(val){
          this.addContent(this.type, val.url, val.media_id);
          this.showMedia = false;
        },
        addContent(type, content, mediaId) {
          let obj = {};
          if (content) {
            obj = {
              type: type,
              content: content
            };
          }
          if (mediaId) {
            obj.media_id = mediaId;
          }
          this.imageContent = obj
        },
        getList(){
          getConfigListApi({key: 'focus_reply'}).then(res => {
            if (res.list.length > 0) {
              this.id = res.list[0].id;
              console.log('res.list[0].desc', res.list[0].desc)
              let _desc = JSON.parse(res.list[0].desc);
              this.list = _desc || [];
//              this.type = _desc[0].type;
//              if (this.type === 'text') {
//                  this.content = _desc[0].content
//              } else if (this.type === 'image') {
//                  this.imageContent = {
//                    type: 'image',
//                    content: _desc[0].content,
//                    media_id: _desc[0].media_id
//                  }
//              }
            }
          })
        },
        add(){
          this.dialogObj = {
            show: true,
            id: this.id ? this.id : null,
            index: -1,
            list: this.list
          }
        },
        editRow(index, row){
          this.dialogObj = {
              show: true,
              id: this.id,
              index: index,
              list: this.list
          }
        },
        deleteRow(index , list){
          let desc = list;
          desc.splice(index,1);
          let json = {
              key: 'focus_reply',
              value: 'focus_reply'
          }
          json.desc = JSON.stringify(desc)
          this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            updateConfigApi(this.id,json).then(res=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getList();
            })
          }).catch(()=>{
            this.$message({
              type: 'success',
              message: '已取消删除!'
            });
          })
        },
        moveRow(first,second){
          let _first = this.list[first];
          let _second = this.list[second];
          this.list[first] = _second;
          this.list[second] = _first;
          let desc = this.list;
          let json = {
            key: 'focus_reply',
            value: 'focus_reply'
          }
          json.desc = JSON.stringify(desc)
          updateConfigApi(this.id,json).then(res=>{
            this.$message({
              type: 'success',
              message: '保存成功!'
            });
            this.getList();
          })
        },
        changeShow(){
          this.dialogObj.show = false;
        }
      }
    }
</script>

<style scoped lang="less">
  .focus-reply {
    padding: 30px;
  }
  ul, ol {
    padding-left: 0;
    list-style-type: none;
  }
  .title {
    color: #353535;
    font-size: 20px;
    font-weight: 400;
    line-height: 1;
  }
  .top {
    margin-bottom: 30px;
  }
  .content {
    padding-top: 40px;
    margin-top: 24px;
    border-top: 1px solid #E4E8EB;
  }
  .inner {
    border: 1px solid #E4E8EB;
    background-color: #FFFFFF;
  }
  .inner-bottom {
    border-top: 1px solid #E4E8EB;
  }
  .img-action {
    padding: 17px 20px;
    min-height: 215px;
  }
  .img-create-access {
    position: relative;
    border: 2px dotted #E4E8EB;
    width: 48.6%;
    height: 211px;
    text-align: center;
  }
  .img-create-access:first-child {
    float: left;
  }
  .img-create-access__link {
    display: inline-block;
    vertical-align: middle;
    color: #9A9A9A;
    margin: 0 10px;
    margin-top: 68px;
    text-decoration: none;
  }
 .img-create-access__link:before {
    content: " ";
    display: block;
    width: 36px;
    height: 36px;
    margin: 0 auto 5px;
    background: transparent url(https://res.wx.qq.com/mpres/en_US/htmledition/pages/modules/msg_sender/images/icon36_add_gray.png) no-repeat 0 0;
  }
  .weui-desktop-msg-sender__tabs {
    line-height: 38px;
    background-color: #FFFFFF;
  }
  .sender__tab_selected {
    color: #44B549;
  }
  .weui-desktop-msg-sender__tab:hover, .weui-desktop-msg-sender__tab_selected {
    color: #44B549;
  }
  .weui-desktop-msg-sender__tab {
    display: inline-block;
    padding: 0 20px;
    cursor: pointer;
  }
  .weui-desktop-msg-sender__tab:hover.weui-desktop-msg-sender__tab_text:before, .weui-desktop-msg-sender__tab_selected.weui-desktop-msg-sender__tab_text:before {
    background-image: url(https://res.wx.qq.com/mpres/en_US/htmledition/pages/modules/msg_sender/svg/default/sender_text_current.svg);
  }
  .weui-desktop-msg-sender__tab_img:before {
    background: transparent url(https://res.wx.qq.com/mpres/en_US/htmledition/pages/modules/msg_sender/svg/default/sender_img.svg) no-repeat 0 0;
  }
  .weui-desktop-msg-sender__tab:hover.weui-desktop-msg-sender__tab_img:before, .weui-desktop-msg-sender__tab_selected.weui-desktop-msg-sender__tab_img:before {
    background-image: url(https://res.wx.qq.com/mpres/en_US/htmledition/pages/modules/msg_sender/svg/default/sender_img_current.svg);
  }
  .weui-desktop-msg-sender__tab:before {
    content: " ";
    display: inline-block;
    width: 22px;
    height: 20px;
    vertical-align: middle;
    margin: -0.2em 5px 0 0;
  }
  .tool_bar {
    padding-top: 0;
    padding-bottom: 50px;
    margin-top: 40px;
    margin-left: 20px;
  }
  .tool_bar > .weui-desktop-btn {
    margin-right: 1em;
  }
  .weui-desktop-btn_primary {
    background-color: #1AAD19;
    border-color: #1AAD19;
    color: #FFFFFF;
  }
  .weui-desktop-btn {
    display: inline-block;
    padding: 0 22px;
    min-width: 54px;
    line-height: 2.42857143;
    vertical-align: middle;
    text-align: center;
    text-decoration: none;
    border-radius: 3px;
    font-size: 14px;
    cursor: pointer;
    border-width: 1px;
    border-style: solid;
    box-sizing: content-box;
  }
  .weui-desktop-popover__wrp {
    display: inline;
    position: relative;
    font-size: 14px;
  }
  .weui-desktop-btn_default {
    background-color: #FBFBFB;
    border-color: #E4E8EB;
    color: #353535;
  }
  .inner-emotion_editor {
    margin: 2%;
  }
  .shotcut {
    width: 50px;
  }
  .clear-both{
    &:after{
       content: '';
       display: block;
       clear: both;
     }
  }
</style>
