<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-26 10:04:31
 -->
<template>
  <div class="daily-set-wrap ">
    <div class="daily-set-content border-box">
      <div class="table-condition flex flex-space-between m-b-20">
        <div class="table-condition-left">
          <el-input placeholder="请输入任务名称" maxlength="50" v-model="conditionObj.searchInput" class="w-264" style="width: 264px;" @keyup.native="value => toInput(value, conditionObj.searchInput)"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
        </div>
        <div class="table-condition-right">
          <!--<el-button type="danger" @click="multDel">批量删除</el-button>-->
          <el-button type="primary" @click="toNewSession">新建会话任务</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="任务标题" width="120" show-overflow-tooltip>
          <template slot-scope="scope"
            ><div style="width: 100%; overflow:hidden;text-overflow: ellipsis;">{{ scope.row.taskTitle }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="任务内容" show-overflow-tooltip>
          <template slot-scope="scope"
            ><div style="width: 100%; overflow:hidden;text-overflow: ellipsis;">{{ scope.row.taskContent }}</div></template
          >
        </el-table-column>
        <el-table-column prop="" label="创建时间" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToYmd }}</div>
            <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="完成期限" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="color-606266 line-18">{{ scope.row.lastTime | timeStampToYmd }}</div>
            <div class="color-606266 line-18">{{ scope.row.lastTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="创建人" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.createName }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--<el-popover placement="top" width="160" v-model="scope.row.visible">
              <p>是否下线该计划？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="hidePop(scope.row)">取消</el-button>
                <el-button type="primary" size="mini" @click="handleDelete(scope.row)">确定</el-button>
              </div>
              <el-button type="text" slot="reference">下线</el-button>
            </el-popover>-->
            <el-button type="text" @click="toDel(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="false" @hideDialog="hideDialog"> </multiple-del>
  </div>
</template>
<script>
import multipleDel from '@/components/app/cloudDaily/multiple-del.vue';
import { getRequest, postRequest } from '@/api/api';
import { _debounce } from '@/common/js/public';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      tableH: window.screen.availHeight - 464 - 126,
      activeTab: '1',
      activeBrand: this.brandId,
      actictiveGroup: this.activeGroupId, // 商户(品牌) groupId
      showDialog: false,
      activeId: '1',
      conditionObj: {
        searchInput: ''
      },
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      loading: false,
      selectRadio: 0, // 选择当页/选择全部
      tableData: [],
      multipleSelection: [],
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0
    };
  },
  computed: {},
  methods: {
    /**
     * 删除
     */
    toDel(index, row) {
      const that = this;
      that
        .$confirm('是否要删除选中的任务?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.delCardList(row.sessionTaskId);
        })
        .catch(() => {});
    },
    /**
     * 删除
     */
    delCardList(sessionTaskId) {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId,
        sessionTaskId: sessionTaskId
      };
      postRequest('/haoban-app-tel-task-three-web/delete-session-task', 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
          });
        });
    },
    /* 下线 */
    hidePop(row) {
      row.visible = false;
    },
    handleDelete(row) {
      const that = this;
      let para = {
        helpId: row.helpId
      };
      postRequest('/haoban-app-tel-task-three-web/del-help', para)
        .then(res => {
          if (res.data.errorCode === 1) {
            that.$message.success(res.data.message);
            that.currentPage = 1;
            that.getTableList();
          } else {
            that.$message.warning(res.data.message);
          }
        })
        .catch(err => {
          // console.log(err);
        });
    },
    /**
     * 选择门店分组
     */
    checkGroupIds: function(nodes) {
      const that = this;
      that.conditionObj.storeGroup = nodes;
      that.currentPage = 1;
      that.getTableList();
    },

    /** *
     * 显示选择本页 / 全部 *
     */
    handleCommand(command) {
      this.selectRadio = command;
    },

    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      let that = this;
      that.currentPage = 1;
      that.getTableList();
    }, 500),

    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /**
     * 删除选的部门
     */
    delDepart(index, item) {
      const that = this;
      item.splice(index, 1);
      that.currentPage = 1;
      that.getTableList();
    },

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

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

    postMultDel(obj) {
      const that = this;

      let para = {
        search: that.conditionObj.searchInput || '', // 搜索字段

        enterpriseId: that.activeBrand, // 品牌 id
        delTaskStatus: obj.delOptFlag, // 0:仅删除逾期任务，1:删除所有待完成任务
        reason: obj.reason, // 备注
        isAll: that.selectRadio // 0：当页，1：所有
        // taskType: 1 //0不良评价，1话务任务
      };
      postRequest('/haoban-app-tel-task-three-web/batch-del-store-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
          });
        });
    },

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

    /**
     * 分页---当前页变化
     *  @param {Number} val
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      // let fullPath = that.$route.fullPath;
      that.getTableList();
    },

    /**
     * 新建会话任务
     */
    toNewSession(index, row) {
      let that = this;
      that.$router.push(`newSession`);
    },

    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      that.loading = true;
      const para = {
        search: that.conditionObj.searchInput || '', // 搜索字段
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize, // 一页显示个数
        wxEnterpriseId: that.wxEnterpriseId
        // taskType: 1 //0不良评价，1话务任务
      };
      getRequest('/haoban-app-tel-task-three-web/session-task-list', para)
        .then(res => {
          let resData = res.data;
          that.loading = false;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.list || [];
            that.total = resData.result.pageInfo.total;
            return false;
          }
          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();
      }
    },
    activeGroupId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    }
  },
  mounted() {
    const that = this;
    document.documentElement.style.backgroundColor = '#f0f2f5';
    that.activeBrand = that.brandId;
    that.$emit('showTab', '3');
    if (!!this.brandId) {
      that.$nextTick(() => {
        that.getTableList();
      });
    }
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  components: {
    multipleDel
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.color-c0c4cc {
  display: inline-block;
  line-height: 34px;
  padding-left: 10px;
  color: #c0c4cc;
}
.condition-tips {
  display: inline-block;
  vertical-align: middle;
  line-height: 32px;
}
.table-condition {
  // min-width: 1200px;
}
.daily-set-wrap {
  height: 100%;
  .daily-set-content {
    padding: 20px;
    .table-condition-left {
      font-size: 0;
    }
  }
  .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>
