<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="searchDate"
        class="filter-item"
        type="daterange"
        format="yyyy年MM月dd日"
        value-format="yyyy-MM-dd"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <el-input v-model="keyword" placeholder="请输入坐席组" class="filter-item"/>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button v-permission="'HY_add'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add">添加</el-button>
      <el-button v-permission="'HY_deletes'" type="primary" class="filter-item" icon="el-icon-delete" @click="btn_deletes">批量删除</el-button>
    </div>
    <el-table v-loading="loading" :data="dataLists" border stripe @selection-change="changeSelects">
      <el-table-column type="selection" width="40" />
      <el-table-column type="index" label="编号" align="center" fixed width="80"/>
      <el-table-column prop="groupname" label="坐席组" align="center" min-width=""/>
      <el-table-column prop="ivrname" label="ivr名称" align="center" min-width=""/>
      <el-table-column label="ivr类型" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.ivrtype | ivrTypeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" min-width="">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isenable | IsenableTypeFilter">{{ scope.row.isenable | IsenableFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createbyuser" label="创建人" align="center" min-width=""/>
      <el-table-column prop="createtime" label="创建时间" align="center" min-width=""/>
      <el-table-column label="操作" width="320" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'HY_edit'" size="mini" plain type="primary" @click="btn_edit(scope.row.id)">编辑</el-button>
          <el-button v-permission="'HY_details'" size="mini" plain type="primary" @click="btn_details(scope.row.id)">详情</el-button>
          <el-button v-permission="'HY_on'" v-show="scope.row.isenable===0" size="mini" plain type="primary" @click="btn_On(scope.row.id)">启用</el-button>
          <el-button v-permission="'HY_off'" v-show="scope.row.isenable===1" size="mini" plain type="primary" @click="btn_Off(scope.row.id)">禁用</el-button>
          <el-button v-permission="'HY_delete'" size="mini" plain type="danger" @click="btn_delete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>
<script>
import {
  getIVRConfigureList,
  deleteIVRConfigure,
  ivrOn,
  ivrOff
} from '@/api/telCall/ivrConfigureManage'
import addIvrConfigure from './addIvrConfigure'
import editIvrConfigure from './editIvrConfigure'
import seeIvrConfigure from './seeIvrConfigure'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
export default {
  name: 'IVRConfigure',
  components: {
    Pagination
  },
  filters: {
    IsenableFilter(status) {
      const IsenableMap = {
        0: '不启用',
        1: '启用'
      }
      return IsenableMap[status]
    },
    ivrTypeFilter(status) {
      const ivrtype = {
        1: '呼入',
        2: '满意度',
        3: '自动外呼'
      }
      return ivrtype[status]
    },
    IsenableTypeFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      loading: false,
      keyword: '',
      searchDate: '',
      dataLists: [],
      isenableOptions: ['启用', '禁用'],
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      multipleSelection: []// 表格选中id
    }
  },
  created() {
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  mounted: function() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          groupcode: this.keyword.replace(/\s+/g, ""),
          stime: this.searchDate && this.searchDate[0],
          etime: this.searchDate && this.searchDate[1]
        }
        getIVRConfigureList(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.data.total
            this.dataLists = response.data.rows
          }
        })
        resolve()
      })
    },
    // 搜索
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    },
    // 添加
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addIvrConfigure, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': ''
          } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['65%', '90%'],
        title: '添加IVR配置'
      })
    },
    // 表格复选框状态改变
    changeSelects(val) {
      this.multipleSelection = val
    },
    // 批量删除
    btn_deletes() {
      const ids = []
      for (let i = 0, len = this.multipleSelection.length; i < len; i++) {
        ids.push(this.multipleSelection[i].id)
      }
      if (ids.length <= 0) {
        this.$message.warning('没有可删除的选项')
        return
      }
      this.btn_delete(ids)
    },
    // 编辑
    btn_edit(editId) {
      this.$layer.iframe({
        content: {
          content: editIvrConfigure, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': editId
          } // props
        },
        area: ['65%', '90%'],
        title: '编辑IVR配置'
      })
    },
    // 删除
    btn_delete(ids) {
      this.$confirm('您确定要将此条信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteIVRConfigure(ids).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 详情
    btn_details(editId) {
      this.$layer.iframe({
        content: {
          content: seeIvrConfigure, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': editId
          } // props
        },
        area: ['65%', '90%'],
        title: '查看IVR配置'
      })
    },
    // 启用
    btn_On(ids) {
      this.$confirm('您确定要进行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const datas = {
          id: ids,
          isenable: 1
        }
        ivrOn(datas).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('操作成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 禁用
    btn_Off(ids) {
      this.$confirm('您确定要进行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const datas = {
          id: ids,
          isenable: 0
        }
        ivrOff(datas).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('操作成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    }

  }
}
</script>
<style lang="scss" scoped>

</style>
