<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-01 13:58:41
 -->
<template>
  <div class="my-customer-wrap common-set-wrap">
    <div class="daily-set-content boder-box">
      <div class="table-condition flex flex-space-between m-b-23">
        <div class="table-condition-left">
          <el-select v-model="conditionObj.completed" placeholder="请选择" class="w-135" @change="changeSelect"> <el-option v-for="item in completedOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select><el-select v-model="conditionObj.overdue" placeholder="请选择" class="w-135 m-l-10" @change="changeSelect"> <el-option v-for="item in overOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select
          ><el-select v-if="!$route.query.ecmId" v-model="conditionObj.taskType" placeholder="请选择" class="w-105 m-l-10" @change="changeSelect">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </div>
        <div class="table-condition-right">
          <el-button type="danger" @click="multDel">批量删除</el-button>
        </div>
      </div>
      <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="35"> </el-table-column>
        <el-table-column width="25">
          <template slot="header" slot-scope="scope">
            <el-dropdown style="line-height: 10px; padding: 0; margin-left: -15px; transform: translateY(4px); -webkit-transform: translateY(4px);" @command="handleCommand" placement="bottom-start">
              <span class="el-dropdown-link"><i class="iconfont icongengduo"></i> </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="0"><span :style="{ color: selectRadio == 0 ? '#2f54eb' : '#606266' }">选择当页</span></el-dropdown-item>
                <el-dropdown-item command="1"><span :style="{ color: selectRadio == 1 ? '#2f54eb' : '#606266' }">选择全部</span></el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
          <template slot-scope="scope"> </template>
        </el-table-column>
        <el-table-column label="指派对象" show-overflow-tooltip width="170">
          <template slot-scope="scope">
            <div class="flex flex-pack-center flex-start">
              <!-- <div :class="['image-wrap flex flex-align-center flex-pack-center', !!scope.row.headImgUrl ? 'bg-eceaeb' : 'bg-82c5ff']">
                <img v-if="!!scope.row.headImgUrl" :src="scope.row.headImgUrl" alt="" />
                <i v-else class="iconfont icon-yewuduanmorentouxian"></i>
              </div> -->
              <el-popover placement="top-start" width="300" trigger="hover" @show="showSingleInfo(scope.row.clerkId)">
                <div class="apply-info-detail">
                  <div class="flex">
                    <div class="apply-info-img flex-align-center flex-pack-center bg-82C5FF ">
                      <i v-if="!userData.headImg" class="iconfont icon-yewuduanmorentouxian"></i>
                      <img v-else :src="userData.headImg" alt="img" />
                    </div>
                    <div class="flex flex-column apply-info-right flex-space-between">
                      <div class="apply-info-name">
                        {{ userData.staffName || '--' }} ( {{ userData.position || '--' }})
                        <!-- <i :class="[userData.sex == 2 ? 'icon-xingbienv color-FF585C' : 'icon-xingbienan color-508CEE', 'iconfont']"></i> -->
                      </div>
                      <div class="apply-info-phone">
                        <span class="w-80">手机号：</span><span class="w-130">{{ userData.nationCode == '86' ? userData.phoneNumber : '+' + userData.nationCode + '-' + userData.phoneNumber }}</span>
                      </div>
                      <div class="apply-info-store">
                        <span class="w-80">所属部门：</span><span class="w-130">{{ userData.departmentName || '--' }}</span>
                      </div>
                    </div>
                  </div>
                </div>
                <div slot="reference">
                  <div class="flex flex-align-center flex-pack-center bg-82C5FF table-head-pic cursor-pointer">
                    <i v-if="!scope.row.headImgUrl" class="iconfont icon-yewuduanmorentouxian"></i>
                    <img v-else :src="scope.row.headImgUrl" alt="img" />
                  </div>
                </div>
              </el-popover>
              <div class="clerk-info flex flex-column flex-space-between m-l-16" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                <p class="font-14 color-303133" style="line-height: 18px;overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ scope.row.clerkName }}</p>
                <p class="font-14 color-909399" style="line-height: 18px;width: 100%; overflow:hidden;text-overflow: ellipsis;">{{ scope.row.clerkPhone }}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="任务类型">
          <template slot-scope="scope">
            <div>{{ scope.row.taskType == '0' ? '不良评价' : scope.row.taskType == '5' ? '会话任务' : '话务任务' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="107">
          <template slot-scope="scope">
            <div class="line-18">{{ scope.row.createTime | formatTimeYMD }}</div>
            <div class="line-18">{{ scope.row.createTime | formatTimeHMS }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="完成期限" show-overflow-tooltip width="107">
          <template slot-scope="scope">
            <div class="line-18">{{ scope.row.lastTime | formatTimeYMD }}</div>
            <div class="line-18">{{ scope.row.lastTime | formatTimeHMS }}</div>
          </template>
        </el-table-column>
        <el-table-column label="逾期情况">
          <template slot-scope="scope">{{ scope.row.isOverTime }}</template>
        </el-table-column>
        <el-table-column label="完成情况">
          <template slot="header" slot-scope="scope">
            <el-tooltip class="item" effect="light" placement="right">
              <div slot="content">
                完成情况包含3种<br />
                1.完成拨打任务；<br />
                2.完成任务（无需拨打）；<br />
                3.完成任务（放弃拨打）；<br />
                4.未完成任务
              </div>
              <span style="cursor: pointer;padding-bottom: 2px; border-bottom: 1px dashed #2F54EB;">完成情况</span>
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            {{ scope.row.isCompleted }}
          </template>
        </el-table-column>
        <el-table-column label="完成时间">
          <template slot-scope="scope">
            <div class="line-18">{{ scope.row.finishTime | formatTimeYMD }}</div>
            <div class="line-18">{{ scope.row.finishTime | formatTimeHMS }}</div>
          </template>
        </el-table-column>
        <el-table-column label="提交人">
          <template slot-scope="scope">
            <div>{{ scope.row.operationStaffName || '--' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.reason" placement="top-start" title="" width="200" trigger="hover" :content="scope.row.reason">
              <i slot="reference" class="el-icon-chat-dot-square cursor-pointer color-909399" style="font-size: 20px;"></i>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <el-popover popper-class="notice-img-popover" placement="right" width="400" trigger="hover" v-for="(item, index) in scope.row.taskUrls" :key="index">
              <div class="img-preview flex">
                <img :src="item" alt="" style="max-width: 400px;" />
              </div>
              <img slot="reference" :src="item" alt="" class="task-imgs" style="" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="110">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            <el-button type="text" @click="getPreviewData(scope.$index, scope.row)">预览</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
        <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
      </div>
    </div>
    <multiple-del :showDialog="showDialog" :detailFlag="true" @hideDialog="hideDialog"> </multiple-del>
    <qrcode-dialog ref="qrcodePreview" v-model="dialogVisible"></qrcode-dialog>
    <detail-preview ref="detailPreview" :previewData="previewData" v-model="previewVisible" @closePreview="closePreview"></detail-preview>
  </div>
</template>
<script>
import multipleDel from '@/components/app/cloudDaily/multiple-del.vue';
import qrcodeDialog from '@/components/app/qrcode-dialog.vue';
import detailPreview from '@/components/app/detail-preview.vue';
import { getRequest, postRequest } from '@/api/api';
import errMsg from '@/common/js/error';
import showMsg from '@/common/js/showmsg';
export default {
  name: 'reviewed',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  filters: {
    formatTimeYMD(data) {
      return data && data != '- -' ? data.split(' ')[0] : '--';
    },
    formatTimeHMS(data) {
      return data && data != '- -' ? data.split(' ')[1] : '--';
    }
  },
  data() {
    return {
      tableH: window.screen.availHeight - 464 - 126,
      activeId: '2', // 详情头部选中 id
      activeBrand: this.brandId, // 品牌 id

      selectRadio: 0, // 0：选择当页； 1：选择全部
      showDialog: false, // 删除弹窗参数
      // 条件
      conditionObj: {
        completed: '',
        overdue: '',
        taskType: ''
      },
      completedOptions: [
        {
          label: '所有完成情况',
          value: ''
        },
        {
          label: '已完成(已拨通)',
          value: '1'
        },
        {
          label: '已完成(放弃)',
          value: '2'
        },
        /* {
            label: '已完成(特殊完成)',
            value: '3'
          }, */
        {
          label: '未完成',
          value: '0'
        }
      ],
      overOptions: [
        {
          label: '所有逾期情况',
          value: ''
        },
        {
          label: '已逾期',
          value: '1'
        },
        {
          label: '未逾期',
          value: '0'
        }
      ],
      typeOptions: [
        {
          label: '所有类型',
          value: ''
        },
        {
          label: 'ECM话务任务',
          value: '1'
        },
        {
          label: '不良评价',
          value: '0'
        },
        {
          label: '会话任务',
          value: '5'
        }
      ],
      loading: false,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false, // 二维码
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      previewData: {},
      previewVisible: false,
      // user
      userData: {
        staffName: '',
        phoneNumber: '',
        nationCode: '',
        postion: '',
        departmentName: '',
        headImg: ''
      }
    };
  },
  computed: {},
  methods: {
    /**
     * 获取头像处显示信息
     */
    /* eslint-disable */

    showSingleInfo(memberId) {
      const that = this;
      const para = {
        clerkId: memberId
      };
      postRequest('/haoban-manage3-web/get-clerk-info', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.userData = resData.result;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    closePreview() {
      const that = this;
      that.previewData = {};
    },
    /**
     * 获取预览数据
     */
    getPreviewData(index, item) {
      const that = this;
      let para = {
        taskId: item.taskId
      };
      that.previewVisible = true;
      postRequest('/haoban-app-tel-task-three-web/task/app-task-detail', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.previewData = resData.result;
            that.previewVisible = true;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 显示选择本页/全部
     */
    handleCommand(command) {
      this.selectRadio = command;
    },

    /**
     * 选择条件
     */
    changeSelect() {
      let that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /**
     * 批量删除
     */
    multDel() {
      let that = this;
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择删除项'
        });
        return false;
      }
      that.showDialog = true;
    },

    /**
     * 批量删除---弹窗关闭
     */
    hideDialog(obj) {
      let that = this;
      that.showDialog = false;
      if (!Object.keys(obj).length) {
        return false;
      }
      that.postMultDel(obj);
    },

    postMultDel(obj) {
      let that = this;
      let para = {
        // taskType: 1, // 0 不良评价，1 话务任务
        storeId: that.conditionObj.storeId,
        taskIds: that.multipleSelection.map(ele => ele.taskId) || '', // 门店分组,数组
        isOverTime: that.conditionObj.overdue || '', // 逾期
        isCompleted: that.conditionObj.completed || '', // 完成
        reason: obj.reason, // 删除备注
        // chooseFlag: obj.chooseFlag || '', // 0仅删除逾期任务，1删除所有待完成任务
        enterpriseId: that.activeBrand, // 品牌 id
        isAll: that.selectRadio // 0：当页，1：所有
      };
      postRequest('/haoban-app-tel-task-three-web/task/batch-del-clerk-task-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },

    /**
     * 删除
     */
    handleDel(index, row) {
      const that = this;
      that
        .$confirm(`确认要删除吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDel(index, row);
        })
        .catch(() => {});
    },

    postDel(index, row) {
      const that = this;
      let para = {
        taskId: row.taskId,
        enterpriseId: that.activeBrand // 品牌 id
      };
      getRequest('/haoban-app-tel-task-three-web/task/del-one', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 预览
     */
    handlePreview(index, row) {
      const that = this;
      that.postPreview(index, row);
    },

    /**
     *  预览---api
     */
    postPreview(index, row) {
      const that = this;
      // 生成二维码需要的 url
      let webUrl = `${row.taskId}`;
      that.$refs.qrcodePreview.qrcode(webUrl, 'task');
      that.dialogVisible = true;
    },

    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      that.loading = true;
      let para = {
        ecmId: that.$route.query.ecmId || '',
        taskType: that.conditionObj.taskType, // 0-不良评价回访 1-ecm话务任务
        storeId: that.conditionObj.storeId || '', // 门店
        isOverTime: that.conditionObj.overdue || '', // 逾期
        isCompleted: that.conditionObj.completed || '',
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize, // 一页显示个数
        enterpriseId: that.activeBrand
        // taskType: 1
      };
      getRequest('/haoban-app-tel-task-three-web/task/clerk-task-list', para)
        .then(res => {
          let resData = res.data;
          that.loading = false;
          if (resData.errorCode == 1) {
            if (resData.result.list) {
              resData.result.list.forEach(ele => {
                if (!ele.taskUrl || ele.taskUrl.length) {
                  ele.taskUrl = [];
                }
              });
            }
            that.tableData = resData.result.list || [];
            that.total = resData.result.pageInfo.total;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.loading = false;
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeBrand = newData;
        that.getTableList();
      }
    }
  },
  mounted() {
    let that = this;
    that.$emit('showTab', '11');
    document.documentElement.style.backgroundColor = '#f0f2f5';
    that.activeBrand = that.brandId;
    that.conditionObj.storeId = that.$route.query.storeId;
    if (!!that.brandId) {
      that.getTableList();
    }
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  components: {
    multipleDel,
    qrcodeDialog,
    detailPreview
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.w-135 {
  width: 135px;
}
.task-imgs {
  display: inline-block;
  vertical-align: middle;
  width: 30px;
  height: 30px;
  margin-right: 10px;
  border-radius: 4px;
}
.daily-set-content {
  padding: 16px;
  .image-wrap {
    width: 40px;
    height: 40px;
    min-width: 40px;
    max-width: 40px;
    max-height: 40px;
    border-radius: 4px;
    &.bg-82c5ff {
      background: #82c5ff;
    }
    &.bg-eceaeb {
      background: #eceaeb;
    }
    img {
      max-width: 40px;
      max-height: 40px;
      border-radius: 4px;
    }
    i {
      font-size: 20px;
      color: #e5f3ff;
    }
  }
}
.depart-item-content {
  width: 213px;
  height: 32px;
  overflow: hidden;
  white-space: nowrap;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  cursor: pointer;
}
.item-cell-select {
  /deep/ .el-select__tags {
    white-space: nowrap;
    overflow: hidden;
  }
}
.depart-cell {
  position: relative;
  margin: 0 24px 24px 0;
  .el-icon-circle-close {
    position: absolute;
    top: -10px;
    right: -10px;
    font-size: 16px;
    color: #808995;
    cursor: pointer;
  }
}
</style>
