<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :md="14">
        <el-alert :closable="false" title="外呼任务" type="success"/>
        <div class="filter-container">
          <el-input v-model="searchData.keyword" placeholder="请输入关键字" class="filter-item" />
          <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search_task">搜索</el-button>
          <el-button v-permission="'HY_add_task'" type="primary" class="filter-item" @click="btn_add_task">添加</el-button>
        </div>
        <el-table v-loading="loading" :data="taskDataLists" border stripe highlight-current-row @row-click="taskRowClick" >
          <el-table-column type="index" label="编号" align="center" fixed width="80" />
          <el-table-column prop="F_TaskUser" label="任务人" align="center" min-width />
          <el-table-column prop="F_State" label="任务状态" align="center">
            <template slot-scope="scope">
              {{ scope.row.F_State | judgmentState }}
            </template>
          </el-table-column>
          <el-table-column prop="F_Name" label="任务名称" align="center" />
          <el-table-column prop="F_StartTime" label="任务开始时间" align="center" />
          <el-table-column prop="F_EndTime" label="任务结束时间" align="center" />
          <el-table-column prop="F_Type" label="外呼类型" align="center">
            <template slot-scope="scope">
              {{ scope.row.F_Type | judgmentType }}
            </template>
          </el-table-column>
          <el-table-column prop="F_Content" label="外呼内容" align="center" />
          <el-table-column prop="F_Remarks" label="任务说明" align="center" />
          <el-table-column label="操作" width="240" align="center" class-name="oparate_btn" fixed="right">
            <template slot-scope="scope">
              <el-button v-permission="'HY_edit_task'" type="text" @click="btn_edit_task(scope.row.F_ID)">编辑</el-button>
              <el-button v-permission="'HY_delete_task'" type="text" @click="btn_delete_task(scope.row.F_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="pageParamsL.total > 0"
          :total="pageParamsL.total"
          :pageindex.sync="pageParamsL.pageindex"
          :pagesize.sync="pageParamsL.pagesize"
          class="pagination"
          @pagination="getListTask"
        />
      </el-col>
      <el-col :md="10">
        <el-alert :closable="false" title="外呼号码" type="success"/>
        <div class="filter-container">
          <el-button type="primary" class="filter-item" @click="btn_import_outbound_number"><svg-icon icon-class="daoru" />导入</el-button>
        </div>
        <el-table v-loading="loading" :data="outboundNumberDataLists" border stripe >
          <el-table-column type="index" label="编号" align="center" fixed width="80" />
          <el-table-column prop="F_Tel" label="电话号码" align="center" />
          <el-table-column prop="F_ISOutbound" label="是否已外呼" align="center">
            <template slot-scope="scope">
              {{ scope.row.F_ISOutbound | judgmentISOutbound }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240" align="center" class-name="oparate_btn" fixed="right">
            <template slot-scope="scope">
              <el-button v-permission="'HY_edit_outbound_number'" type="text" @click="btn_edit_outbound_number(scope.row.F_ID)">编辑</el-button>
              <el-button v-permission="'HY_delete_outbound_number'" type="text" @click="btn_delete_outbound_number(scope.row.F_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="pageParamsR.total > 0"
          :total="pageParamsR.total"
          :pageindex.sync="pageParamsR.pageindex"
          :pagesize.sync="pageParamsR.pagesize"
          class="pagination"
          @pagination="getListOutboundNumber"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getTaskLists, deleteTaskLists, getOutboundNumberLists, deleteOutboundNumberLists } from "@/api/outbound/automaticOutbound";
import { pickerOptions, formatterContent } from "@/utils";
import { mapGetters } from 'vuex';
import addOrEdit from './components/addOrEdit';
import importFile from './components/importFile'
import editOutboundNumber from './components/editOutboundNumber';
import Pagination from "@/components/Pagination"; // 对el-pagination 二次封装

export default {
  name: "MyTask",
  components: {
    Pagination
  },
  filters: {
    judgmentState (status) {
      const statusMap = {
        '0': '未启动',
        '1': '启动',
        '2': '暂停',
        '3': '终止',
      }
      return statusMap[status]
    },
    judgmentISOutbound (status) {
      const statusMap = {
        '0': '未外呼',
        '1': '已外呼',
      }
      return statusMap[status]
    },
    judgmentType (status) {
      const statusMap = {
        '1': '语音外呼',
        '2': '坐席外呼',
      }
      return statusMap[status]
    },
  },
  data () {
    return {
      loading: false,
      searchData: {
        keyword: '',
      },
      pickerOptions, // 日期数据
      pageParamsL: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      pageParamsR: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      taskDataLists: [], // 外呼任务列表数据
      outboundNumberDataLists: [], //外呼号码列表数据
      taskRowClickId: 0, //选中外呼人id
    };
  },
  created () {
    this.getListTask()
    document.onkeyup = e => {
      if (e.keyCode === 13) {
        this.getListTask();
      }
    };
  },
  computed: {
    ...mapGetters([
      'usercode', // 工号
      'extension', // 分机号
      'sidebar',
      'avatar',
      'telCallType', // 通话类型
      'telIsVisCallout' // 外呼面板是否显示
    ])
  },
  methods: {
    getListTask () {
      this.loading = true;
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParamsL.pageindex, // int 第几页
          pagesize: this.pageParamsL.pagesize, // int 每页几条信息
          keyword: this.searchData.keyword.replace(/\s+/g, ""), //关键字
        };
        getTaskLists(params).then(response => {
          this.loading = false;
          if (response.state.toLowerCase() === "success") {
            this.pageParamsL.total = response.rows.Totals;
            this.taskDataLists = response.rows.Rows;
          }
        });
        resolve();
      });
    },
    btn_search_task () {
      this.pageParamsL.pageindex = 1;
      this.getListTask();
    },
    btn_add_task () {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: '' } // props
        },
        area: ["80%", "90%"],
        title: "添加任务"
      });
    },
    btn_edit_task (id) {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: id } // props
        },
        area: ["80%", "90%"],
        title: "编辑任务"
      });
    },
    btn_delete_task(id) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          id: id,
        }
        deleteTaskLists(data).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getListTask()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    getListOutboundNumber (taskId) {
      this.loading = true;
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParamsR.pageindex, // int 第几页
          pagesize: this.pageParamsR.pagesize, // int 每页几条信息
          keyword: this.searchData.keyword.replace(/\s+/g, ""), //关键字
          pid: taskId, //任务id
        };
        getOutboundNumberLists(params).then(response => {
          this.loading = false;
          if (response.state.toLowerCase() === "success") {
            this.pageParamsR.total = response.rows.Totals;
            this.outboundNumberDataLists = response.rows.Rows;
          }
        });
        resolve();
      });
    },
    btn_delete_outbound_number(id) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          ids: id,
        }
        deleteOutboundNumberLists(data).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getListOutboundNumber(this.taskRowClickId)
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    btn_edit_outbound_number (id) {
      this.$layer.iframe({
        content: {
          content: editOutboundNumber, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: id, taskId: this.taskRowClickId } // props
        },
        area: ["80%", "90%"],
        title: "编辑外呼号码"
      });
    },
    btn_import_outbound_number() {
      this.$layer.iframe({
        content: {
          content: importFile, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: this.taskRowClickId, } // props
        },
        area: ['40%', '380px'],
        title: "导入外呼号码"
      });
    },
    taskRowClick(row, column, event) {
      this.taskRowClickId  = row.F_ID
      this.getListOutboundNumber (row.F_ID)
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el {
  cursor: pointer;
}
.filter-container {
  padding-top: 15px;
}
</style>
