<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="房间号" prop="id">
        <el-input
            v-model="queryParams.id"
            placeholder="请输入房间号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="'拨打方'+systemName+'号'" prop="fromUsercode">
        <el-input
            v-model="queryParams.usercode"
            :placeholder="'请输入拨打方'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="'接收方'+systemName+'号'" prop="toUsercode">
        <el-input
            v-model="queryParams.mobile"
            :placeholder="'请输入接收方'+systemName+'号'"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable size="small">
          <el-option
              v-for="dict in userCallStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="链路号" prop="traceId">
        <el-input
            v-model="queryParams.traceId"
            placeholder="请输入链路号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userCallList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间号" align="center" prop="id" />
      <el-table-column label="拨打方" align="center">
        <el-table-column :label="systemName+'号'" align="center" prop="fromUsercode" />
        <el-table-column label="手机号" align="center" prop="fromMobile"  width="120" />
      </el-table-column>
      <el-table-column label="接收方" align="center">
        <el-table-column :label="systemName+'号'" align="center" prop="fromUsercode" />
        <el-table-column label="头像" align="center" prop="fromAvatar">
          <template v-slot="scope">
            <image-avatar :src="scope.row.fromAvatar"/>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="120">
        <template v-slot="scope">
          <cai-dict-tag :options="userCallStatusList" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime" width="160"/>
      <el-table-column label="结束时间" align="center" prop="endTime" width="160" />
      <el-table-column label="通话时长" align="center" prop="callTime" :formatter="callTimeFormatter"/>
      <el-table-column label="单价" align="center" prop="callPrice" />
      <el-table-column label="金额" align="center" prop="callAmount" />
      <el-table-column label="删除状态" align="center" prop="deleteFlag" >
        <template v-slot="scope">
          <cai-dict-tag :options="deleteFlagList" :value="scope.row.deleteFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="链路号" align="center" prop="traceId" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="140" fixed="right">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              v-if="scope.row.traceId"
              @click="handleConsumerLog(scope.row)"
          >分销
          </el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cai:userCall:edit']"
          >修改</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <consume-log-info v-if="consumeLogInfoVisible" ref="consumeLogInfo" />
    <user-call-info v-if="userCallInfoVisible" ref="userCallInfo" />
  </div>
</template>

<script>
import {listUserCall} from "@/api/cai/userCall";
import ConsumeLogInfo from "@/views/cai/consumeLog/consume-log-info";
import UserCallInfo from "@/views/cai/userCall/user-call-info";
import {deleteFlagList, userCallStatusList} from "@/constant/statusMap";

export default {
  name: "UserCall",
  components:{
    ConsumeLogInfo,UserCallInfo
  },
  data() {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      userCallStatusList,deleteFlagList,
      consumeLogInfoVisible: false,
      userCallInfoVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通话记录表格数据
      userCallList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: undefined,
        fromUsercode: undefined,
        toUsercode: undefined,
        status: undefined,
        deleteFlag: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {

    callTimeFormatter(row,column){
      let callTime = row.callTime;
      if(!callTime){
        return "00:00:00"
      }
      const hour = parseInt(callTime / 3600) % 24;
      const minute = parseInt((callTime % 3600) / 60);
      const second = callTime % 60;
      return [hour, minute, second].map(function(item) {
        return item < 10 ? '0' + item : item;
      }).join(':');
    },
    /** 查询通话记录列表 */
    getList() {
      this.loading = true;
      listUserCall(this.queryParams).then(response => {
        this.userCallList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleConsumerLog(row) {
      this.consumeLogInfoVisible = true
      this.$nextTick(() => {
        this.$refs.consumeLogInfo.traceIdInit(row.traceId)
      })
    },
    handleInfo(row){
      this.userCallInfoVisible = true
      this.$nextTick(() => {
        this.$refs.userCallInfo.init(row.id)
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
    },
  }
};
</script>
