<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="key" placeholder="请输入关键词" class="filter-item"/>
      <el-select v-model="state" placeholder="请选择启动状态">
        <el-option label="请选择启动状态" value=""/>
        <el-option label="未启动" value="0"/>
        <el-option label="生效中" value="1"/>
        <el-option label="已失效" value="2"/>
      </el-select>
      <el-date-picker
        v-model="searchDate"
        type="datetimerange"
        range-separator="至"
        value-format="yyyy-MM-dd hh:mm:ss"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <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>
    </div>

    <el-table v-loading="loading" :data="dataLists" border stripe>
      <el-table-column type="index" label="编号" align="center" fixed width="80"/>
      <el-table-column prop="title" label="欢迎词标题" align="center" min-width=""/>
      <el-table-column :show-overflow-tooltip="true" prop="content" label="欢迎词内容" align="center" width="200"/>
      <el-table-column label="欢迎词类型" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.type | ivrTypeFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="groupname" label="坐席组" align="center" min-width=""/>
      <el-table-column label="语音文件" align="center" min-width="">
        <template slot-scope="scope">
          <svg-icon v-if="scope.row.wavfile.length>0" class="rec_file" icon-class="bofangluyin" @click.native="playSound(scope.row.wavfile[0].fileurl,scope.row.createby)"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" min-width="">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isstate | stateTypeFilter">{{ scope.row.isstate | ivrStateFilter }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="createby" label="添加工号" align="center" min-width=""/>
      <el-table-column prop="startdate" label="生效时间" align="center" min-width="150"/>
      <el-table-column prop="enddate" label="失效时间" align="center" min-width="150"/>
      <el-table-column prop="createtime" label="添加时间" align="center" min-width="150"/>
      <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注" align="center" width="200"/>
      <el-table-column label="操作" width="252" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'HY_edit'" v-if="scope.row.isstate !==2" size="mini" plain type="primary" @click="btn_edit(scope.row.id)">编辑</el-button>
          <el-button v-permission="'HY_delete'" size="mini" plain type="danger" @click="btn_delete(scope.row.id)">删除</el-button>
          <el-button v-permission="'HY_details'" size="mini" plain type="primary" @click="btn_details(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="getLists" />
  </div>
</template>
<script>
import { getList, delIvrGreeting } from '@/api/telCall/IVRgreeting'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
import audioPlayer from '@/components/audioPlayer'
import addOrEditIVRgreeting from './addOrEditIVRgreeting'
import seeIvrGreeting from './seeIVRgreeting'
export default {
  components: {
    Pagination,
    audioPlayer
  },
  filters: {
    ivrTypeFilter(status) {
      const calltype = {
        1: '文本',
        2: '语音文件'
      }
      return calltype[status]
    },
    ivrStateFilter(status) {
      // state状态：0未启动，1生效中，2已失效
      const callState = {
        0: '未启动',
        1: '生效中',
        2: '已失效'
      }
      return callState[status]
    },
    stateTypeFilter(status) {
      const statusMap = {
        0: 'primary',
        1: 'success',
        2: 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      state: '',
      key: '',
      searchDate: '',
      loading: false,
      dataLists: [], // 表格数据
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      }
    }
  },
  mounted: function() {
    this.getLists()
  },
  methods: {
    getLists() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          state: this.state, // 状态
          key: this.key, // 关键字
          stime: this.searchDate && this.searchDate[0], // 开始时间
          etime: this.searchDate && this.searchDate[1] // 结束时间
        }
        getList(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.getLists()
    },
    // 播放录音
    playSound(urls, agent_code) {
      this.$layer.iframe({
        content: {
          content: audioPlayer, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { recFiles: urls, artists: agent_code } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['600px', '230px'],
        shadeClose: true,
        title: '录音详情'
      })
    },
    // 添加
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEditIVRgreeting, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': ''
          } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['45%', '600px'],
        title: '添加IVR欢迎词'
      })
    },
    // 编辑
    btn_edit(editId) {
      this.$layer.iframe({
        content: {
          content: addOrEditIVRgreeting, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': editId
          } // props
        },
        area: ['40%', '550px'],
        title: '编辑IVR欢迎词'
      })
    },
    // 删除
    btn_delete(ids) {
      this.$confirm('您确定要将此条信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delIvrGreeting(ids).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getLists()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 详情
    btn_details(editId) {
      this.$layer.iframe({
        content: {
          content: seeIvrGreeting, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': editId
          } // props
        },
        area: ['65%', '660px'],
        title: '查看IVR欢迎词'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

