<template>
  <div>
    <a-card>
      <div :class="$style.title">审核结果统计</div>
      <div v-show="false">
        <table>
          <tr class="sumnum">
            <td colspan="8" :class="$style.sumtd">总计</td>
            <td>{{ allnum }}</td>
            <td>{{ tgnum }}</td>
            <td>{{ btgnum }}</td>
            <td>{{ tgl }}</td>
          </tr>
        </table>
      </div>
      <sd-data-table-ex
        form-id="iamCubeCount"
        page-id="cube/count/iamCubeCount"
        :columns="columns"
        :actions="actions"
        show-selection
        :search-fields="[]"
        :filter-expressions="expressions"
        @tableDataLoaded="tableDataLoaded"
      >
        <!-- 总数 -->
        <div slot="allnum" slot-scope="text, record">
          <a :title="text" class="allnum" @click="toCubeClick(record)">{{ text }}</a>
        </div>
        <div slot="tgnum" slot-scope="text, record">
          <a :title="text" class="tgnum" @click="toCubeClick(record)">{{ text }}</a>
        </div>
        <div slot="btgnum" slot-scope="text, record">
          <a :title="text" class="btgnum" @click="toCubeClick(record)">{{ text }}</a>
        </div>
        <div slot="tgl" slot-scope="text, record">
          {{
            record.examineNum === '0' || record.examineNum === null
              ? '0'
              : Math.round((record.adoptNum / record.examineNum) * 100, 2) + '%'
          }}
        </div>
        <!-- <div slot="toCube" slot-scope="text, record">
          <a :title="text" @click="toCubeClick(record)">{{ text }}</a>
        </div> -->
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import loginService from '@/login/login-service'
import axios from '@/common/services/axios-instance'
import TableColumnTypes from '@/common/services/table-column-types'
import { sdLocalStorage } from '@/common/services/storage-service'
import { message, Modal } from '@/common/one-ui'
import cubeServices from '@product/iam/cube/cube-services'
import TableActionTypes from '@/common/services/table-action-types'
import components from './_import-components/cube-count-list-import'

export default {
  name: 'CubeCountList',
  metaInfo: {
    title: '审核结果统计',
  },
  components,
  data() {
    return {
      allnum: 0,
      tgnum: 0,
      btgnum: 0,
      tgl: 0,
      expressions: [
        {
          dataType: 'long',
          name: 'isNewest',
          op: 'eq',
          longValue: 1,
        },
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '签订日期',
          dataIndex: 'signDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '合同名称',
          dataIndex: 'contractName',
        },
        {
          title: '合同编号',
          dataIndex: 'contractCode',
        },
        {
          title: '合同金额',
          dataIndex: 'contractAmount',
        },
        {
          title: '合同类型',
          dataIndex: 'contractType',
        },
        {
          title: '合作方名称',
          dataIndex: 'partnerName',
        },
        {
          title: '审核结果数',
          dataIndex: 'examineNum',
          scopedSlots: { customRender: 'allnum' },
        },
        {
          title: '通过数',
          dataIndex: 'adoptNum',
          scopedSlots: { customRender: 'tgnum' },
        },
        {
          title: '不通过数',
          dataIndex: 'notAdoptNum',
          scopedSlots: { customRender: 'btgnum' },
        },
        {
          title: '通过率',
          dataIndex: 'tgl',
          scopedSlots: { customRender: 'tgl' },
        },
        {
          title: '合同ID',
          dataIndex: 'contractId',
          sdHidden: true,
        },
      ],
      actions: [],
    }
  },
  created() {},
  methods: {
    // 数据加载完
    tableDataLoaded(pageInfo) {
      this.allnum = 0
      this.tgnum = 0
      this.btgnum = 0
      this.tgl = 0
      // 没有数据直接退出
      if (pageInfo.total === 0) return

      const getAllNum = setInterval(() => {
        if (document.querySelectorAll('.allnum').length > 0) {
          clearInterval(getAllNum)
        }
        document.querySelectorAll('.allnum').forEach((item) => {
          this.allnum += parseFloat(item.innerHTML)
        })
        document.querySelectorAll('.tgnum').forEach((item) => {
          this.tgnum += parseFloat(item.innerHTML)
        })
        document.querySelectorAll('.btgnum').forEach((item) => {
          this.btgnum += parseFloat(item.innerHTML)
        })

        this.tgl = Math.round((this.tgnum / this.allnum) * 100, 2) + '%'

        document.querySelector('.ant-table-tbody').appendChild(document.querySelector('.sumnum'))
      }, 500)
    },
    // 跳转方法
    toCubeClick(record) {
      cubeServices.checkCubeToken('review').then((res) => {
        if (!res) {
          Modal.warning({
            title: '提示',
            content: '未开启CUBE集成功能参数或单点认证失败，请联系管理员',
          })
          return false
        }
        // 获取本地token
        loginService.updateAccessToken().then(() => {
          axios({
            url: `api/framework/v1/page/wp/cube/contract/iamCubeContract?id=${record.contractId}`,
          }).then((res) => {
            const item = res.data.pageFormData.pageFieldInfos.find((field) => {
              return field.name === 'contractFile'
            }).attr.attachments[0]
            const cubeJson = JSON.parse(sdLocalStorage.getItem('cube') || '{}')
            // 项目修改
            axios.get('api/xcoa-mobile/v1/iamcubecontract/getCubeWebUrl').then((res) => {
              if (res.data !== '') {
                let url =
                  res.data +
                  `/projects/8/o/rule_review?file_path=${
                    location.origin
                  }/api/framework/v1/task-form-process/download-attachments/${
                    item.code
                  }?access_token=${loginService.getTokens().access_token}`
                url += `&access_token=${cubeJson.review_token}`
                url += `&file_id=${item.code}`
                url += `&file_format=${item.suffix}`
                // 获取合同分类
                axios({
                  url:
                    'api/xcoa-mobile/v1/iamcubecontract/findFileCode?accessToken=' +
                    cubeJson.review_token,
                }).then((list) => {
                  // 获取合同分类
                  const docText = res.data.pageFormData.pageFieldInfos.find((field) => {
                    return field.name === 'contractType'
                  }).value
                  const docType = list.data.find((item) => {
                    return item.text === docText
                  })

                  url += `&doc_type=${docType.code}`
                  window.open(url)
                })
              }
            })
          })
        })
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.title {
  margin: 10px;
  font-size: 22px;
  font-weight: bold;
  text-align: center;
}
.sumtd {
  padding: 0;
  font-size: 18px;
  font-weight: bold;
  text-align: center;
  background: #f0f2f5;
}
</style>
