<template>
  <div class="app-container">
    <div class="filter-container">
      <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>
    </div>

    <el-table v-loading="loading" :data="dataLists" border stripe>
      <el-table-column type="index" label="编号" align="center" fixed width="80"/>
      <el-table-column label="主叫坐席" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.caller_agent_id = scope.row.caller_agent_id === 0 ? '-' : scope.row.caller_agent_id }}
        </template>
      </el-table-column>
      <el-table-column prop="caller" label="呼叫号码" align="center" min-width=""/>
      <el-table-column label="被叫坐席" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.callee_agent_id = scope.row.callee_agent_id === 0 ? '-' : scope.row.callee_agent_id }}
        </template>
      </el-table-column>
      <el-table-column prop="callee" label="被呼叫号码" align="center" min-width=""/>
      <el-table-column label="振铃时间" align="center" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.time_alerting | timestampToTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="振铃时长" align="center" min-width="">
        <template slot-scope="scope">
          <span>{{ scope.row.period_alerting }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应答时间" align="center" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.time_answer | timestampToTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通话时长" align="center" min-width="">
        <template slot-scope="scope">
          <span>{{ scope.row.period_talking }}</span>
        </template>
      </el-table-column>
      <el-table-column label="录音" align="center" min-width="">
        <template slot-scope="scope">
          <svg-icon v-if="scope.row.record_path.length" class="rec_file" icon-class="bofangluyin" @click.native="playSound(scope.row.record_path, scope.row.caller_agent_id)"/>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="呼入方式" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.call_type | callTypeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" min-width="">
        <template slot-scope="scope">
          <span>{{ scope.row.peer_line_type | peerLineTypeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否应答" align="center" min-width="">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.is_answer === 1 ? 'success' : 'danger'"
            size="mini"
            disable-transitions>
            {{ scope.row.is_answer | statusFilter }}
          </el-tag>
        </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 { getExtCallinList } from '@/api/trafficData/trafficData'
import { timestampToTime } from '@/utils'
import audioPlayer from '@/components/audioPlayer'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
export default {
  name: 'ExtensionCallin',
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: '否',
        1: '是'
      }
      return statusMap[status]
    },
    callTypeFilter(status) {
      const statusMap = {
        0: '直通',
        1: 'IVR呼入',
        2: '转移',
        3: '三方'
      }
      return statusMap[status]
    },
    peerLineTypeFilter(type) {
      const typeMap = {
        0: '未知设备',
        1: '内线分机',
        2: '中继外线',
        7: 'VOIP资源'
      }
      return typeMap[type]
    }
  },
  data() {
    return {
      loading: false,
      keyword: '',
      recordpath: '', // 录音的路径
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0, // 总共多少数据
        prevText: '上一页',
        nextText: '下一页'
      },
      dataLists: [] // 列表数据
    }
  },
  created() {
    this.getList()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          keyword: this.keyword //	否	string	模糊查询（呼叫号码）
        }
        getExtCallinList(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.recordpath = response.data.recordpath
            this.pageParams.total = response.data.total
            this.dataLists = response.data.rows
          }
        })
        resolve()
      })
    },
    btn_search() {
      this.getList()
    },
    // 播放录音
    playSound(rec_file, artist) {
      this.$layer.iframe({
        content: {
          content: audioPlayer, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'recFiles': this.recordpath + rec_file, 'artists': artist.toString() }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['600px', '230px'],
        shadeClose: true,
        title: '录音详情'
      })
    }
  }
}
</script>

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

</style>
