<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()">
            <!-- ID -->
            <!-- <sd-form-item-td name="id" /> -->
            <!-- 随机ID -->
            <sd-form-item-td name="sourceId" />
          </tr>
          <tr>
            <!-- 评价名称 -->
            <sd-form-item-td name="evaluationName">
              <template>{{ model.evaluationName }}</template>
            </sd-form-item-td>
            <!-- 年度 -->
            <sd-form-item-td name="year">
              <template>{{ model.year }}</template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 检查单位 -->
            <sd-form-item-td name="inspectUnitOpt">
              <template>{{ model.inspectUnitName }}</template>
            </sd-form-item-td>
            <!-- 被检查单位 -->
            <sd-form-item-td name="evaluationUnitOpt">
              <template>{{ model.evaluationUnitName }}</template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 创建人员 -->
            <sd-form-item-td name="creatorName" />
            <!-- 创建日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="icEvaluationInventoryList" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="confirmExpand"
                    :expand-str="'confirmExpand'"
                    :group-label="'缺陷清单'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        ref="confirmData"
                        v-model="model.icEvaluationInventoryList"
                        :class="$style.deptPlanChild"
                        :label="null"
                        :read-only="true"
                        :addbuttonvisiable="false"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.icEvaluationInventoryList.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 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-evaluation-work-confirm-form-import'

export default {
  name: 'IcEvaluationWorkConfrimForm',
  metaInfo: {
    title: '缺陷确认',
  },
  components: {
    ...components,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      isSave: false,
      Expand: true,
      confirmExpand: true,
      datalist: [],
      planId: '-1',
      infocolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          dataIndex: 'prcName',
          title: '流程名称',
        },
        {
          dataIndex: 'measureName',
          title: '控制点名称',
        },
        {
          dataIndex: 'measureCode',
          title: '控制点编号',
        },
        {
          dataIndex: 'testResult',
          title: '测试结果',
        },
        {
          dataIndex: 'defectType',
          title: '缺陷类型',
        },
        {
          dataIndex: 'defectLevel',
          title: '缺陷等级',
        },
        {
          dataIndex: 'defectDesc',
          title: '缺陷描述',
        },
        {
          dataIndex: 'testUserName',
          title: '测试人',
        },
        { dataIndex: 'testUserOpt', sdHidden: true },
      ],
    }
  },
  created() {
    // 获取计划ID
    let cids = []
    let crossid = []
    if (this.$route.query.extParams) {
      this.planId = JSON.parse(this.$route.query.extParams).planId
      cids = JSON.parse(this.$route.query.extParams)?.cids
      crossid = JSON.parse(this.$route.query.extParams)?.crossid
    }
    if ((cids && cids.length > 0) || (crossid && crossid.length > 0)) {
      // 选择清单弹出
      IcEvaluationService.getQdidsList(cids, crossid).then((res) => {
        this.datalist = res.data.data
      })
    } else {
      // 获取所有缺陷列表
      const params = {
        columns:
          'sortNumber,id,prcName,measureName,measureCode,testResult,defectType,defectLevel,defectDesc,testUserName',
        expressions: [
          { dataType: 'long', name: 'planId', op: 'eq', longValue: parseInt(this.planId) },
        ],
        formId: 'icEvaluationInventory',
        maxResults: -1,
        startPosition: 0,
      }
      IcEvaluationService.getList(params).then((res) => {
        this.datalist = res.data.data
      })
    }
  },
  methods: {
    initData() {
      if (this.planId !== '-1') {
        this.$refs.flow?.setFieldValue('sourceId', parseInt(this.planId))
        axios({
          url: `api/framework/v1/page/wp/ic/evaluation/plan/icEvaluationPlan?id=` + this.planId,
        }).then((res) => {
          const name = res.data.pageFormData.pageFieldInfos.findIndex(
            (i) => i.name === 'evaluationName'
          )
          const year = res.data.pageFormData.pageFieldInfos.findIndex((i) => i.name === 'year')
          const inspectUnitOpt = res.data.pageFormData.pageFieldInfos.findIndex(
            (i) => i.name === 'inspectUnitOpt'
          )
          const evaluationUnitOpt = res.data.pageFormData.pageFieldInfos.findIndex(
            (i) => i.name === 'evaluationUnitOpt'
          )
          const inspectUnitName = res.data.pageFormData.pageFieldInfos.findIndex(
            (i) => i.name === 'inspectUnitName'
          )
          const evaluationUnitName = res.data.pageFormData.pageFieldInfos.findIndex(
            (i) => i.name === 'evaluationUnitName'
          )
          this.$refs.flow?.setFieldValue(
            'inspectUnitName',
            res.data.pageFormData.pageFieldInfos[inspectUnitName].value
          )
          this.$refs.flow?.setFieldValue(
            'evaluationUnitName',
            res.data.pageFormData.pageFieldInfos[evaluationUnitName].value
          )
          this.$refs.flow?.setFieldValue(
            'evaluationName',
            res.data.pageFormData.pageFieldInfos[name].value
          )
          this.$refs.flow?.setFieldValue('year', res.data.pageFormData.pageFieldInfos[year].value)
          this.$refs.flow?.setFieldValue(
            'inspectUnitOpt',
            JSON.parse(res.data.pageFormData.pageFieldInfos[inspectUnitOpt].value)
          )
          this.$refs.flow?.setFieldValue(
            'evaluationUnitOpt',
            JSON.parse(res.data.pageFormData.pageFieldInfos[evaluationUnitOpt].value)
          )
        })
        const data = []
        this.datalist.forEach((r, index) => {
          const d = {
            sortNum: index + 1,
            prcName: r.prcName,
            measureName: r.measureName,
            measureCode: r.measureCode,
            testResult: r.testResultstr,
            defectType: r.defectTypestr,
            defectLevel: r.defectLevelstr,
            defectDesc: r.defectDesc,
            testUserName: r.testUserName,
          }
          data.push(d)
        })
        if (data.length > 0) {
          this.$refs.flow?.setFieldValue('icEvaluationInventoryList', data)
        }
      }
      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>
