<template>
  <div :class="$style.wrapHeight">
    <a-card>
      <div :class="$style.tableDiv">
        <sd-data-table-ex
          v-if="loading"
          :key="controlKey"
          ref="dataTable"
          :data-url="dataUrl"
          :columns="columns"
          form-id=""
          page-id=""
          :filter-expressions="expressions"
          :process-req="processReq"
          :search-fields="searchFields"
        >
          <template slot="identifyTypeDict" slot-scope="text, record">
            {{ getDictValue(identifyTypeDict, text) }}
          </template>
          <template slot="riskTrendDict" slot-scope="text, record">
            {{ getOneDictValue(riskTrendDict, text) }}
          </template>
          <template slot="select" slot-scope="text, record">
            {{
              text && JSON.parse(text) && typeof JSON.parse(text) === 'object'
                ? JSON.parse(text)[0].name
                : text
            }}
          </template>
        </sd-data-table-ex>
      </div>
    </a-card>
  </div>
</template>

<script>
import TableColumnTypes from '@/common/services/table-column-types'
import systemManage from '@/system-manage/system-manage'
import components from './_import-components/risk-task-overview-list-import'
import riskTaskOverviewServices from './risk-task-overview-services'
const firstColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '标题',
    dataIndex: 'TITLE',
    width: '35%',
  },
  {
    title: '编制人员',
    dataIndex: 'CREATOR_NAME',
  },

  {
    title: '编制日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
    defaultSortOrder: 'descend',
  },
  {
    title: '流程状态',
    dataIndex: 'FLOW_STATE',
  },
  {
    title: '当前处理人',
    dataIndex: 'CURRENTUSER',
  },
  {
    title: '识别人',
    dataIndex: 'CONTACTS_NAME',
  },
]
const secondColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '标题',
    dataIndex: 'TITLE',
    width: '35%',
  },
  {
    title: '编制人员',
    dataIndex: 'CREATOR_NAME',
  },

  {
    title: '编制日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
    defaultSortOrder: 'descend',
  },
  {
    title: '风险识别类型',
    dataIndex: 'IDENTIFY_TYPE',
  },
  {
    title: '识别人',
    dataIndex: 'CONTACTS_NAME',
  },
]
const threeColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '风险分类',
    dataIndex: 'RISK_CATEGORY_PATH',
  },
  {
    title: '风险事项编号',
    dataIndex: 'EVENT_CODE',
  },
  {
    title: '风险事项名称',
    dataIndex: 'EVENT_NAME',
  },
  {
    title: '发生可能性(P)',
    dataIndex: 'OCCUR_POSSIBILITY',
    scopedSlots: { customRender: 'select' },
  },
  {
    title: '风险影响程度(C)',
    dataIndex: 'RISK_IMPACT_DEGREE',
    scopedSlots: { customRender: 'select' },
  },
  {
    title: '风险等级',
    dataIndex: 'RISK_LEVEL',
  },
  {
    title: '未来风险趋势',
    dataIndex: 'RISK_TREND',
    scopedSlots: { customRender: 'riskTrendDict' },
  },
  {
    title: '评估人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '评估日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  { title: '当前处理人', dataIndex: 'CURRENTUSER', width: '120px' },
]
const fourColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '风险分类',
    dataIndex: 'RISK_CATEGORY_PATH',
  },
  {
    title: '风险事项编号',
    dataIndex: 'EVENT_CODE',
  },
  {
    title: '风险事项名称',
    dataIndex: 'EVENT_NAME',
  },
  {
    title: '发生可能性(P)',
    dataIndex: 'OCCUR_POSSIBILITY',
    scopedSlots: { customRender: 'select' },
  },
  {
    title: '风险影响程度(C)',
    dataIndex: 'RISK_IMPACT_DEGREE',
    scopedSlots: { customRender: 'select' },
  },
  {
    title: '风险等级',
    dataIndex: 'RISK_LEVEL',
  },
  {
    title: '未来风险趋势',
    dataIndex: 'RISK_TREND',
    scopedSlots: { customRender: 'riskTrendDict' },
  },
  {
    title: '评估人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '评估日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
  },
]
const fiveColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '风险分类',
    dataIndex: 'RISK_CATEGORY_PATH',
  },
  {
    title: '风险事项编号',
    dataIndex: 'EVENT_CODE',
  },
  {
    title: '风险事项名称',
    dataIndex: 'EVENT_NAME',
  },
  {
    title: '风险应对策略',
    dataIndex: 'RISK_RESPONSE_STRATEGY',
  },
  {
    title: '责任部门',
    dataIndex: 'RESPONSIBLE_DEPT_NAME',
  },
  {
    title: '应对完成状态',
    dataIndex: 'RESPONSE_COMPLETION_STATE',
  },
  {
    title: '填报人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '填报日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
  },
  {
    title: '当前状态',
    dataIndex: 'FLOW_STATE',
  },
  { title: '当前处理人', dataIndex: 'CURRENTUSER', width: '120px' },
]
const sixColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '风险分类',
    dataIndex: 'RISK_CATEGORY_PATH',
  },
  {
    title: '风险事项编号',
    dataIndex: 'EVENT_CODE',
  },
  {
    title: '风险事项名称',
    dataIndex: 'EVENT_NAME',
  },
  {
    title: '风险应对策略',
    dataIndex: 'RISK_RESPONSE_STRATEGY',
  },
  {
    title: '责任部门',
    dataIndex: 'RESPONSIBLE_DEPT_NAME',
  },
  {
    title: '应对完成状态',
    dataIndex: 'RESPONSE_COMPLETION_STATE',
  },
  {
    title: '填报人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '填报日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
  },
]
const sevenColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '风险分类',
    dataIndex: 'RISK_CATEGORY_PATH',
  },
  {
    title: '风险事项编号',
    dataIndex: 'EVENT_CODE',
  },
  {
    title: '风险事项名称',
    dataIndex: 'EVENT_NAME',
  },
  {
    title: '风险应对策略',
    dataIndex: 'RISK_RESPONSE_STRATEGY',
  },
  {
    title: '责任部门',
    dataIndex: 'RESPONSIBLE_DEPT_NAME',
  },
  {
    title: '应对完成状态',
    dataIndex: 'RESPONSE_COMPLETION_STATE',
  },
  {
    title: '填报人员',
    dataIndex: 'TEST_USER_NAME',
  },
  {
    title: '填报日期',
    dataIndex: 'CREATION_TIME',
    sdRender: TableColumnTypes.date,
  },
]
export default {
  name: 'RiskTaskOverviewList',
  metaInfo: {
    title: '任务概况查询列表',
  },
  components: {
    ...components,
  },
  data() {
    return {
      loading: false,
      searchFields: [],
      identifyTypeDict: [], // 风险识别类型
      riskTrendDict: [], // 缺陷级别
      firstColumns,
      secondColumns,
      threeColumns,
      fourColumns,
      fiveColumns,
      sixColumns,
      sevenColumns,
      waterMark: systemManage.getFormWaterMark(),
      controlKey: 0,
      columns: [],
      expressions: [],
      dataUrl: '',
      listType: '',
      dataType: '',
      endType: null,
      userAccount: null,
      params: {},
      loadCross: false,
      mtxPrcPathData: {},
    }
  },
  mounted() {
    // 获取字典 缺陷类型和整改状态
    riskTaskOverviewServices
      .getDictValue('IDENTIFY_TYPE')
      .then((res) => {
        this.identifyTypeDict = res.data.map((item) => {
          return {
            label: item.name,
            value: item.id,
            title: item.name,
          }
        })
        return riskTaskOverviewServices.getDictValue('RISK_TREND')
      })
      .then((res) => {
        this.riskTrendDict = res.data.map((item) => {
          return {
            label: item.name,
            value: item.id,
            title: item.name,
          }
        })
      })
    // 根据统计类型展示不同的列
    this.taskId = this.$route.query.taskId
    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.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.firstColumns
      this.searchFields = ['TITLE']
      this.params = {
        taskId: this.taskId,
        columnValue: 'identifyDrill',
        recordValue: this.endType,
      }
    }
    if (this.listType === 'second') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.secondColumns
      this.searchFields = ['TITLE']
      this.params = {
        taskId: this.taskId,
        columnValue: 'identifyDrill',
        recordValue: this.dataType,
      }
    }
    if (this.listType === 'three') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.threeColumns
      this.searchFields = ['EVENT_NAME']
      this.params = {
        taskId: this.taskId,
        columnValue: 'assessmentDrill',
        recordValue: this.endType,
      }
    }
    if (this.listType === 'four') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.fourColumns
      this.searchFields = ['EVENT_NAME']
      this.params = {
        taskId: this.taskId,
        columnValue: 'assessmentDrill',
        recordValue: this.dataType,
      }
    }
    if (this.listType === 'five') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.fiveColumns
      this.searchFields = ['EVENT_NAME']
      this.params = {
        taskId: this.taskId,
        columnValue: 'responseDrill',
        recordValue: this.endType,
      }
    }
    if (this.listType === 'six') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.sixColumns
      this.searchFields = ['EVENT_NAME']
      this.params = {
        taskId: this.taskId,
        columnValue: 'responseDrill',
        recordValue: this.dataType,
      }
    }
    if (this.listType === 'seven') {
      this.dataUrl = 'api/xcoa-mobile/v1/risk-statistics/getTaskSurveyList'
      this.columns = this.sevenColumns
      this.searchFields = ['EVENT_NAME']
      this.params = {
        taskId: this.taskId,
        columnValue: 'responseDrill',
        recordValue: this.dataType,
      }
    }
    // 最后显示，否则会有多余报错
    this.loading = true
  },
  methods: {
    // 获取字典值
    getDictValue(list, val) {
      const valList = []
      val.split(',').forEach((valItem) => {
        valList.push(
          list.find((item) => {
            return item.value.trim() === valItem.trim()
          })?.label
        )
      })
      return valList.join(',')
    },
    // 获取单值
    getOneDictValue(list, val) {
      return list.find((item) => {
        return item.value === val
      })?.label
    },
    // 控制请求处理
    processReq(req) {
      const maxNum = req.data.maxResults
      const startNum = req.data.startPosition
      const exp = req.data.expressions
      const orderBy = req.data.orderBy
      req.data = this.params
      req.data.expressions = exp
      req.data.orderBy = orderBy
      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) {
      riskTaskOverviewServices.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>
