<template>
  <sd-detail-modal
    ref="resultModal"
    :record-id="model.id"
    :page-id="pageId"
    :modal-props="{ width: 1200 }"
    :read-only="readonly"
    @actionBtnClick="actionBtnClick"
  >
    <template v-slot="{ model, fields }">
      <table :class="$style.table">
        <sd-form-colgroup :columns="4" />
        <tr>
          <!-- 控制点编号 -->
          <sd-form-item-td name="measureCode" />
          <!-- 主流程名称 -->
          <sd-form-item-td name="mainPrcName" />
        </tr>
        <tr>
          <!-- 子流程名称 -->
          <sd-form-item-td name="subPrcName" />
          <!-- 子流程编号 -->
          <sd-form-item-td name="subPrcCode" />
        </tr>
        <tr>
          <!-- 控制目标 -->
          <sd-form-item-td name="controlTarget" :colspan="3" />
        </tr>
        <tr>
          <!-- 主要风险 -->
          <sd-form-item-td name="majorRisk" :colspan="3" />
        </tr>
        <tr>
          <!-- 内部控制程序 -->
          <sd-form-item-td name="controlProgram" :colspan="3" />
        </tr>
        <tr>
          <!-- 测试结果 -->
          <sd-form-item-td name="testResult" :colspan="3" />
        </tr>
        <tr>
          <!-- 测试结果描述 -->
          <sd-form-item-td name="resultContent" :colspan="3" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 缺陷名称 -->
          <sd-form-item-td name="defectName" :colspan="3" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 缺陷描述 -->
          <sd-form-item-td name="defectDesc" :colspan="3" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 整改意见及建议 -->
          <sd-form-item-td name="bugSuggestions" :colspan="3" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 缺陷类型 -->
          <sd-form-item-td name="defectType" :hidden="model.testResult !== '02'" />
          <!-- 缺陷等级 -->
          <sd-form-item-td name="defectLevel" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 责任人 -->
          <sd-form-item-td
            name="responsiblePerson"
            :hidden="model.testResult !== '02'"
            @change="selectPerson"
          />
          <!-- 责任部门 -->
          <sd-form-item-td name="defectDeptOpt" :hidden="model.testResult !== '02'" />
        </tr>
        <tr>
          <!-- 责任岗位 -->
          <sd-form-item-td name="responsiblePos" :hidden="model.testResult !== '02'" :colspan="3" />
        </tr>
        <tr>
          <!-- 失效样本附件 -->
          <sd-form-item-td name="attachment" :colspan="3">
            <template v-slot:read-and-edit>
              <sd-attachment
                :key="groupId"
                :group-id="groupId ? groupId : JSON.parse(fields.attachment.value).value"
                :read-only="readonly"
              />
            </template>
          </sd-form-item-td>
        </tr>
        <tr>
          <!-- 是否移交线索 -->
          <sd-form-item-td name="isClues" />
          <!-- 移交对象 -->
          <sd-form-item-td name="handOver" />
        </tr>
        <tr>
          <!-- 测试人员 -->
          <sd-form-item-td name="testPerson" />
          <!-- 测试时间 -->
          <sd-form-item-td name="testDate" />
        </tr>
        <tr>
          <td :colspan="4">
            <sd-form-item name="isubIcEvaluationCrossMeasure" :label="null">
              <template v-slot:read-and-edit="{ editable }">
                <audit-advanced-group
                  :expand="expandB"
                  :expand-str="'expandB'"
                  :group-label="'缺陷涉及金额列表'"
                  @changedClick="changedClick"
                  ><template>
                    <xm-child-table
                      ref="docformMeasureRel"
                      v-model="model.isubIcEvaluationCrossMeasure"
                      :read-only="!editable"
                      label=""
                      :fields="
                        [
                          {
                            caption: '序号',
                            name: 'sortNum',
                            dataType: 'number',
                            attr: {},
                          },
                        ].concat(Array.from(fields.isubIcEvaluationCrossMeasure.attr.dync))
                      "
                      :columns="childColumns"
                    >
                    </xm-child-table
                  ></template>
                </audit-advanced-group>
              </template>
            </sd-form-item>
          </td>
        </tr>
      </table>
    </template>
  </sd-detail-modal>
</template>

<script>
import moment from 'moment'
import axios from '@/common/services/axios-instance'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import IcEvaluationService from './ic-evaluation-service'
import components from './_import-components/ic-write-result-modal-import'

export default {
  name: 'IcWriteResultModal',
  components: {
    auditAdvancedGroup,
    ...components,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      expandB: true,
      childColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'type' },
        { dataIndex: 'pay' },
      ],
      groupId: null,
    }
  },
  mounted() {
    this.$refs.resultModal.show()
    const _this = this
    setTimeout(() => {
      axios({
        url: 'api/framework/v1/page/wp/ic/measure/icMtxMeasure?id=' + this.model.sourceId,
        method: 'get',
      }).then((res) => {
        const controlTarget = res.data.pageFormData.pageFieldInfos.find(
          (i) => i.name === 'controlTarget'
        )
        const measureCode = res.data.pageFormData.pageFieldInfos.find(
          (i) => i.name === 'measureCode'
        )
        const subPrcCode = res.data.pageFormData.pageFieldInfos.find((i) => i.name === 'subPrcCode')
        const subPrcName = res.data.pageFormData.pageFieldInfos.find((i) => i.name === 'subPrcName')
        const mainPrcName = res.data.pageFormData.pageFieldInfos.find(
          (i) => i.name === 'mainPrcName'
        )
        const majorRisk = res.data.pageFormData.pageFieldInfos.find((i) => i.name === 'majorRisk')
        const controlProgram = res.data.pageFormData.pageFieldInfos.find(
          (i) => i.name === 'controlProgram'
        )
        _this.$refs.resultModal.setFieldsValue({
          controlTarget: controlTarget.value,
          measureCode: measureCode.value,
          subPrcCode: subPrcCode.value,
          subPrcName: subPrcName.value,
          mainPrcName: mainPrcName.value,
          majorRisk: majorRisk.value,
          controlProgram: controlProgram.value,
        })
        const fields = [
          'controlTarget',
          'measureCode',
          'subPrcCode',
          'subPrcName',
          'mainPrcName',
          'majorRisk',
          'controlProgram',
          'attachment',
        ]
        const parseFields = [
          'responsiblePerson',
          'defectDeptOpt',
          'testPerson',
          'isubIcEvaluationCrossMeasure',
        ]
        for (const key in _this.model) {
          if (fields.indexOf(key) === -1 && _this.model[key]) {
            if (key === 'testDate') {
              _this.$refs.resultModal.setFieldValue(key, moment(_this.model[key]))
            } else if (parseFields.includes(key)) {
              _this.$refs.resultModal.setFieldValue(key, JSON.parse(_this.model[key]))
            } else {
              _this.$refs.resultModal.setFieldValue(key, _this.model[key])
            }
          }
        }
        _this.groupId = _this.model.attachment ? JSON.parse(_this.model.attachment).value : null
      })
    }, 1000)
  },
  methods: {
    // 选择缺陷责任人
    selectPerson(user) {
      const p = user.map((item) =>
        IcEvaluationService.getDeptByUser({ account: item.props.account })
      )
      Promise.all(p).then((res) => {
        let dept = res.map((item) => {
          return {
            code: item.data.department.id,
            name: item.data.department.name,
            type: 'Group',
          }
        })
        dept = this.uniqueFunc(dept, 'code')
        this.$refs.resultModal.setFieldValue('defectDeptOpt', dept)
      })
    },
    uniqueFunc(arr, uniId) {
      const res = new Map()
      return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1))
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.table {
  :global(.ant-input-number) {
    width: 100%;
  }
}
</style>
