<template>
  <div>
    <!-- :validate-form="validForm" -->
    <sd-webflow
      ref="flow"
      :removed-tabs="['sdRelatedDoc']"
      :validate-form="validForm"
      @actionBtnClick="actionBtnClick"
      @saveproject="saveproject"
      @afterDispatch="afterDispatch"
    >
      <template v-slot:form="{ model, fields }">
        <table>
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="Expand"
                :expand-str="'Expand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="Expand">
          <tr v-show="initData(model)">
            <!-- ID -->
            <!-- <sd-form-item-td name="id" /> -->
            <!-- 随机ID -->
            <!-- <sd-form-item-td name="sourceId" /> -->
          </tr>
          <tr>
            <!-- 流程名称 -->
            <sd-form-item-td name="prcName">
              {{ model.prcName }}
            </sd-form-item-td>
            <!-- 测试结果 -->
            <sd-form-item-td name="testResult" />
          </tr>
          <tr>
            <!-- 测试结果描述 -->
            <sd-form-item-td name="testResultDesc" :colspan="3">
              <a-textarea v-model="model.testResultDesc" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 测试人员 -->
            <!-- <sd-form-item-td name="testUserOpt" /> -->
            <sd-form-item-td name="testUserName">
              {{ model.testUserName }}
            </sd-form-item-td>
            <!-- 测试日期 -->
            <sd-form-item-td name="testTime" />
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="icEvaluationCrossStepList" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="stepExpand"
                    :expand-str="'stepExpand'"
                    :group-label="'测试步骤'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        ref="stepData"
                        v-model="model.icEvaluationCrossStepList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="!editable"
                        :key="key"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ]
                            .concat(Array.from(fields.icEvaluationCrossStepList.attr.dync))
                            .concat([
                              {
                                caption: '操作',
                                name: 'operation',
                                dataType: 'string',
                              },
                            ])
                        "
                        :columns="columns"
                        :flagpage="true"
                      >
                        <!-- <template v-slot:contactsOpt="{ field, text, index, value }">
                          {{ value[index].contactsName }}
                        </template> -->
                        <template v-slot:whetherSamples="{ field, text, index, value }">
                          <a-radio-group
                            v-model="value[index].whetherSamples"
                            :disabled="fields.icEvaluationCrossStepList.readonly"
                            @change="onChange(value[index].whetherSamples, index)"
                          >
                            <a-radio
                              v-for="item in field.attr.selectListItem"
                              :key="item.value"
                              :value="item.value"
                            >
                              {{ item.label }}
                            </a-radio>
                          </a-radio-group>
                        </template>
                        <template v-slot:operation="{ field, text, index, value }">
                          <a-button
                            v-if="
                              simpleFlag[index] === index || value[index].whetherSamples === '01'
                            "
                            type="primary"
                            size="small"
                            @click="simpleInfo(fields.icEvaluationCrossStepList, index)"
                            >样本信息</a-button
                          >
                          <span v-else> </span>
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table>

        <!--
        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="icEvaluationCrossSampleList" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="infoExpand"
                    :expand-str="'infoExpand'"
                    :group-label="'样本信息'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        ref="infoData"
                        v-model="model.icEvaluationCrossSampleList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.icEvaluationCrossSampleList.attr.dync))
                        "
                        :columns="infocolumns"
                        :flagpage="true"
                      >
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table> -->

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="icEvaluationCrossMeasureList" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="measureExpand"
                    :expand-str="'measureExpand'"
                    :group-label="'控制措施信息'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        ref="infoData"
                        v-model="model.icEvaluationCrossMeasureList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="!editable"
                        :addbuttonvisiable="false"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.icEvaluationCrossMeasureList.attr.dync))
                        "
                        :columns="measurecolumns"
                        :flagpage="true"
                      >
                        <template v-slot:testResult="{ field, text, index, value }">
                          <a-select v-model="value[index][field.name]" v-if="editable">
                            <a-select-option
                              :value="item.id"
                              :key="item.id"
                              v-for="item in testResultoption"
                            >
                              {{ item.name }}
                            </a-select-option>
                          </a-select>
                          <span v-else>{{
                            testResultoption.find((i) => i.id === value[index][field.name])
                              ? testResultoption.find((i) => i.id === value[index][field.name]).name
                              : ''
                          }}</span>
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table>
      </template>
    </sd-webflow>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import { Modal, message } from 'ant-design-vue'
import IamLightService from '@product/iam/components/iam-light-service'
import IcEvaluationService from '../ic-evaluation-service'
import MtxProcessService from '../../mtxprocess/mtx-process-service'
import auditAdvancedGroup from '../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../components/audit-advanced-group-mixins'
import components from './_import-components/ic-evaluation-work-cross-form-import'

export default {
  name: 'IcEvaluationWorkCrossForm',
  metaInfo: {
    title: '穿行措施底稿',
  },
  components: {
    ...components,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      Expand: true,
      infoExpand: true,
      stepExpand: true,
      measureExpand: true,
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'sourceId', sdHidden: true },
        {
          dataIndex: 'stepName',
          title: '步骤名称',
          width: '20%',
        },
        { dataIndex: 'stepReason', title: '步骤说明', width: '30%' },
        { dataIndex: 'whetherSamples', title: '是否涉及样本', width: '13%' },
        { dataIndex: 'testResult', title: '测试结论', width: '20%' },
        { dataIndex: 'icEvaluationCrossSampleList', sdHidden: true },
        {
          title: '附件',
          dataIndex: 'attachment',
          sdHidden: true,
        },
      ],
      infocolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'sourceId', sdHidden: true },
        {
          dataIndex: 'sampleName',
          title: '样本名称',
          width: '30%',
        },
        { dataIndex: 'sampleDesc', title: '样本描述', width: '30%' },
        {
          title: '附件',
          dataIndex: 'attachment',
        },
      ],
      measurecolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'sourceId', sdHidden: true },
        { title: '控制点名称', dataIndex: 'measureName' },
        { title: '控制点编号', dataIndex: 'measureCode' },
        { title: '测试结果', dataIndex: 'testResult' },
        { title: '缺陷类型', dataIndex: 'defectType' },
        { title: '缺陷等级', dataIndex: 'defectLevel' },
        { title: '缺陷责任组织', dataIndex: 'defectDeptOpt' },
        { title: '缺陷描述', dataIndex: 'defectDesc' },
        { title: '缺陷原因', dataIndex: 'defectReason' },
        { dataIndex: 'defectDeptName', sdHidden: true },
      ],
      orgId: null,
      versionId: null,
      flag: true,
      testResultoption: [],
      simpleFlag: [],
      key: 0,
    }
  },
  mounted() {
    this.initDictionaryInfo()
  },
  methods: {
    simpleInfo(model, index) {
      const stepInfo = this.$refs.flow.getFieldValue('icEvaluationCrossStepList')
      IamLightService.simpleInfo(model, stepInfo[index]).then((info) => {
        if (info.length) {
          const cloneData = JSON.parse(JSON.stringify(stepInfo))
          cloneData[index].icEvaluationCrossSampleList = JSON.stringify(info)
          this.$refs.flow.setFieldValue('icEvaluationCrossStepList', cloneData)
          this.key++
        }
      })
    },
    onChange(val, index) {
      if (val === '01') {
        this.simpleFlag[index] = index
      } else {
        this.simpleFlag[index] = null
      }
      this.key++
    },
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=TEST_RESULT',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.testResultoption = res.data
        }
      })
    },
    initData(model) {
      // this.$refs.flow?.setFieldValue('sourceId', -1)
      return false
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'save') {
      }
    },
    // 保存操作，记录标识位
    saveproject() {
      this.isSave = true
    },
    /**
     * 提交后事件
     */
    afterDispatch() {
      // 提交也认为是保存
      this.isSave = true
    },
    // 自定义校验
    validForm() {
      return new Promise((resolve, reject) => {
        // 获取环节号
        const flowNum = this.$refs.flow.flowData.attrs.stepN
        // 起草环节才校验 校验是否选择了缺陷
        if (flowNum === 0) {
          const measureList = this.$refs.flow.getFieldValue('icEvaluationCrossMeasureList')
          measureList.forEach((m) => {
            if (m.defectDeptOpt !== undefined) {
              const opt = JSON.parse(m.defectDeptOpt)
              let oname = ''
              let oid = ''
              opt.forEach((o) => {
                oname += ',' + o.name
                oid += ',' + o.code
              })
              oname = oname !== '' ? oname.substring(1, oname.length) : oname
              oid = oid !== '' ? oid.substring(1, oid.length) : oid
              m.defectDeptName = oname
              m.defectDeptId = oid
            }
            this.$refs.flow.setFieldValue('icEvaluationCrossMeasureList', measureList)
          })
          resolve(true)
        } else {
          resolve(true)
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
