<!--补录事件，待创建事件列表-->
<template>
  <app-container>
    <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.sex" :size="size" placeholder="受理人" clearable>
          <el-option v-for="item in userList" :label="item.name" :value="item.id"/>
        </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" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="openSound(scope.row)">录音</el-button>
            <el-button type="text" size="small" @click="createCase(scope.row)">受理</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <!--新建事件页面-->
    <el-dialog
      :visible.sync="showAddCase"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="新建事件"
      width="1200px"
      custom-class="addcase-dialog"
      top="5vh"
      append-to-body>
      <create-case ref="addcase" @cancel="closeCreateDialog"/>
    </el-dialog>
    <!--播放音频弹窗-->
    <el-dialog
      :visible.sync="soundShow"
      title="录音播放"
      width="600px"
      append-to-body
      custom-class="sound-dialog"
      @close="closeSound">
      <audio-player ref="adplayer" :the-url="sound.url" :the-control-ist="sound.controlList"/>
    </el-dialog>
  </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 AudioPlayer from '../../components/AudioPlayer/AudioPlayer'
// import { realFormatSecond } from '@/utils/stringutils'
import { getUserSimpleList } from '@/api/system/user'
import { getSoundList } from '@/api/sound'
export default {
  name: 'WaitForCreate',
  components: { AudioPlayer, SearchItem, SearchArea, AppContainer, NormalTable, CreateCase },
  data() {
    return {
      listQuery: {
        callerNumber: '', // 主叫号码
        agentUser: '', // 受理人
        status: '', // 受理状态
        startTime: '', // 创建开始时间
        endTime: '', // 创建结束时间
        offset: 1,
        limit: 20,
        sort: 'dialStartStamp',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '录音编号',
          value: 'callid',
          align: 'center'
        },
        {
          text: '主叫号码',
          value: 'callerNumber',
          align: 'center'
        },
        {
          text: '受理人',
          value: 'agentUserName',
          align: 'center'
        },
        {
          text: '归属地',
          value: 'isp',
          align: 'center',
          width: 100
        },
        {
          text: '来电时间',
          value: 'dialStartStamp',
          align: 'center',
          width: 160
        },
        // {
        //   text: '通话时间',
        //   value: 'bridgeStamp',
        //   align: 'center'
        // },
        {
          text: '状态',
          value: 'statusName',
          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: []
    }
  },
  created() {
    this.fetchUserList()
    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
      // getSoundList(this.listQuery).then(response => {
      //   if (response.code === 200) {
      //     this.listLoading = false
      //     this.list = response.data.rows
      //     this.total = response.data.total
      //   }
      // })
      const that = this
      setTimeout(function() {
        that.list = [
          { callid: '122fse223f2122', callerNumber: '15652360420', agentUserName: '受理员', isp: '北京联通', dialStartStamp: '2020-04-15 09:00:20', bridgeStamp: 61, statusName: '未接听' },
          { callid: '122fse223f2123', callerNumber: '18600202639', agentUserName: '受理员', isp: '北京联通', dialStartStamp: '2020-04-04 09:02:01', bridgeStamp: 61, statusName: '未接听' }
        ]
        that.total = 2
        that.listLoading = false
      }, 2000)
    },
    // 播放录音
    openSound(row) {
      this.soundShow = true
      this.sound.url = 'https://wdd.js.org/element-audio/static/falling-star.mp3'
      // this.sound.url = row.recordName
    },
    // 关闭播放弹窗
    closeSound() {
      this.$refs.adplayer.pausePlay()
    },
    // 创建工单
    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()
    },
    fetchUserList() {
      const params = {
        roleTips: 'receiver,monitor'
      }
      getUserSimpleList(params).then(response => {
        if (response.code === 200) {
          this.userList = response.data
        }
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.$message.success('clearInput')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .sound-dialog{
    .el-dialog__body{
      padding:0px 20px 20px;
    }
  }
</style>
