<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="searchDate"
        :picker-options="pickerOptions"
        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>
    </div>

    <el-table :data="dataLists" border stripe>
      <el-table-column type="index" label="编号" align="center" fixed width="80" />
      <el-table-column prop="Caller" label="主叫号码" align="center" min-width />
      <el-table-column prop="CusCode" label="坐席工号" align="center" min-width />
      <el-table-column prop="Callee" label="被叫号码" align="center" min-width />
      <el-table-column label="呼叫类型" align="center" min-width>
        <template slot-scope="scope">{{ scope.row.CallType | calltypeFilter }}</template>
      </el-table-column>
      <el-table-column label="呼叫状态" align="center" min-width>
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.CallState === 1 ? 'success' : 'danger'"
            size="mini"
            disable-transitions
          >{{ scope.row.CallState | statusFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理方式" align="center" min-width>
        <template slot-scope="scope">{{ scope.row.DealType | dealtypeFilter }}</template>
      </el-table-column>
      <el-table-column label="满意度" align="center" min-width>
        <template slot-scope="scope">{{ scope.row.MYD | evaluationFilter }}</template>
      </el-table-column>
      <el-table-column label="录音" align="center" min-width>
        <template slot-scope="scope">
          <svg-icon
            v-if="scope.row.FilePath"
            class="rec_file"
            icon-class="bofangluyin"
            @click.native="playSound(scope.row.FilePath, scope.row.agent_code)"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="呼叫开始时间" align="center" min-width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.BeginTime | timesFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通话开始时间" align="center" min-width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.TalkStartTime | timesFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通话结束时间" align="center" min-width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.TalkEndTime | timesFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="挂机时间" align="center" min-width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.EndTime | timesFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="TalkLongTime" label="通话时长" align="center" min-width />
      <el-table-column prop="RelayNumber" label="中继号码" align="center" min-width />
      <el-table-column label="外呼类型" align="center" min-width>
        <template slot-scope="scope">{{ scope.row.callopttype | callopttypeFilter }}</template>
      </el-table-column>
      <!-- <el-table-column label="操作" width="" align="center" fixed="right" class-name="oparate_btn">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click.stop="btn_ruturn(scope.row.id)">添加工单</el-button>
        </template>
      </el-table-column>-->
      <!-- <el-table-column 更多公告信息="操作" align="center" min-width="">
        <template>
          <div class="footer_menu">
            <router-link class="inlineBlock" to="order/orderCreateHY">
              <el-dropdown-item>
                <i class="el-icon-more"/>更多公告信息
              </el-dropdown-item>
            </router-link>
          </div>
        </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 store from '@/store'
import { mapGetters } from 'vuex'
import { getCallRecords } from '@/api/trafficData/trafficData'
import audioPlayer from '@/components/audioPlayer'
import { Send } from '@/utils/telWebsocket'
// import { getPhone } from '@/api/questionnaire/management'
import { pickerOptions } from '@/utils'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
export default {
  name: 'CallRecord',
  components: {
    Pagination
  },
  filters: {
    // 呼叫类型
    calltypeFilter (status) {
      const statusMap = {
        0: '呼入',
        1: '呼出'
      }
      return statusMap[status]
    },
    // 呼叫状态
    statusFilter (status) {
      const statusMap = {
        0: '未接通',
        1: '已接通'
      }
      return statusMap[status]
    },
    // 外呼类型
    callopttypeFilter (status) {
      const statusMap = {
        0: '拨号外呼',
        1: '回访外呼'
      }
      return statusMap[status]
    },
    // 处理方式
    dealtypeFilter (status) {
      const statusMap = {
        0: 'IVR处理',
        1: '骚扰电话',
        2: '自助服务',
        3: '转值班电话',
        4: '留言',
        5: '呼损',
        6: '人工处理'
      }
      return statusMap[status]
    },
    // 满意度
    evaluationFilter (status) {
      const statusMap = {
        0: '-',
        1: '非常满意',
        2: '基本满意',
        3: '不满意'
      }
      return statusMap[status]
    },
    // 时间格式化
    timesFilter (tm) {
      if (tm === '1970-01-01 08:00:00') {
        return '-'
      } else {
        return tm
      }
    }
  },
  data () {
    return {
      loading: false,
      keyword: '',
      searchDate: '',
      pickerOptions,
      recordpath: '', // 录音的路径
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [] // 列表数据
    }
  },
  computed: {
    ...mapGetters([
      'usercode', // 工号
      'extension', // 分机号
      'telIsLogin', // 是否签入
      'telSeatState', // 坐席状态
      'telLineState', // 线路状态
      'telIsVisCallout' // 外呼面板是否显示
    ]),
    // 是否可以点击外呼
    isAllowCallout: function () {
      // `this` 指向 vm 实例
      return this.telIsLogin && this.telSeatState === 2 && this.telLineState === 1
    }
    //
  },
  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, // 每页几条信息
          callstate: 0,
          keyword: this.keyword, //	否	string	模糊查询（呼叫号码）
          stime: this.searchDate && this.searchDate[0],
          etime: this.searchDate && this.searchDate[1]
        }
        getCallRecords(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.recordpath = response.data.recordpath
            this.pageParams.total = response.data.Totals
            this.dataLists = response.data.Rows
          }
        })
        resolve()
      })
    },
    btn_search () {
      this.getList()
    },
    // btn_ruturn() {
    //   alert('1')
    // },
    outboundClick (quesPhone) {
      this.$confirm('您确定要对此客户发起呼叫吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.scoketDatas = {
            'Type': 'MakeCall',
            'AgentID': this.usercode,
            'AgentExten': this.extension,
            'DestinationNumber': quesPhone //
          }
          store.dispatch('ChangeCallNum', quesPhone)
          Send(this.scoketDatas)
          this.getList()
        })
        .catch(() => {
          this.$message.info('已取消呼叫')
        })
    },
    // 播放录音
    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>
.footer_menu {
  .el-dropdown-menu__item {
    padding: 20px;
    line-height: 20px;
  }
}
.footer_menu {
  background-color: #f3f7f9;
  border-top: 1px solid #e4eaec;
}
.phoneNum {
  color: #409eff;
  cursor: pointer;
}
</style>
