<template>
  <app-container>
    <all-show :data="showData" @refresh="refreshData"/>
    <el-row>
      <el-col :span="6">
        <ivr-area :seat-form="currentUser"/>
      </el-col>
      <el-col :span="12">
        <el-row>
          <div v-show="extensionList.length>0" class="realtime-seats">
            <el-col v-for="exten in extensionList" :key="exten.exten" :span="6">
              <seat :data="exten" :current="exten.exten==currentUser.exten" @detail="fetchUserDetail"/>
            </el-col>
          </div>
          <div v-show="extensionList.length==0" class="realtime-seats">
            <el-col v-for="i in 8" :key="i" :span="6">
              <seat :i="'600'+i"/>
            </el-col>
          </div>
        </el-row>
      </el-col>
      <el-col :span="6">
        <realtime-check :seat-form="currentUser"/>
      </el-col>
    </el-row>

  </app-container>
</template>

<script>
import AppContainer from '../../../components/layout/AppContainer'
import Seat from './components/seat'
import AllShow from './components/allShow'
import RealtimeCheck from './components/realtimeCheck'
import IvrArea from './components/ivrArea'
import { mapState } from 'vuex'
import { getUserSimpleList } from '@/api/system/user'
import { getSeatsState, getCurrentCalls } from '@/api/cti'

export default {
  name: 'SeatMonitor',
  components: { IvrArea, RealtimeCheck, AllShow, Seat, AppContainer },
  data() {
    return {
      showData: {
        all: 8,
        signin: 0,
        signout: 8,
        showbusy: 0,
        showidle: 0,
        busy: 0,
        wait: 0
      }, // 总览数据
      userList: [], // 从后台获取的用户列表
      currentUser: {
        agentUserName: '--',
        agentName: '--',
        phone: '--',
        state: '--',
        exten: '--',
        callerNumber: '--',
        calleeNumber: '--',
        callid: ''
      },
      callList: [] // 正在通话列表
    }
  },
  computed: {
    ...mapState({
      exten: state => state.user.exten, // 用户绑定的分机号
      wsStatus: state => state.ivr.wsStatus,
      signStatus: state => state.ivr.signStatus, // 签入签出状态
      callStatus: state => state.ivr.callStatus,
      isInCall: state => state.ivr.isInCall,
      isInComming: state => state.ivr.isInComming,
      extensionList: state => state.ivr.extensionList,
      reg: state => state.ivr.reg,
      webSocket: state => state.ivr.websocket,
      websocketObj: state => state.ivr.websocketObj
    })
  },
  watch: {
    extensionList(val) {
      this.handleExtensionListChange(val)
    },
    signStatus(val) {
      if (val) {
        this.getExtentionList()
      }
    }
  },
  created() {
    this.fetchUserList()
    this.refreshData()
  },
  methods: {
    refreshData() {
      this.getExtentionList()
      this.fetchCurrentCalls()
    },
    // 当extensionList变化之后的处理
    handleExtensionListChange(val) {
      const data = {
        all: val.length,
        signin: 0,
        signout: 0,
        showbusy: 0,
        showidle: 0,
        busy: 0,
        wait: 0
      }
      for (const item of val) {
        if (item.loginId) { // 有loginId表示已签入，否则表示未签入
          data.signin += 1
          if (item.state === '空闲') { // 话机为空闲状态,等待数+1，否则忙碌数+1
            data.wait += 1
          } else {
            data.busy += 1
          }
          if (item.busy) { // 如果busy不为空,示忙数+1
            data.showbusy += 1
          } else {
            data.showidle += 1
          }
        }
      }
      data.signout = data.all - data.signin
      this.showData = data
    },
    // 获取已登录坐席列表
    getExtentionList() {
      if (this.wsStatus) {
        const params = {
          type: 'all'
        }
        this.$store.dispatch('getExtensionList', params)
      }
      // getSeatsState().then(response => {
      //   console.log(response)
      // })
    },
    // 获取用户列表
    fetchUserList() {
      const params = {
        roleTips: 'receiver,monitor,administrator'
      }
      getUserSimpleList(params).then(response => {
        if (response.code === 200) {
          this.userList = response.data
        }
      })
    },
    // 根据坐席号获取当前用户详情
    fetchUserDetail(loginId, name, exten, state) {
      if (loginId) {
        for (const user of this.userList) {
          if (user.id === loginId) {
            this.currentUser.agentUserName = user.name
            this.currentUser.agentName = name
            this.currentUser.phone = user.phone
            this.currentUser.exten = exten
            this.currentUser.state = state
            if (state.includes('通话')) {
              const res = this.fetchCurrentCalls(loginId)
              if (res) {
                this.currentUser.callerNumber = res.callerNumber
                this.currentUser.calleeNumber = res.calleeNumber
                this.currentUser.callid = res.callid
              }
            }
          }
        }
      } else {
        this.currentUser = {
          agentUserName: '--',
          agentName: '--',
          phone: '--',
          state: '--',
          exten: '--',
          callerNumber: '--',
          calleeNumber: '--',
          callid: ''
        }
      }
    },
    // 获取当前正在通话列表
    fetchCurrentCalls(loginId) {
      getCurrentCalls().then(response => {
        console.log(response)
        response = {
          root: [{ 'Call_pbxName': 'spzc_pbx', 'Call_city': '', 'Call_isp': '中国联通', 'Call_number': '18513995803', 'Call_province': '北京', 'Call_callStatus': '接通', 'Call_id': 'b7c32570-8acd-11ea-96b3-89e56eb1026b', 'Call_staySeconds': '8', 'Call_talkSeconds': '4', 'Call_ivrName': '座席组--客服', 'Call_agentName': '888', 'Call_otherLegNumber': '6001', 'Call_agentStaffid': '1', 'Call_sp_entityName': 'Call' }]
        }
        this.callList = response.root
        for (const call of this.callList) {
          if (call.Call_agentStaffid === loginId) {
            return {
              callerNumber: call.Call_number,
              calleeNumber: call.Call_otherLegNumber,
              callid: call.Call_id
            }
          }
        }
        return null
      })
    }
  }
}
</script>

<style scoped>
.realtime-seats{
  padding:30px 20px;
}
</style>
