<template>
  <div class="data-detail">
    <div v-if="analyseFlag" class="page-tip">活动转化金额每天更新 1 次</div>
    <el-input v-model="search.search" @change="handleCurrentChange(1)" type="text" placeholder="请输入姓名/昵称/手机号/会员卡号" prefix-icon="el-icon-search" clearable class="search-bar"></el-input>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="基本信息" min-width="200px" show-overflow-tooltip fixed="left">
        <div slot-scope="{ row }" class="member-info">
          <img class="member-logo" v-if="row.memberImage" :src="row.memberImage" alt="" />
          <svg aria-hidden="true" v-else class="member-logo">
            <use xlink:href="#icon-user-default"></use>
          </svg>
          <div>
            <p class="member-name">
              <template v-if="row.memberNick">
                {{ row.memberNick || '--' }}
              </template>
              <span class="member-subname" v-if="row.memberName">
                {{ row.memberNick ? `(${row.memberName})` : row.memberName }}
              </span>
            </p>
            <p class="member-name" v-if="row.memberMobile">{{ row.memberMobile }}</p>
          </div>
        </div>
      </el-table-column>
      <el-table-column label="会员卡号" min-width="130px" prop="memberCard" show-overflow-tooltip :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="会员等级" min-width="90px" prop="memberGradeName" :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="服务门店" min-width="190px" prop="mainStoreName" :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="通话状态" min-width="80px" prop="aiThirdStatus" show-overflow-tooltip :formatter="(row, col, val) => formatAiStatus(val)"></el-table-column>
      <el-table-column label="通话时长" min-width="80px" prop="aiDurationSec" :formatter="(row, col, val) => (val > 0 ? `${val}s` : '0s')"></el-table-column>
      <el-table-column label="意向标签" min-width="130px" prop="aiLabel" :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column v-if="analyseFlag" label="活动转化金额(实付)" min-width="160px" prop="orderPayAmount" :formatter="(row, col, val) => (val == null ? '--' : val)"></el-table-column>
      <el-table-column label="外呼时间" min-width="100px" prop="aiTime">
        <div style="line-height: 20px" slot-scope="{ row }">
          <p>{{ formatDateTimeByType(row.aiTime, 'yyyy-MM-dd') || '--' }}</p>
          <p>{{ formatDateTimeByType(row.aiTime, 'HH:mm:ss') || '--' }}</p>
        </div>
      </el-table-column>
      <el-table-column v-if="showSendTime" label="短信发送时间" min-width="110px" prop="smsSendTime" fixed="right">
        <div style="line-height: 20px" slot-scope="{ row }">
          <p>{{ formatDateTimeByType(row.smsSendTime, 'yyyy-MM-dd') || '--' }}</p>
          <p>{{ formatDateTimeByType(row.smsSendTime, 'HH:mm:ss') || '--' }}</p>
        </div>
      </el-table-column>
    </el-table>
    <dm-pagination v-if="total > 0" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="total" :current-page="search.pageNum" :page-size="search.pageSize" :page-sizes="[20, 40, 60, 80]" layout="total, sizes, prev, pager, next"></dm-pagination>
  </div>
</template>

<script>
import { formatDateTimeByType } from '@/utils/index.js';
import { getCustomDetail } from '@/service/api/aiApi.js';

export default {
  name: 'AiDataDetail',
  data() {
    return {
      formatDateTimeByType,
      showSendTime: false,
      analyseFlag: false,
      search: {
        activityId: '',
        search: '', // 姓名、昵称 、手机号、会员卡号
        pageNum: 1,
        pageSize: 20
      },
      tableData: [],
      total: 0,
      loading: false
    };
  },
  created() {
    this.search.activityId = this.$route.params.id;
    this.showSendTime = this.$route.query.flag == 1;
    this.analyseFlag = this.$route.query.analyseFlag == 1;
    this.getData();
    console.log('mounted3');
  },
  methods: {
    getData() {
      this.loading = true;
      getCustomDetail(this.search)
        .then(res => {
          const { result, totalCount } = res.result || {};
          this.total = totalCount;
          this.tableData = result;
        })
        .finally(() => (this.loading = false));
    },
    handleCurrentChange(pageNum) {
      this.search.pageNum = pageNum;
      this.getData();
    },
    handleSizeChange(pageSize) {
      this.search.pageSize = pageSize;
      this.handleCurrentChange(1);
    },
    formatAiStatus(state) {
      // "aiThirdStatus":5, //第三方：已完成通话状态    0:已接听,1:拒接,2:无法接通,3:外呼失败,4:空号,5:关机,6:占线,7:停机,8:未接,9:主叫欠费,10:呼损,11:黑名单,12:天盾拦截,22:线路盲区,23呼出拦截,25:无可用线路(可选线路为空、线路超频等)
      const status = {
        0: '已接听',
        1: '拒接',
        2: '无法接通',
        3: '外呼失败',
        4: '空号',
        5: '关机',
        6: '占线',
        7: '停机',
        8: '未接',
        9: '主叫欠费',
        10: '呼损',
        11: '黑名单',
        12: '天盾拦截',
        22: '线路盲区',
        23: '呼出拦截',
        25: '无可用线路' //(可选线路为空、线路超频等)
      };
      return status[state] || '--';
    }
  }
};
</script>

<style lang="scss" scoped>
.data-detail {
  padding: 5px 20px 20px 20px;
  .search-bar {
    margin-bottom: 20px;
    width: 290px;
  }
  .member-info {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .member-logo {
      flex-shrink: 0;
      margin-right: 10px;
      width: 40px;
      height: 40px;
      border-radius: 40px;
      overflow: hidden;
    }
    .member-name {
      font-size: 14px;
      font-weight: 400;
      color: #303133;
      line-height: 20px;
      .member-subname {
        color: #909399;
      }
    }
  }
}
</style>
