<template>
  <div>
    <sd-webflow ref="flow" :removed-tabs="['sdRelatedDoc']" :class="$style.mainForm">
      <template v-slot:form="{ model, fields }">
        <table>
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="planExpand"
                expand-str="planExpand"
                group-label="基本信息"
                tablestyle=""
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="planExpand">
          <tr>
            <!-- 整改名称 -->
            <sd-form-item-td name="rectName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.rectName }}
              </template>
            </sd-form-item-td>
            <!-- 整改要求编号 -->
            <sd-form-item-td name="rectCode">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.rectCode }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 整改计划反馈截止日期 -->
            <td class="ant-form-item-label ant-form-item-label">
              <label
                title="整改计划反馈截止日期"
                :class="fields.feedbackDate.required ? 'ant-form-item-required' : ''"
              >
                整改计划反馈<br />{{ '截\u2002止\u2003日\u2002期' }}
              </label>
            </td>
            <sd-form-item-td
              name="feedbackDate"
              :colspan="3"
              :label="'整改计划反馈截止日期'"
              :label-td-attrs="{ hidden: true }"
            >
              <template v-slot:read-and-edit="{ editable }">
                {{ model.feedbackDate | sdDateFormat('YYYY-MM-DD') }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="requireAttachment" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <sd-attachment
                  :uploadable="false"
                  read-only
                  :group-id="JSON.parse(fields.requireAttachment.value).value"
                  :init-value="getRequireAttachment(fields.requireAttachment)"
                >
                </sd-attachment>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 整改要求 -->
            <sd-form-item-td name="rectRequire" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.rectRequire }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 创建人 -->
            <sd-form-item-td name="requireCreatorName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.requireCreatorName }}
              </template>
            </sd-form-item-td>
            <!-- 创建日期 -->
            <sd-form-item-td name="requireCreationTime">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.requireCreationTime | sdDateFormat('YYYY-MM-DD') }}
              </template>
            </sd-form-item-td>
          </tr>
        </table>

        <table>
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="defectExpand"
                expand-str="defectExpand"
                group-label="缺陷信息"
                tablestyle=""
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="defectExpand">
          <tr>
            <!-- 评价名称 -->
            <sd-form-item-td name="evaluationName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.evaluationName }}
              </template>
            </sd-form-item-td>
            <!-- 流程名称 -->
            <sd-form-item-td name="prcName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.prcName }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 控制点名称 -->
            <sd-form-item-td name="measureName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.measureName }}
              </template>
            </sd-form-item-td>
            <!-- 控制点编号 -->
            <sd-form-item-td name="measureCode">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.measureCode }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 测试结果 -->
            <sd-form-item-td name="testResult">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.testResult }}
              </template>
            </sd-form-item-td>
            <!-- 缺陷类型 -->
            <sd-form-item-td name="defectType">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.defectType }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 缺陷等级 -->
            <sd-form-item-td name="defectLevel">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.defectLevel }}
              </template>
            </sd-form-item-td>
            <!-- 缺陷责任组织 -->
            <sd-form-item-td name="liabilityOrgName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.liabilityOrgName }}
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 整改联系人 -->
            <sd-form-item-td name="rectContactName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.rectContactName }}
              </template>
            </sd-form-item-td>
            <!-- 整改跟踪人 -->
            <sd-form-item-td name="rectTrackerName">
              <template v-slot:read-and-edit="{ editable }">
                {{ model.rectTrackerName }}
              </template>
            </sd-form-item-td>
          </tr>
        </table>

        <table>
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="defectRectifyExpand"
                expand-str="defectRectifyExpand"
                group-label="缺陷整改计划"
                tablestyle=""
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="defectRectifyExpand">
          <tr>
            <!-- 整改责任部门 -->
            <sd-form-item-td name="responsibleDeptName" />
            <!-- 直接责任人 -->
            <sd-form-item-td name="directResponsibleName" />
          </tr>
          <tr>
            <!-- 拟整改完成日期 -->
            <sd-form-item-td name="finishDate" :colspan="3" />
          </tr>
          <tr>
            <!-- 拟整改措施 -->
            <sd-form-item-td name="measure" :colspan="3">
              <a-textarea v-model="model.measure" :auto-size="{ maxRows: 3, minRows: 3 }" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 说明 -->
            <sd-form-item-td name="evidenceExplain" :colspan="3">
              <a-textarea v-model="model.evidenceExplain" :auto-size="{ maxRows: 3, minRows: 3 }" />
            </sd-form-item-td>
          </tr>
        </table>

        <table v-if="!isPlanStat">
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="defectRectifyEndExpand"
                expand-str="defectRectifyEndExpand"
                group-label="缺陷整改结果"
                tablestyle=""
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-if="!isPlanStat" v-show="defectRectifyEndExpand">
          <tr>
            <!-- 整改状态 -->
            <sd-form-item-td
              name="rectStatus"
              :colspan="model.rectStatus === '03' ? 1 : 3"
              :input-td-attrs="{
                class: model.rectStatus !== '03' ? $style.rectStatusTd : '',
              }"
              @change="rectStatusChange"
            />
            <!-- 整改完成日期 -->
            <sd-form-item-td name="rectFinishDate" :hidden="model.rectStatus !== '03'" />
          </tr>
          <tr>
            <!-- 整改情况说明 -->
            <sd-form-item-td name="description" :colspan="3">
              <a-textarea v-model="model.description" :auto-size="{ maxRows: 3, minRows: 3 }" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 成果利用 -->
            <sd-form-item-td name="resultUse" :colspan="3" component="a-select"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3"> </sd-form-item-td>
          </tr>
        </table>

        <table v-if="!isPlanStat">
          <tr
            ><td :colspan="4">
              <sd-form-item name="icPreRectResultEntity" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="listExpand"
                    expand-str="listExpand"
                    group-label="历次整改结果列表"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        :key="infoKey"
                        ref="infoData"
                        v-model="model.icPreRectResultEntity"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="true"
                        :addbuttonvisiable="false"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.icPreRectResultEntity.attr.dync))
                        "
                        :columns="defectColumns"
                        :flagpage="true"
                      >
                        <!-- 整改状态 -->
                        <template v-slot:rectStatus_xm="{ text, record, field, value }">
                          {{ getDictValue(fields.rectStatus.attr.selectListItem, text) }}
                        </template>
                        <!-- 整改完成日期 -->
                        <template v-slot:rectFinishDate_xm="{ text, record, field, value }">
                          {{ text | sdDateFormat('YYYY-MM-DD') }}
                        </template>
                        <!-- 整改情况说明 -->
                        <template v-slot:description_xm="{ text, record, field, value }">
                          {{ text }}
                        </template>
                        <!-- 成果利用 -->
                        <template v-slot:resultUse_xm="{ text, record, field, value }">
                          {{ getDictValue(fields.resultUse.attr.selectListItem, text) }}
                        </template>
                        <!-- 附件 -->
                        <template v-slot:attachment_xm="{ text, record, field, value }">
                          <sd-attachment :uploadable="false" read-only :group-id="text">
                          </sd-attachment>
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table>
      </template>
    </sd-webflow>
  </div>
</template>

<script>
import auditAdvancedGroup from '@product/iam/components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '@product/iam/components/audit-advanced-group-mixins'
import iamIcRectifyFeedbackServices from './iam-ic-rectify-feedback-service'
import components from './_import-components/iam-ic-rectify-feedback-form-import'
export default {
  name: 'IamIcRectifyFeedbackForm',
  metaInfo: {
    title: '整改反馈',
  },
  components: {
    ...components,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      isPlanStat: true, // 是否为计划审核环节
      requireAttachmentKey: 0,
      userType: null,
      infoKey: 0,
      rectVisible: false, // 缺陷清单列表显示
      planExpand: true,
      defectExpand: true,
      defectRectifyExpand: true,
      defectRectifyEndExpand: true,
      listExpand: true,
      defectColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          dataIndex: 'rectStatus',
          title: '整改状态',
          width: '150px',
        },
        { dataIndex: 'rectFinishDate', title: '整改完成日期', width: '150px' },
        { dataIndex: 'description', title: '整改情况说明', width: '40%' },
        { dataIndex: 'resultUse', title: '成果利用' },
        { dataIndex: 'attachment', title: '附件' },
        { dataIndex: 'rectFeedbackId', title: 'ID', sdHidden: true },
      ],
    }
  },
  mounted() {
    // 判断当前环节是否为计划审核环节
    this.fnCheckIsPlanStat()
  },
  methods: {
    // 判断是否为计划环节
    fnCheckIsPlanStat() {
      const checkPlanStat = setInterval(() => {
        if (this.$refs.flow) {
          clearInterval(checkPlanStat)
          if (this.$refs.flow.FlowData.attrs.audit$isPlanStat) {
            this.isPlanStat = this.$refs.flow.FlowData.attrs.audit$isPlanStat === true
          } else {
            iamIcRectifyFeedbackServices
              .getSetpInfo(this.$refs.flow.FlowData.instId, this.$refs.flow.FlowData.activeStepId)
              .then((res) => {
                this.isPlanStat = res.data.audit$isPlanStat === 'true'
              })
          }
        }
      }, 100)
    },
    // 获取附件
    getRequireAttachment(attachment) {
      // 获取附件列表
      const attList = attachment.attr.attachments
      return attList.map((item) => {
        item.readOnly = false
        item.attr.deletable = false
        item.attr.editable = false
        item.attr.renamable = false
        return item
      })
    },
    // 整改状态变化时触发
    rectStatusChange(val, option) {
      if (val === '03') {
        this.$refs.flow.FlowData.processFormData.processFormPropertyValues.find((item) => {
          return item.name === 'rectFinishDate'
        }).required = true
      } else {
        this.$refs.flow.FlowData.processFormData.processFormPropertyValues.find((item) => {
          return item.name === 'rectFinishDate'
        }).required = false
        // 不为必填时，清空整改完成时间
        this.$refs.flow.setFieldValue('rectFinishDate', null)
      }
    },
    // 获取字典值
    getDictValue(list, val) {
      return list.find((item) => {
        return item.value.trim() === (val + '').trim()
      })?.label
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.main-form {
  :global(.header_xm-child-table_product) {
    display: none;
  }
  :global(.ant-calendar-picker) {
    width: 100%;
  }
  .rect-status-td {
    border-right: 0 !important;
  }
}
</style>
