<template>
  <div>
    <sd-webflow
      ref="flow"
      :removed-tabs="['sdRelatedDoc']"
      @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="measureName">
              {{ model.measureName }}
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 控制点编号 -->
            <sd-form-item-td name="measureCode">
              {{ model.measureCode }}
            </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="defectType" />
            <!-- 缺陷等级 -->
            <sd-form-item-td name="defectLevel" />
          </tr>
          <tr>
            <!-- 缺陷责任组织 -->
            <sd-form-item-td name="defectDeptOpt" />
            <!-- 缺陷描述 -->
            <sd-form-item-td name="defectDesc" />
          </tr>
          <tr>
            <!-- 缺陷原因 -->
            <sd-form-item-td name="defectReason" :colspan="3" />
          </tr>
          <tr>
            <!-- 测试人员 -->
            <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="icEvaluationControlStepList" :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.icEvaluationControlStepList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :key="key"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ]
                            .concat(Array.from(fields.icEvaluationControlStepList.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.icEvaluationControlStepList.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.icEvaluationControlStepList, 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="icEvaluationControlSampleList" :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.icEvaluationControlSampleList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.icEvaluationControlSampleList.attr.dync))
                        "
                        :columns="infocolumns"
                        :flagpage="true"
                      >
                        <! -- <template v-slot:contactsOpt="{ field, text, index, value }">
                          {{ value[index].contactsName }}
                        </template> - ->
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table> -->
      </template>
    </sd-webflow>
  </div>
</template>

<script>
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-control-form-import'

export default {
  name: 'IcEvaluationWorkcontrolForm',
  metaInfo: {
    title: '控制措施底稿',
  },
  components: {
    ...components,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      Expand: true,
      stepExpand: true,
      infoExpand: 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: 'icEvaluationControlSampleList', 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',
        },
      ],
      orgId: null,
      versionId: null,
      flag: true,
      simpleFlag: [],
      key: 0,
    }
  },
  methods: {
    simpleInfo(model, index) {
      const stepInfo = this.$refs.flow.getFieldValue('icEvaluationControlStepList')
      IamLightService.simpleInfo(model, stepInfo[index]).then((info) => {
        if (info.length) {
          const cloneData = JSON.parse(JSON.stringify(stepInfo))
          cloneData[index].icEvaluationControlSampleList = JSON.stringify(info)
          this.$refs.flow.setFieldValue('icEvaluationControlStepList', cloneData)
          this.key++
        }
      })
    },
    onChange(val, index) {
      if (val === '01') {
        this.simpleFlag[index] = index
      } else {
        this.simpleFlag[index] = null
      }
      console.log(this.simpleFlag[index])
      this.key++
    },
    initData(model) {
      return false
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'save') {
      }
    },
    // 保存操作，记录标识位
    saveproject() {
      this.isSave = true
    },
    /**
     * 提交后事件
     */
    afterDispatch() {
      // 提交也认为是保存
      this.isSave = true
    },
  },
}
</script>

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