<template>
  <div :class="$style.wrapHeight">
    <a-card>
      <div
        v-if="listType !== 'four' && (dataType === 'all') | (dataType === 'control')"
        :class="$style.tableDiv"
      >
        <div :class="$style.tableTitle">控制测试列表</div>
        <!-- 控制测试 -->
        <sd-data-table-ex
          :key="controlKey"
          ref="overviewTable1"
          :data-url="controlDataUrl"
          :columns="controlColumns"
          form-id=""
          page-id=""
          :filter-expressions="controlExpressions"
          :process-req="controlProcessReq"
          :search-fields="['measureName', 'prcName', 'measureCode']"
          @tableDataLoaded="dataLoaded"
        >
          <template slot="islink" slot-scope="text, record">
            <a @click="openversion(record)">{{ text }}</a>
          </template>
          <template slot="defecttype" slot-scope="text, record">
            {{ getDictValue(defectType, text) }}
          </template>
          <template slot="defectlv" slot-scope="text, record">
            {{ getDictValue(defectLevel, text) }}
          </template>
          <template slot="testres" slot-scope="text, record">
            {{ getDictValue(testResultDict, text) }}
          </template>
        </sd-data-table-ex>
      </div>
      <div
        v-if="(listType === 'four') | ((dataType === 'all') | (dataType === 'cross'))"
        :class="$style.tableDiv"
      >
        <div :class="$style.tableTitle">{{
          listType !== 'four' ? '穿行测试列表' : '测试缺陷汇总'
        }}</div>
        <!-- 穿行测试 -->
        <sd-data-table-ex
          :key="crossKey"
          ref="overviewTable2"
          :data-url="crossDataUrl"
          form-id=""
          page-id=""
          :columns="crossColumns"
          :filter-expressions="crossExpressions"
          :process-req="crossProcessReq"
          :search-fields="['prcName', 'prcCode']"
        >
          <template slot="islink" slot-scope="text, record">
            <a @click="openversion(record)">{{ text }}</a>
          </template>
          <template slot="defecttype" slot-scope="text, record">
            {{ getDictValue(defectType, text) }}
          </template>
          <template slot="defectlv" slot-scope="text, record">
            {{ getDictValue(defectLevel, text) }}
          </template>
          <template slot="testres" slot-scope="text, record">
            {{ getDictValue(testResultDict, text) }}
          </template>
        </sd-data-table-ex>
      </div>
    </a-card>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import systemManage from '@/system-manage/system-manage'
import components from './_import-components/iam-ic-evaluation-overview-list-import'
import iamIcEvaluationOverviewServices from './iam-ic-evaluation-overview-services'
const firstColumnsControl = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '内控流程分类',
    dataIndex: 'PRC_NAME',
  },
  {
    title: '控制点编号',
    dataIndex: 'MEASURE_CODE',
  },

  {
    title: '控制点名称',
    dataIndex: 'MEASURE_NAME',
  },
  {
    title: '缺陷级别',
    dataIndex: 'DEFECT_LEVEL',
    scopedSlots: { customRender: 'defectlv' },
  },
  {
    title: '缺陷类型',
    dataIndex: 'DEFECT_TYPE',
    scopedSlots: { customRender: 'defecttype' },
  },
  {
    title: '测试人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  {
    title: '当前处理人',
    dataIndex: 'NOWUSER',
  },
]
const firstColumnsCross = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '内控流程分类',
    dataIndex: 'PRC_NAME',
  },
  {
    title: '末级流程分类编号',
    dataIndex: 'PRC_CODE',
  },
  {
    title: '测试人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  {
    title: '当前处理人',
    dataIndex: 'NOWUSER',
  },
]
const threeColumnsControl = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '内控流程分类',
    dataIndex: 'PRC_NAME',
  },
  {
    title: '控制点名称',
    dataIndex: 'MEASURE_NAME',
  },
  {
    title: '控制点编号',
    dataIndex: 'MEASURE_CODE',
  },
  {
    title: '测试人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  {
    title: '测试结果',
    dataIndex: 'TEST_RESULT',
    scopedSlots: { customRender: 'testres' },
  },
]
const threeColumnsCross = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '内控流程分类',
    dataIndex: 'PRC_NAME',
  },
  {
    title: '末级流程分类编号',
    dataIndex: 'PRC_CODE',
  },
  {
    title: '测试人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  {
    title: '测试结果',
    dataIndex: 'TEST_RESULT',
    scopedSlots: { customRender: 'testres' },
  },
]
const fourColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '内控流程',
    dataIndex: 'PRC_NAME',
  },
  {
    title: '控制措施名称',
    dataIndex: 'MEASURE_NAME',
  },
  {
    title: '控制措施编号',
    dataIndex: 'MEASURE_CODE',
  },
  {
    title: '测试结果',
    dataIndex: 'TEST_RESULT',
    scopedSlots: { customRender: 'testres' },
  },
  {
    title: '缺陷类型',
    dataIndex: 'DEFECT_TYPE',
    scopedSlots: { customRender: 'defecttype' },
  },
  {
    title: '缺陷等级',
    dataIndex: 'DEFECT_LEVEL',
    scopedSlots: { customRender: 'defectlv' },
  },
  {
    title: '缺陷描述',
    dataIndex: 'DEFECT_DESC',
  },
  {
    title: '测试人',
    dataIndex: 'TEST_USER_NAME',
  },
]
export default {
  name: 'IamIcEvaluationOverviewList',
  metaInfo: {
    title: '内控概况查询列表',
  },
  components: {
    ...components,
  },
  data() {
    return {
      defectType: [], // 缺陷类型
      defectLevel: [], // 缺陷级别
      firstColumnsControl,
      firstColumnsCross,
      threeColumnsControl,
      threeColumnsCross,
      fourColumns,
      waterMark: systemManage.getFormWaterMark(),
      controlKey: 0,
      crossKey: 0,
      columns: [],
      controlExpressions: [],
      crossExpressions: [],
      controlDataUrl: '',
      crossDataUrl: '',
      controlColumns: [],
      crossColumns: [],
      listType: '',
      dataType: '',
      endType: null,
      userAccount: null,
      params: {},
      loadCross: false,
      testResult: null,
      testResultDict: [],
      defectTypeText: null,
      defectLevelText: null,
      mtxPrcPathData: {},
    }
  },
  mounted() {
    // 获取缺陷类型与级别
    iamIcEvaluationOverviewServices
      .getDictValue('DEFECT_TYPE')
      .then((res) => {
        if (res.status === 200) {
          this.defectType = res.data
          return iamIcEvaluationOverviewServices.getDictValue('DEFECT_LEVEL')
        }
      })
      .then((res) => {
        if (res.status === 200) {
          this.defectLevel = res.data
          return iamIcEvaluationOverviewServices.getDictValue('TEST_RESULT')
        }
      })
      .then((res) => {
        if (res.status === 200) {
          this.testResultDict = res.data
          // 根据统计类型展示不同的列
          this.params = JSON.parse(this.$route.query.params)
          this.dataType = this.$route.query.dataType
          this.listType = this.$route.query.listType
          this.endType = this.$route.query.endType
          this.userAccount = this.$route.query.userAccount
          this.testResult = this.$route.query.testResult
          if ((this.listType === 'first') | (this.listType === 'second')) {
            this.controlDataUrl = 'api/xcoa-mobile/v1/ic-statistics/getBasicDrilList'
            this.crossDataUrl = 'api/xcoa-mobile/v1/ic-statistics/getBasicDrilList'
            this.controlColumns = this.firstColumnsControl
            this.crossColumns = this.firstColumnsCross
          }
          if (this.listType === 'three') {
            this.controlDataUrl = 'api/xcoa-mobile/v1/ic-statistics/getBasicDrilList'
            this.crossDataUrl = 'api/xcoa-mobile/v1/ic-statistics/getBasicDrilList'
            this.controlColumns = this.threeColumnsControl
            this.crossColumns = this.threeColumnsCross
          }
          if (this.listType === 'four') {
            this.defectTypeText = this.$route.query.defectType
            this.defectLevelText = this.$route.query.defectLevel
            this.crossDataUrl = 'api/xcoa-mobile/v1/ic-statistics/getCollectList'
            this.crossColumns = this.fourColumns
          }
        }
      })
  },
  methods: {
    // 获取字典值
    getDictValue(list, val) {
      return list.find((item) => {
        return item.id.trim() === (val + '').trim()
      })?.name
    },
    dataLoaded() {
      this.loadCross = true
      this.crossKey++
    },
    // 控制请求处理
    controlProcessReq(req) {
      const maxNum = req.data.maxResults
      const startNum = req.data.startPosition
      const exp = req.data.expressions
      req.data = this.params
      req.data.expressions = exp
      req.data.fieldName = 'control'
      req.data.endType = this.endType
      if (this.userAccount) {
        req.data.testUserAccount = "'" + this.userAccount + "'"
      }
      if (this.testResult) {
        req.data.testResult = "'" + this.testResult + "'"
      }
      // 缺陷类型
      if (this.defectTypeText) {
        req.data.defectType = "'" + this.defectTypeText + "'"
      }
      // 缺陷级别
      if (this.defectLevelText) {
        req.data.defectLevel = "'" + this.defectLevelText + "'"
      }

      req.data.maxResults = maxNum
      req.data.startPosition = startNum
      return req
    },
    // 穿行请求处理
    crossProcessReq(req) {
      const maxNum = req.data.maxResults
      const startNum = req.data.startPosition
      const exp = req.data.expressions
      req.data = this.params
      req.data.expressions = exp
      if (this.loadCross | (this.listType === 'four') | (this.dataType === 'cross')) {
        req.data.fieldName = 'cross'
        if (this.listType === 'four') {
          req.data.fieldName = this.dataType
        }
        req.data.endType = this.endType
        if (this.userAccount) {
          req.data.testUserAccount = "'" + this.userAccount + "'"
        }
        if (this.testResult) {
          req.data.testResult = "'" + this.testResult + "'"
        }
        // 缺陷类型
        if (this.defectTypeText) {
          req.data.defectType = "'" + this.defectTypeText + "'"
        }
        // 缺陷级别
        if (this.defectLevelText) {
          req.data.defectLevel = "'" + this.defectLevelText + "'"
        }
      }
      req.data.maxResults = maxNum
      req.data.startPosition = startNum
      return req
    },
    // 获取内控流程分类
    getMtxPrcPath(record) {
      iamIcEvaluationOverviewServices.getMtxPrcPath(record.MTX_PRC_ID).then((res) => {
        this.$set(this.mtxPrcPathData, 'mtxPrcPath' + record.MTX_PRC_ID, res.data)
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;
  .table-div {
    min-height: 450px;
  }
  .table-title {
    position: absolute;
    font-size: 17px;
    font-weight: bold;
    color: #44a5ff;
    margin: 0 3px;
  }
}
</style>
