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

      <table v-show="basicExpand">
        <colgroup>
          <col style="width: 20%;"/>
          <col style="width: 30%;"/>
          <col style="width: 20%;"/>
          <col style="width: 30%;"
        /></colgroup>

        <tr v-show="false">
          <sd-form-item-td name="randomId" />
          <sd-form-item-td name="id" />
          <sd-form-item-td name="projectId" :input-props="{ defaultValue: projectId }" />
        </tr>
        <tr v-show="false">
          <sd-form-item-td
            name="frameClassId"
            :input-props="{
              defaultValue: catalogId,
            }"
          />
        </tr>
        <tr>
          <!-- 底稿名称 -->
          <sd-form-item-td name="paperTitle" :colspan="3" />
        </tr>

        <tr>
          <!-- 底稿编号 -->
          <sd-form-item-td name="paperCode">
            <span>{{ model.paperCode }}</span>
          </sd-form-item-td>
          <!-- 被审计单位 -->
          <sd-form-item-td name="auditedUnit" :input-props="{ defaultValue: units }" />
        </tr>

        <tr>
          <!-- 审计详细情况 -->
          <sd-form-item-td name="auditedSituation" :colspan="3">
            <template v-slot:read-and-edit="{ editable }">
              <div v-if="!editable" v-html="model.auditedSituation"></div>
              <sd-quill-editor
                v-else
                v-model="model.auditedSituation"
                :options="editorOption"
                :class="$style.editor"
              />
            </template>
          </sd-form-item-td>
        </tr>
        <tr>
          <!-- 审计结论 -->
          <sd-form-item-td name="conclusion" :colspan="3">
            <a-textarea v-model="model.conclusion" :rows="3" />
          </sd-form-item-td>
        </tr>
        <tr>
          <!-- 编制人员 -->
          <sd-form-item-td name="creatorName" />
          <!-- 编制日期 -->
          <sd-form-item-td name="creationTime" />
        </tr>
        <tr>
          <!-- 审核人员 -->
          <sd-form-item-td name="auditorUser" />
          <!-- 审核日期 -->
          <sd-form-item-td v-if="initparams(model, FlowData)" name="checkDate" />
        </tr> </table

      ><table>
        <tr>
          <td :colspan="8">
            <sd-form-item :label="null" name="iamWorkPaperProgrameEntityList">
              <template v-slot:read-and-edit="{ editable }">
                <audit-advanced-group
                  :expand="programeExpand"
                  :expand-str="'programeExpand'"
                  :group-label="'审计程序列表'"
                  tablestyle="''"
                  @changedClick="changedClick"
                  ><template>
                    <xm-child-table
                      v-show="programeExpand"
                      ref="programeData"
                      v-model="model.iamWorkPaperProgrameEntityList"
                      :class="$style.deptPlanChild"
                      :label="null"
                      :read-only="!editable"
                      :columns="programecolumns"
                      :colspan="8"
                      :addfun="fnaddprograme"
                      :changeflag = true
                      name="iamWorkPaperProgrameEntityList"
                      :fields="
                        [
                          {
                            caption: '序号',
                            name: 'sortNum',
                            dataType: 'sortNum',
                          },
                        ].concat(
                          Array.from(fields.iamWorkPaperProgrameEntityList.attr.dync).slice(1, 7)
                        )
                      "
                      :input-props="programecolumns"
                      @change="changetable"
                    >
                      <!-- input-form 表示这部分是子表组件的form插槽 -->
                      <template v-slot:form="{ model }">
                        <table>
                          <tr><sd-form-item-td name="sortNum" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="id" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="projectId" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="paperId" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="stepName"/></tr>
                          <tr><sd-form-item-td name="importance"/></tr>
                          <tr><sd-form-item-td name="isExecited"/></tr>
                          <tr
                            ><sd-form-item-td name="execitedExplain">
                              <a-textarea
                                v-model="model.execitedExplain"
                                :rows="3"
                              /> </sd-form-item-td
                          ></tr>
                          <tr><sd-form-item-td name="attachment"/></tr>
                        </table>
                      </template>
                    </xm-child-table> </template
                ></audit-advanced-group>
              </template>
            </sd-form-item>
          </td>
        </tr>
        <tr
          ><td :colspan="8">
            <sd-form-item :label="null" name="iamAuditFindEntityList">
              <template v-slot:read-and-edit="{ editable }">
                <audit-advanced-group
                  :expand="findExpand"
                  :expand-str="'findExpand'"
                  :group-label="'审计发现列表'"
                  tablestyle="''"
                  @changedClick="changedClick"
                  ><template>
                    <xm-paper-child-table
                      v-show="findExpand"
                      ref="findData"
                      v-model="iamAuditFindEntityList"
                      :class="$style.deptPlanChild"
                      :label="null"
                      :colspan="8"
                      :read-only="!editable"
                      name="iamAuditFindEntityList"
                      :fields="Array.from(fields.iamAuditFindEntityList.attr.dync).slice(0, 4)"
                      :input-props="findcolumns"
                      :columns="findcolumns"
                      :creatnewfind="creatnewfind"
                      :deletefind="deletefind"
                      :findtable="true"
                      :captionvisiable="true"
                    >
                      <a
                        slot="findTitle"
                        slot-scope="text, record"
                        @click="linkFind(text.record, editable)"
                        >{{ text.text }}</a
                      >
                    </xm-paper-child-table>
                  </template>
                </audit-advanced-group>
              </template>
            </sd-form-item></td
          >
        </tr>
        <tr
          ><td :colspan="8">
            <sd-form-item :label="null" name="iamWorkPaperEvidenceEntityList">
              <template v-slot:read-and-edit="{ editable }">
                <audit-advanced-group
                  :expand="evidenceExpand"
                  :expand-str="'evidenceExpand'"
                  :group-label="'审计取证单列表'"
                  tablestyle="''"
                  @changedClick="changedClick"
                  ><template>
                    <xm-paper-child-table
                      v-show="evidenceExpand"
                      ref="evidenceData"
                      v-model="model.iamWorkPaperEvidenceEntityList"
                      :class="$style.deptPlanChild"
                      :label="null"
                      :read-only="!editable"
                      :colspan="8"
                      name="iamWorkPaperEvidenceEntityList"
                      :columns="evidencecolumns"
                      :fields="
                        Array.from(fields.iamWorkPaperEvidenceEntityList.attr.dync).slice(0, 5)
                      "
                      :input-props="evidencecolumns"
                      :createvidence="showevidenceTable"
                      :evidencetable="true"
                    >
                    </xm-paper-child-table></template
                ></audit-advanced-group>
              </template> </sd-form-item
          ></td>
        </tr>
        <tr
          ><td :colspan="8">
            <sd-form-item :label="null" name="iamWorkPaperMaterialEntityList">
              <template v-slot:read-and-edit="{ editable }">
                <audit-advanced-group
                  :expand="materialExpand"
                  :expand-str="'materialExpand'"
                  :group-label="'审计资料列表'"
                  tablestyle="''"
                  @changedClick="changedClick"
                  ><template>
                    <template>
                      <div :class="$style.wrapper">
                        <div v-show="materialExpand && editable" :class="$style.btnselect">
                          <a-button type="link" :class="$style.batchselect" @click="showmaterial"
                            ><a-icon type="check-circle" :theme="'filled'" />选择
                          </a-button>
                        </div>
                      </div>
                    </template>
                    <xm-child-table
                      v-show="materialExpand"
                      ref="materialData"
                      v-model="model.iamWorkPaperMaterialEntityList"
                      :modal-width="'800px'"
                      :class="$style.deptPlanChild"
                      :label="null"
                      :colspan="8"
                      :columns="materialcolumns"
                      :read-only="!editable"
                      name="iamWorkPaperMaterialEntityList"
                      :fields="
                        [
                          {
                            caption: '序号',
                            name: 'sortNum',
                            dataType: 'sortNum',
                          },
                        ].concat(
                          Array.from(fields.iamWorkPaperMaterialEntityList.attr.dync).slice(1, 8)
                        )
                      "
                      ><template v-slot:materialCode="{ text }">
                        <span>{{ text }}</span>
                      </template>
                      <template v-slot:attachment="{ field, text, index, value }">
                        <sd-attachment
                          :group-id="JSON.parse(value[index][field.name]).value"
                          :read-only="!editable"
                          @change="fnchangename1"
                          @input="fnclickattach(index)"
                        />
                      </template>
                      <!-- input-form 表示这部分是子表组件的form插槽 -->
                      <template v-slot:form="{ model }">
                        <table class="operatepaperchildform">
                          <tr><sd-form-item-td name="projectId" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="paperId" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="sortNum" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="id" :hidden="true"/></tr>
                          <tr><sd-form-item-td name="materialName" :max="50"/></tr>
                          <tr>
                            <sd-form-item-td name="materialCode">
                              <span>{{ model.materialCode }}</span>
                            </sd-form-item-td>
                          </tr>
                          <tr
                            ><sd-form-item-td name="materialDesc">
                              <a-textarea v-model="model.materialDesc" :rows="3" />
                            </sd-form-item-td>
                          </tr>
                          <tr> <sd-form-item-td name="attachment" @change="fnchangename"/></tr>
                        </table>
                      </template>
                    </xm-child-table>
                  </template>
                </audit-advanced-group>
              </template> </sd-form-item
          ></td>
        </tr>
      </table>

      <!-- 选择导入模板 -->
      <a-modal
        :visible="evidencevisible"
        title="请选择"
        width="1200px"
        :body-style="{
          minHeight: '700px',
        }"
        :destroy-on-close="true"
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <sd-data-table-ex
          ref="evidenceTable"
          class="evidencechildtable"
          check-type="checkbox"
          :show-selection="showSelection"
          :filter-expressions="evidenceexpressions"
          :columns="evidencemodalcolumns"
          form-id="iamAuditEvidenceSheet"
          page-id="audit/evidence/iamAuditEvidenceSheet"
          :search-fields="['itemTitle', 'itemCode', 'auditedUnitNames', 'creatorName', 'docStatus']"
        >
        </sd-data-table-ex>
      </a-modal>
      <a-modal
        :visible="materialvisible"
        title="请选择"
        width="1200px"
        :body-style="{
          minHeight: '700px',
        }"
        :destroy-on-close="true"
        @ok="handlematerialOk"
        @cancel="handlematerialCancel"
      >
        <sd-data-table-ex
          ref="materialTable"
          check-type="checkbox"
          :show-selection="showSelection"
          :filter-expressions="materialexpressions"
          :columns="materialmodalcolumns"
          form-id="iamAuditMaterial"
          page-id="audit/preparation/material/iamAuditMaterial"
          data-url="api/xcoa-mobile/v1/iam-material/material-list"
          :search-fields="['materialTitle']"
        >
          <div slot="attachment" slot-scope="text, record">
            <sd-attachment
              :group-id="JSON.parse(text).value ? JSON.parse(text).value : text"
              read-only
            />
          </div>
        </sd-data-table-ex>
      </a-modal>
    </template>
  </sd-webflow>
</template>

<script>
import { xmLocalStorage } from '@/common/services/storage-service'
import moment from 'moment'
import { Modal, Message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import contacts from '@/common/components/sd-icon-library/icons/outline/contacts'
import auditAdvancedGroup from '../../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../../components/audit-advanced-group-mixins'
import xmPaperChildTable from './table/xm-paper-child-table.vue'
import xmPaperChildTableMixins from './table/xm-paper-child-table-mixins'
import AuditOperatePaperService from './audit-operate-paper-service'
import components from './_import-components/iam-operate-paper-form-import'
const programecolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '步骤名称',
    dataIndex: 'stepName',
    width: '35%',
  },
  {
    title: '重要性',
    dataIndex: 'importance',
    width: '100px',
  },
  {
    title: '是否执行',
    dataIndex: 'isExecited',
    width: '120px',
  },
  {
    title: '执行说明',
    dataIndex: 'execitedExplain',
  },
  {
    title: '附件',
    dataIndex: 'attachment',
  },
]
const findcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px !important',
  },
  {
    title: '审计发现标题',
    dataIndex: 'findTitle',
    width: '45%',
  },
  {
    title: '审计发现编号',
    dataIndex: 'findCode',
    width: '200px',
  },
  {
    title: '对应审计步骤',
    dataIndex: 'stepName',
  },
  {
    title: 'id',
    dataIndex: 'id',
  },
]
const evidencecolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '取证单名称',
    dataIndex: 'materialName',
  },
  {
    title: '取证单编号',
    dataIndex: 'materialCode',
    width: '200px',
  },
  {
    title: '附件',
    dataIndex: 'attachment',
  },
]
const evidencemodalcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '取证单名称',
    dataIndex: 'itemTitle',
    width: '60%',
  },
  {
    title: '取证单编号',
    dataIndex: 'itemCode',
  },
  {
    title: '附件',
    dataIndex: 'attachment',
    sdHidden: true,
  },
]
const materialcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '资料名称',
    dataIndex: 'materialName',
  },
  {
    title: '资料编号',
    dataIndex: 'materialCode',
    width: '200px',
  },
  {
    title: '资料描述',
    dataIndex: 'materialDesc',
  },
  {
    title: '附件',
    dataIndex: 'attachment',
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    dataIndex: 'paperId',
    sdHidden: true,
  },
  {
    dataIndex: 'projectId',
    sdHidden: true,
  },
]
const materialmodalcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '资料名称',
    dataIndex: 'materialTitle',
  },
  {
    title: '资料编号',
    dataIndex: 'materialCode',
    width: '200px',
  },
  {
    title: '资料描述',
    dataIndex: 'materialDesc',
  },
  {
    title: '附件',
    dataIndex: 'attachment',
    scopedSlots: { customRender: 'attachment' },
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    dataIndex: 'projectId',
    sdHidden: true,
  },
]
export default {
  name: 'IamOperatePaperForm',
  metaInfo: {
    title: '工作底稿',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    xmPaperChildTable,
  },
  mixins: [auditAdvancedGroupMixins, xmPaperChildTableMixins],
  data() {
    return {
      isSave: false, // 是否有保存操作
      programecolumns,
      findcolumns,
      evidencecolumns,
      evidencemodalcolumns,
      materialcolumns,
      materialmodalcolumns,
      basicExpand: true,
      programeExpand: true,
      findExpand: true,
      evidenceExpand: true,
      materialExpand: true,
      projectId: '',
      paperid: '',
      catalogId: 0,
      projectCode: '',
      editorOption: {
        placeholder: '请从此处开始输入文本...',
      },
      iamAuditFindEntityList: [],
      iamWorkPaperEvidenceEntityList: [],
      expressions: [],
      materialexpressions: [],
      evidenceexpressions: [],
      evidencevisible: false,
      selectlist: [],
      materialvisible: false,
      selectedKeys: ['1'],
      materialCode: '',
      units: [],
      flag: true,
      childindex: 0,
      curuser: null,
    }
  },
  computed: {
    // 计算随机key
    randomId() {
      let randomNum = Math.random()
      while (randomNum === 0) {
        randomNum = Math.random()
      }
      const time = new Date().getTime()
      return time + Math.ceil(randomNum * 1000000000)
    },
  },
  created() {
    // 监听关闭事件，关闭时如果没有保存操作且没有id，则直接删除所有子表
    window.addEventListener('beforeunload', (e) => {
      this.beforeunloadHandler(e)
    })
    // 获取审计发现
    if (this.$route.query.extParams) {
      this.catalogId = parseInt(JSON.parse(this.$route.query.extParams).catalogId)
      this.projectId = JSON.parse(this.$route.query.extParams).projectId
      this.materialexpressions.push({
        dataType: 'long',
        name: 'projectId',
        op: 'eq',
        longValue: this.projectId,
      })
      this.materialexpressions.push({
        dataType: 'str',
        name: 'docStatus',
        op: 'in',
        stringValue: '已结束,已反馈',
      })
      this.evidenceexpressions.push({
        dataType: 'long',
        name: 'projectId',
        op: 'eq',
        longValue: this.projectId,
      })
      this.evidenceexpressions.push({
        dataType: 'str',
        name: 'docStatus',
        op: 'like',
        stringValue: `%已结束%`,
      })
    }
  },
  methods: {
    beforeunloadHandler(e) {
      if (!this.isSave && !this.$refs.flow.getFieldValue('id')) {
        const strid = this.iamAuditFindEntityList.map((item) => {
          return item.id
        })
        if (strid.length > 0) {
          axios({
            url: 'api/xcoa-mobile/v1/page/iamAuditDiscover?ids=' + strid,
            method: 'delete',
          })
        }
      }
    },
    showSelection(record) {
      if (
        this.selectlist.findIndex((i) => i === record.itemCode) === -1 &&
        this.selectlist.findIndex((i) => i === record.materialCode) === -1
      ) {
        return true
      } else {
        return false
      }
    },
    saveproject(formdata) {
      this.isSave = true
      const selectedKeys = formdata.processFormData.processFormPropertyValues
      selectedKeys.forEach((item) => {
        if (item.name === 'paperCode') {
          this.$refs.flow.setFieldValue('paperCode', item.value)
        } else if (item.name === 'iamWorkPaperMaterialEntityList') {
          // this.$refs.flow.setFieldValue('iamWorkPaperMaterialEntityList', JSON.parse(item.value))
        }
      })
    },
    initparams(model, FlowData) {
      if (this.flag) {
        if (model.id !== undefined) {
          this.paperid = model.id
        } else {
          // 如果是新建，则直接传随机id
          this.paperid = this.randomId
        }
        if (this.projectId === '') {
          this.projectId = model.projectId
          this.materialexpressions.push({
            dataType: 'long',
            name: 'projectId',
            op: 'eq',
            longValue: this.projectId,
          })
          this.materialexpressions.push({
            dataType: 'str',
            name: 'docStatus',
            op: 'in',
            stringValue: '已结束,已反馈',
          })
          this.evidenceexpressions.push({
            dataType: 'long',
            name: 'projectId',
            op: 'eq',
            longValue: this.projectId,
          })
          this.evidenceexpressions.push({
            dataType: 'str',
            name: 'docStatus',
            op: 'like',
            stringValue: `%已结束%`,
          })
        }
        // 获取取整列表 获取被审计单位
        if (model.auditedUnit === undefined) this.fngetunit()
        this.getPaperFind(this.projectId, this.paperid)
        if (FlowData.attrs.stepProps) {
          if (FlowData.attrs.stepProps.auditor) {
            if (FlowData.attrs.stepProps.auditor === 'true') {
              if (model.auditorUser.length === 0) {
                const user = getUserInfo()
                this.curuser = [
                  {
                    type: 'User',
                    code: user.account,
                    name: user.name,
                    attrs: user.attrs,
                  },
                ]
                setTimeout(() => {
                  this.$refs.flow.setFieldValue('auditorUser', this.curuser)
                  this.$refs.flow.setFieldValue('checkDate', moment(new Date()))
                }, 1000)
              }
            }
          }
        }
        this.flag = false
      }
      this.$refs.flow?.setFieldValue('randomId', this.randomId)
      return true
    },
    fngetunit() {
      axios({
        url: 'api/xcoa-mobile/v1/page/wp/audit/project/iamAuditProject?id=' + this.projectId,
        method: 'get',
      }).then((res) => {
        const obj = res.data.pageFormData.pageFieldInfos.find(
          (item) => item.name === 'auditedUnitIds'
        )
        if (obj !== null) {
          if (obj.value !== undefined) {
            const units = JSON.parse(obj.value)
            const newunits = []
            if (units.length === 1) {
              units.forEach((item) => {
                item.id = item.code
              })
              this.units = units
              setTimeout(() => {
                this.$refs.flow.setFieldValue('auditedUnit', this.units)
              }, 1000)
            }
          }
        }
      })
    },
    fngetid(obj) {
      return obj.find((item) => item.name === 'id').value
    }, // 展示选择资料列表
    showmaterial() {
      this.selectlist = []
      let list = []
      if (this.$refs.flow.getFieldValue('iamWorkPaperMaterialEntityList')) {
        list = this.$refs.flow.getFieldValue('iamWorkPaperMaterialEntityList')
      }
      list.forEach((l) => {
        if (l.materialCode) {
          this.selectlist.push(l.materialCode)
        }
      })
      this.materialvisible = !this.materialvisible
    },
    // 弹出窗确认
    handlematerialOk() {
      this.showmaterial()
      // 获取别选择的行信息
      let list = []
      if (this.$refs.flow.getFieldValue('iamWorkPaperMaterialEntityList')) {
        list = this.$refs.flow.getFieldValue('iamWorkPaperMaterialEntityList')
      }
      const selectInfo = this.$refs.materialTable.getSelectedRows()
      let i = 1
      selectInfo.forEach((item) => {
        const mmc = {}
        mmc.materialName = item.materialTitle
        mmc.sortNum = i
        mmc.materialCode = item.materialCode
        mmc.materialDesc = item.materialDesc ? item.materialDesc : ''
        mmc.projectId = this.projectId
        mmc.paperId = this.paperId
        if (isNaN(item.attachment)) {
          mmc.attachment = item.attachment
        } else {
          mmc.attachment = JSON.stringify({ value: item.attachment })
        }
        i = i + 1
        list.push(mmc)
      })
      this.$refs.flow.setFieldValue('iamWorkPaperMaterialEntityList', list)
    },
    // 弹出窗取消
    handlematerialCancel() {
      this.showmaterial()
    },
    // 展示选择列表
    showevidenceTable() {
      this.selectlist = []
      let list = []
      if (this.$refs.flow.getFieldValue('iamWorkPaperEvidenceEntityList')) {
        list = this.$refs.flow.getFieldValue('iamWorkPaperEvidenceEntityList')
      }
      list.forEach((l) => {
        if (l.materialCode) {
          this.selectlist.push(l.materialCode)
        }
      })
      this.evidencevisible = !this.evidencevisible
    },
    // 弹出窗确认
    handleOk() {
      this.showevidenceTable()
      // 获取别选择的行信息
      let list = []
      if (this.$refs.flow.getFieldValue('iamWorkPaperEvidenceEntityList')) {
        list = this.$refs.flow.getFieldValue('iamWorkPaperEvidenceEntityList')
      }
      const selectInfo = this.$refs.evidenceTable.getSelectedRows()
      let i = 1
      selectInfo.forEach((item) => {
        const mmc = {}
        mmc.materialName = item.itemTitle
        mmc.sortNum = i
        mmc.materialCode = item.itemCode
        mmc.projectId = this.projectId
        mmc.paperId = this.paperId
        if (isNaN(item.attachment)) {
          mmc.attachment = item.attachment
        } else {
          mmc.attachment = JSON.stringify({ value: item.attachment })
        }
        i = i + 1
        list.push(mmc)
      })
      this.$refs.flow.setFieldValue('iamWorkPaperEvidenceEntityList', list)
    },
    // 弹出窗取消
    handleCancel() {
      this.showevidenceTable()
    },
    getPaperFind(projectId, paperid) {
      // 获取取证列表
      const params = { projectId: projectId, paperId: paperid }
      AuditOperatePaperService.getAuditFindData(params).then((res) => {
        res.data.forEach((item, index) => {
          item.materialName = item.itemTitle
          item.materialCode = item.itemCode
          item.attachment = JSON.stringify({ value: item.attachment })
          item.sortNum = index
        })
        this.iamAuditFindEntityList = res.data
      })
    },
    validForm() {
      // 增加校验审计程序
      return true
      // return  this.$refs.programeData.valid().then((res) => {
      //   if (res) {
      //     // 校验审计资料动态子表
      //     return this.$refs.materialData.valid().then((res) => {
      //       if (res) {
      //         return Promise.resolve(true)
      //       } else {
      //         return Promise.resolve(false)
      //       }
      //     })
      //   } else {
      //     return Promise.resolve(false)
      //   }
      // })
    },
    changetable(data) {
      // data.forEach((item, index) => {
      //   item.sortNum = index + 1
      // })
      // 将审计程序写入localStroge
      this.setFindPrograme()
    },
    creatnewfind() {
      let id = this.paperid
      const projectId = this.projectId
      if (this.paperid === '') {
        id = this.fngetid(this.$refs.flow.FlowData.processFormData.processFormPropertyValues)
      }
      // 将审计程序写入localStroge
      this.setFindPrograme()
      const url = '/iam-operate-find-form?projectId=' + this.projectId + '&id=' + id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.getPaperFind(projectId, id)
        }
      })
      // }
    },
    deletefind(ids) {
      const strid = ids.join(',')
      axios({
        url: 'api/xcoa-mobile/v1/page/iamAuditDiscover?ids=' + strid,
        method: 'delete',
      })
    },
    linkFind(record, editable) {
      let url = ''
      // event.stopPropagation()
      if (editable) {
        url = '/iam-operate-find-form?record=' + record.id
      } else {
        url = '/iam-operate-find-detail-form?record=' + record.id
      }
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.getPaperFind(this.projectId, this.paperid)
        }
      })
    },
    stopevent(event) {
      event.stopPropagation()
    },

    fnchangename(value) {
      if (value.length === 1) {
        let name = value[0].name
        if (name.indexOf('.') > 0) {
          name = name.substring(0, name.indexOf('.'))
        }
        this.$refs.materialData.$refs.form.model.materialName = name
      }
    },
    fnclickattach(index) {
      this.childindex = index
    },
    fnchangename1(value) {
      if (value.length === 1) {
        let name = value[0].name
        if (name.indexOf('.') > 0) {
          name = name.substring(0, name.indexOf('.'))
        }
        this.$refs.materialData.$refs[
          'materialName_' + this.childindex
        ][0].model.materialName = name
        this.$refs.materialData.changolddata()
      }
    },
    fnaddprograme(data) {
      data.importance = '01'
      data.isExecited = '01'
      data.stepName = ''
      data.execitedExplain = ''
      return data
    },
    // 将审计程序写入localStroage
    setFindPrograme() {
      // 重要性字典
      const importanceAttr = this.$refs.flow.flowData.processFormData.processFormPropertyValues
        .find((item) => {
          return item.name === 'iamWorkPaperProgrameEntityList'
        })
        .attr.dync.find((item) => {
          return item.name === 'importance'
        }).attr.selectListItem
      // 是否执行字典
      const isExecitedAttr = this.$refs.flow.flowData.processFormData.processFormPropertyValues
        .find((item) => {
          return item.name === 'iamWorkPaperProgrameEntityList'
        })
        .attr.dync.find((item) => {
          return item.name === 'isExecited'
        }).attr.selectListItem
      if (this.$refs.flow.getFieldValue('iamWorkPaperProgrameEntityList')) {
        const list = this.$refs.flow
          .getFieldValue('iamWorkPaperProgrameEntityList')
          .map((item, index) => {
            // if (!item.isExecited) item.isExecited = '02' // 如果传过来没有值，则直接赋值为否
            return {
              key: index + 1,
              stepName: item.stepName ? item.stepName : '',
              importance:
                item.importance &&
                importanceAttr.find((att) => {
                  return item.importance === att.value
                })
                  ? importanceAttr.find((att) => {
                      return item.importance === att.value
                    }).label
                  : importanceAttr[0].label,
              isExecited:
                item.isExecited &&
                isExecitedAttr.find((att) => {
                  return item.isExecited === att.value
                })?.label,
            }
          })
        xmLocalStorage.setItem('findPrograme', JSON.stringify(list))
      } else {
        xmLocalStorage.setItem('findPrograme', '[]')
      }
    },
    /**
     * 提交后事件
     */
    afterDispatch() {
      // 提交也认为是保存
      this.isSave = true
    },
  },
}
</script>

<style module lang="scss">
@use '@/webflow/sd-flow-form.scss';
.editor {
  z-index: 1;
  margin: 0 auto;
}
.btnselect {
  position: relative;
  float: right;
  top: 4px;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
.btnmubandiv {
  position: relative;
  float: right;
  .btnmuban {
    z-index: 100;
    margin-left: 10px;
  }
}
.btnselect {
  position: relative;
  float: right;
  .batchselect {
    z-index: 100;
    margin-right: 190px;
  }
}
:global(.operatepaperchildform) {
  :global(.ant-form-item-label) {
    width: 150px;
  }
}

.loading {
  position: fixed;
  top: 310px;
  left: 50%;
  text-align: center;
}
:global(.evidencechildtable) {
  :global(.header_sd-data-table_common) {
    padding-right: 20px;
  }
}
</style>
