<!--
 * @Description: 待质检列表
 * @Author: 王晓颖
 * @Date: 2020-04-27 14:27:37
 -->
<template>
  <app-container>
    <el-row type="flex">
      <el-col :span="5">
        <user-list @change="changeSeat"/>
      </el-col>
      <el-col :span="19">
        <search-area :need-clear="true" :need-search-more="false" :size="size" type="default" search-more-type="default" @search="search" @clear="clearInput">
          <!--一般查询条件-->
          <search-item>
            <el-input v-model.trim="listQuery.callerNumber" :size="size" placeholder="主叫号码" clearable/>
          </search-item>
          <search-item/>
          <search-item>
            <el-select v-model="listQuery.callWay" :size="size" placeholder="呼叫方向" clearable>
              <el-option v-for="item in callWayList" :label="item.name" :value="item.value"/>
            </el-select>
          </search-item>
          <search-item>
            <el-select v-model="listQuery.hangupCause" :size="size" placeholder="挂机方向" clearable>
              <el-option v-for="item in hangupCauseList" :label="item.name" :value="item.name"/>
            </el-select>
          </search-item>
          <search-item>
            <el-date-picker
              v-model="timeRange"
              :size="size"
              type="datetimerange"
              range-separator="至"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="通话开始时间"
              end-placeholder="通话结束时间"/>
          </search-item>
        </search-area>
        <normal-table :data="list" :head="tableOption.head" :size="size" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage">
          <template slot="columns">
            <el-table-column label="通话时长（分:秒）" align="center">
              <template slot-scope="scope">
                {{ scope.row.bridgeStamp | timeFilter }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="80">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="goCheck(scope.row)">质检</el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </el-col>
    </el-row>
    <quality-check ref="qualitycheck"/>
  </app-container>
</template>

<script>
import NormalTable from '@/components/NomalTable'
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import CreateCase from '@/views/caseManage/createCase'
import UserList from './components/userList'
import { getWaitCheckSoundList } from '@/api/qualityCheck'
import { getCallWay } from '@/api/allDict'

import QualityCheck from './qualityCheck'

export default {
  name: 'QualitySound',
  components: { QualityCheck, UserList, AppContainer, SearchArea, SearchItem, CreateCase, NormalTable },
  filters: {
    timeFilter(val) {
      const m = Math.floor(val / 60)
      const s = val - m * 60
      const str = ('0' + m).slice(-2) + ':' + ('0' + s).slice(-2)
      return str
    }
  },
  data() {
    return {
      listQuery: {
        callerNumber: '', // 主叫号码
        agentUser: '', // 受理人
        startTime: '', // 来电开始时间
        endTime: '', // 来电结束时间
        minTime: '', // 通话最短时间
        maxTime: '', // 通话最长时间
        callWay: '', // 呼叫方向
        hangupCause: '', // 挂机方向
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '主叫号码',
          value: 'callerNumber',
          align: 'center'
        },
        {
          text: '被叫号码',
          value: 'calleeNumber',
          align: 'center'
        },
        {
          text: '呼叫方向',
          value: 'callWayName',
          align: 'center'
        },
        {
          text: '挂机方向',
          value: 'hangupCause',
          align: 'center'
        },
        {
          text: '通话开始时间',
          value: 'dialStartStamp',
          align: 'center'
        },
        {
          text: '通话结束时间',
          value: 'dialEndStamp',
          align: 'center'
        },
        {
          text: '坐席工号',
          value: 'agentName',
          align: 'center'
        },
        {
          text: '坐席姓名',
          value: 'agentUserName',
          align: 'center'
        }
      ], // 显示列
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      tableOption: {
        head: {
          show: false, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      showAddCase: false,
      sound: {
        url: '',
        controlList: 'noDownload noSpeed onlyOnePlaying'
      },
      size: 'small',
      soundShow: false, // 显示音频弹窗
      userList: [],
      callWayList: [], // 呼叫方向列表
      hangupCauseList: [{ name: '主叫挂机', value: '1' }, { name: '被叫挂机', value: '2' }, { name: '系统挂机', value: '3' }] // 挂机方向列表
    }
  },
  created() {
    this.fetchCallWay()
    this.search()
  },
  activated() {
    this.search()
  },
  methods: {
    search() {
      this.fetchData(false)
    },
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      this.listLoading = true
      if (this.timeRange && this.timeRange.length === 2) {
        this.listQuery.startTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      getWaitCheckSoundList(this.listQuery).then(response => {
        if (response.code === 200) {
          this.listLoading = false
          this.list = response.data.rows
          this.total = response.data.total
        }
      })
    },
    fetchCallWay() {
      getCallWay().then(response => {
        if (response.code === 200) {
          this.callWayList = response.data
        }
      })
    },
    // 打开质检页面
    goCheck(row) {
      this.$refs.qualitycheck.initDialog('first', true, row)
    },
    // 创建工单
    createCase(row) {
      this.showAddCase = true
      const data = {
        callid: row.callid,
        number: row.callerNumber,
        dialStartStamp: row.dialStartStamp
      }
      console.log(data)
      const that = this
      setTimeout(function() {
        that.$refs['addcase'].initData(data)
      }, 500)
    },
    // 关闭新建案件弹窗
    closeCreateDialog() {
      this.showAddCase = false // 关闭弹窗
      this.search()
    },
    // 更改坐席
    changeSeat(val) {
      this.listQuery.agentUser = val.id
      this.fetchData()
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 清除输入
    clearInput() {
      this.listQuery = {
        callerNumber: '', // 主叫号码
        agentUser: '', // 受理人
        startTime: '', // 来电开始时间
        endTime: '', // 来电结束时间
        minTime: '', // 通话最短时间
        maxTime: '', // 通话最长时间
        callWay: '', // 呼叫方向
        hangupCause: '', // 挂机方向
        offset: 1,
        limit: 20,
        sort: 'dialStartStamp',
        order: 'desc'
      }
      this.timeRange = []
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
