<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :form-data="mode"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/work/reportFind/iamAuditReportFind"
        :record-id="recodeId"
        :class="$style.form"
        :read-only="readOnly"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="
          () => {
            this.sdFormReady()
          }
        "
      >
        <template v-slot="{ model, fields }">
          <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: 220px;"/>
              <col />
              <col style="width: 220px;"/>
              <col
            /></colgroup>
            <tr v-show="initData(model)">
              <!-- projectId -->
              <sd-form-item-td name="projectId" :colspan="3" />
            </tr>
            <tr class="bklabeloverflow">
              <!-- 审计发现标题 -->
              <sd-form-item-td label="问题名称（定性描述）" name="findTitle" :colspan="3">
                <a-input v-model="model.findTitle" />
                <!-- <span style="padding: 5px">
                  <a-button @click="showfind">选择</a-button>
                </span> -->
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计发现编号 -->
              <sd-form-item-td label="审计问题编号" name="findCode">
                <template v-slot>
                  <a-input v-model="model.findCode" placeholder="自动生成" read-only></a-input>
                </template>
              </sd-form-item-td>
              <!-- 被审计单位 -->
              <sd-form-item-td name="auditedUnit"> </sd-form-item-td>
            </tr>
            <tr>
              <sd-form-item-td label="审计问题描述" name="findDesc" :colspan="3">
                <template v-slot>
                  <a-textarea
                    v-model="model.findDesc"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td>
            </tr>
            <tr class="iamauditfindform">
              <!-- 是否下达审计意见书 -->
              <sd-form-item-td name="giveOpinion" :label="'是否属于审计意见书问题'">
              </sd-form-item-td>
              <!-- 问题性质 -->
              <sd-form-item-td name="problemNature">
                <a-select v-model="model.problemNature" :allow-clear="true">
                  <a-select-option
                    v-for="item in fields.problemNature.attr.selectListItem.filter((itc) =>
                      ['wt'].includes(itc.value)
                    )"
                    :key="item.value"
                    :value="item.value"
                  >
                    {{ item.label }}
                  </a-select-option>
                </a-select>
              </sd-form-item-td>
            </tr>

            <tr class="iamauditfindform">
              <!-- <sd-form-item-td label="审计问题类别" name="findType">
                <template v-slot>
                  <a-input v-model="findType" read-only @click="showfindcat" />
                  <a-input v-model="model.findType" :hidden="true" />
                </template>
                <template v-slot:read>
                  {{ JSON.parse(model.findType).text }}
                </template>
              </sd-form-item-td> -->
              <!-- <sd-form-item-td :hidden="true" name="suggestionType" /> -->
              <!-- 问题类别分类（管理职能） -->
              <sd-form-item-td name="problemCategoryFunction" :label="'问题分类'">
                <template v-slot>
                  <a-input v-model="problemCategoryFunction" read-only @click="showfindcat" />
                  <a-input v-model="model.problemCategoryFunction" :hidden="true" />
                </template>
                <template v-slot:read>
                  {{ JSON.parse(model.problemCategoryFunction).text }}
                </template></sd-form-item-td
              >

              <!-- 是否属于零容忍事项 -->
              <sd-form-item-td name="zeroTolerate"> </sd-form-item-td>
            </tr>
            <tr class="iamauditfindform">
              <!-- 是否移送纪检监察线索（单移送） -->
              <sd-form-item-td name="transferClueSingle"> </sd-form-item-td>
              <!-- 是否移送纪检监察线索（双移送） -->
              <sd-form-item-td name="transferClueDouble"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计处理意见 -->
              <sd-form-item-td name="iamBasis" :colspan="3" :label="'问题依据'">
                <a-textarea v-model="model.iamBasis" :rows="3" />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- <sd-form-item-td name="suggestion" :colspan="3" >
                <template v-slot>
                  <a-textarea
                    v-model="model.suggestion"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td> -->
              <!-- 审计处理意见 -->
              <sd-form-item-td name="handleSuggestion" :colspan="3" :label="'审计处理意见'">
                <a-textarea v-model="model.handleSuggestion" :rows="3" />
              </sd-form-item-td>
            </tr>
            <!-- 问题来源 -->
            <!-- 问题类别分类（按部门分类） -->
            <!-- <tr class="iamauditfindform">

              <sd-form-item-td name="problemSource"> </sd-form-item-td>

              <sd-form-item-td name="problemCategoryDept"> </sd-form-item-td>
            </tr> -->

            <!-- <tr v-show="false"> -->
            <!-- 对应审计步骤-->
            <!-- <sd-form-item-td name="stepName">
                <a-input v-model="model.stepName" type="text" read-only @click="showstep" />
              </sd-form-item-td> -->
            <!-- 问题类型 -->
            <!-- <sd-form-item-td name="problemType" /> -->
            <!-- </tr> -->

            <!-- 是否金额类问题 -->
            <!-- <tr>
              <sd-form-item-td name="amountProblem"> </sd-form-item-td>
            </tr> -->
            <!-- <tr class="iamauditfindform"> -->
            <!-- 审计意见书下发时间 -->
            <!-- <sd-form-item-td name="opinionGiveTime"> </sd-form-item-td> -->
            <!-- 问题类别 -->
            <!-- <sd-form-item-td name="problemType" /> -->
            <!-- </tr> -->
            <!-- <tr class="iamauditfindform"> -->
            <!-- 是否属于重大违规问题 -->
            <!-- <sd-form-item-td name="greatProblem"> </sd-form-item-td> -->
            <!-- 重大违规问题类别 -->
            <!-- <sd-form-item-td name="greatProblemCategory"> </sd-form-item-td> -->
            <!-- </tr> -->
            <!-- <tr> -->
            <!-- 零容忍事项确认依据 -->
            <!-- <sd-form-item-td name="zeroTolerateBasis"> </sd-form-item-td> -->
            <!-- </tr> -->
            <!-- <tr> -->
            <!-- 审计依据 -->
            <!-- <sd-form-item-td name="iamBasis"> </sd-form-item-td> -->
            <!-- 审计处理意见 -->
            <!-- <sd-form-item-td name="handleSuggestion"> </sd-form-item-td> -->
            <!-- </tr> -->
            <!-- <tr> -->
            <!-- 问题责任部门-->
            <!-- <sd-form-item-td name="responsibleDept" /> -->
            <!-- 风险评级 -->
            <!-- <sd-form-item-td :hidden="true" name="riskLevel" /> -->
            <!-- </tr> -->
            <!-- <tr v-show="false"> -->
            <!-- 所属业务域-->
            <!-- <sd-form-item-td name="businessProcess" /> -->
            <!-- 是否问责 -->
            <!-- <sd-form-item-td name="whetherOrderDuty" /> -->
            <!-- </tr> -->
            <!-- <tr v-show="false"> -->
            <!-- 问责要求 -->
            <!-- <sd-form-item-td name="orderDutyAsk" :colspan="3">
                <template v-slot>
                  <a-textarea
                    v-model="model.orderDutyAsk"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td> -->
            <!-- </tr> -->
            <tr>
              <!-- 整改责任主体-->
              <sd-form-item-td name="respUnitName" />
              <!-- 发送整改状态 -->
              <sd-form-item-td name="sendStatus" />
            </tr>
            <tr>
              <!-- 备注 -->
              <sd-form-item-td name="findRemark" :colspan="3">
                <template v-slot>
                  <a-textarea
                    v-model="model.findRemark"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 非金额类问题类别-->
              <sd-form-item-td name="notAmountType" />
            </tr>
          </table>

          <table>
            <tr
              ><td :colspan="4">
                <sd-form-item name="iamAuditReportFindMoneyEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="moneyExpand"
                      :expand-str="'moneyExpand'"
                      :group-label="'问题涉及金额列表'"
                      tablestyle="''"
                      @changedClick="changedClick"
                    >
                      <template>
                        <xm-child-table
                          v-show="moneyExpand"
                          ref="moneyData"
                          v-model="model.iamAuditReportFindMoneyEntitys"
                          :class="$style.deptPlanChild"
                          :label="null"
                          :columns="moneycolumns"
                          :colspan="4"
                          :read-only="!editable"
                          name="iamAuditReportFindMoneyEntitys"
                          :changeflag="true"
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'sortNum',
                              },
                            ].concat(
                              Array.from(fields.iamAuditReportFindMoneyEntitys.attr.dync).slice(2)
                            )
                          "
                          @change="changetablemoney"
                        >
                          <!-- input-form 表示这部分是子表组件的form插槽 -->
                          <template v-slot:form="{ model }">
                            <table>
                              <tr><sd-form-item-td name="projectId" :hidden="true"/></tr>
                              <tr><sd-form-item-td name="findId" :hidden="true"/></tr>
                              <tr><sd-form-item-td name="id" :hidden="true"/></tr>
                              <tr><sd-form-item-td name="assetType"/></tr>
                              <tr><sd-form-item-td name="problemClass"/></tr>
                              <tr><sd-form-item-td name="complianceProblem"/></tr>
                              <tr><sd-form-item-td name="amount"/></tr>
                            </table>
                          </template>
                        </xm-child-table>
                        <!-- <sd-child-table
                          v-if="dataReady"
                          ref="childMoneyTable"
                          v-model="model.iamAuditReportFindMoneyEntitys"
                          :read-only="!editable"
                          :fields="childMoneyFields"
                          :columns="childMoneyColumns"
                        >
                          <template v-slot:form="{ model, fields }">
                            <table>
                              <tr>
                                <sd-form-item-td name="assetType"> </sd-form-item-td>
                              </tr>
                              <tr>
                                <sd-form-item-td name="amount"> </sd-form-item-td>
                              </tr>
                            </table>
                          </template>
                        </sd-child-table> -->
                      </template>
                    </audit-advanced-group>
                  </template>
                </sd-form-item>
              </td>
            </tr>
            <tr v-show="moneyExpand">
              <sd-form-item-td :label="null" name="reservestring1" :colspan="4">
                <template v-slot:read-and-edit="{ editable }">
                  <table style="width: 100%">
                    <tr
                      ><td
                        v-if="editable"
                        style="width: 60px; border: none"
                        class="ant-form-item-label ant-form-item-label"
                      ></td
                      ><td
                        style="width: 80px; border: none"
                        class="ant-form-item-label ant-form-item-label"
                      ></td
                      ><td
                        style="width: 50%; border: none"
                        class="ant-form-item-label ant-form-item-label"
                        ><label title="合计" class=""> 合计 </label></td
                      ><td style="border: none; border-left: 1px solid #e8e8e8"
                        ><span>{{ model.reservestring1 }}</span></td
                      ></tr
                    >
                  </table>
                </template>
              </sd-form-item-td>
            </tr>
          </table>

          <!-- 发现分类树 -->
          <a-modal
            :visible="findcatvisible"
            title="请选择"
            width="700px"
            :destroy-on-close="true"
            @ok="handlefindcatOk"
            @cancel="handlefindcatCancel"
          >
            <AuditFindCatTree :default-expand-all="true" @treeSelect="treeSelect" />
          </a-modal>
          <!-- 选择审计步骤模板 -->
          <a-modal
            :visible="stepnamevisible"
            title="请选择"
            width="1200px"
            :body-style="{
              minHeight: '700px',
            }"
            :destroy-on-close="true"
            @ok="handlestepOk"
            @cancel="handlestepCancel"
            ><sd-data-table
              ref="stepTable"
              :loading="false"
              :class="$style.levelTable"
              :columns="stepmodalcolumns"
              data-url="api/xcoa-mobile/v1/audit-report-find/find-step"
              check-type="radio"
              show-selection
              :row-key="'SORT_NUM'"
              :filter-expressions="[
                {
                  dataType: 'long',
                  name: 'originalPaperId',
                  op: 'eq',
                  longValue: model.originalPaperId,
                },
              ]"
            />
            <!-- <sd-table
              ref="stepTable"
              :loading="false"
              :class="$style.levelTable"
              :columns="stepmodalcolumns"
              :data-source="steplist"
              :pagination="true"
              :bordered="true"
              :row-selection="{
                getCheckboxProps(record) {
                  return {
                    props: { disabled: !showSelection(record) },
                  }
                },
                selectedRowKeys: selectedRowKeys,
                onChange: onSelectChange,
              }"
            /> -->
          </a-modal>
          <!-- 选择审计步骤模板 -->
          <a-modal
            :visible="findvisible"
            title="请选择"
            width="1200px"
            :body-style="{
              minHeight: '700px',
              padding: '0 10px',
            }"
            :destroy-on-close="true"
            @ok="handlefindOk"
            @cancel="handlefindCancel"
          >
            <sd-data-table-ex
              ref="findTable"
              class="modalTable"
              check-type="radio"
              :filter-expressions="expressions"
              :columns="findcolumns"
              form-id="iamProblem"
              page-id="audit/auditsource/problem/iamProblem"
              :search-fields="['docTitle', 'problemNumber']"
              show-selection
            >
            </sd-data-table-ex>
          </a-modal>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
    <!-- 法律法规选择器 -->
    <audit-matters-select-modal
      :visible="visible"
      :org-id="null"
      @listSelected="listSelected"
    ></audit-matters-select-modal>
  </span>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditMattersSelectModal from '../../../matters/audit-matters-select-modal.vue'

import auditFormTopBanner from '../../../../components/audit-form-top-banner.vue'
import auditAdvancedGroup from '../../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../../components/audit-advanced-group-mixins'
import AuditFindCatTree from '../../../../components/audit-find-cat-tree.vue'
import AuditFindCatTreeMixins from '../../../../components/audit-find-cat-tree-mixins'
import iamAuditService from './iam-audit-find-service'
import components from './_import-components/iam-audit-find-form-import'
const moneycolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '问题金额性质',
    dataIndex: 'assetType',
    width: '25%',
    sdHidden: true,
  },
  {
    title: '问题分类',
    dataIndex: 'problemClass',
  },
  {
    title: '合规性问题分类',
    dataIndex: 'complianceProblem',
  },
  {
    title: '问题金额（万元）',
    dataIndex: 'amount',
  },
]
const rulecolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '标题',
    dataIndex: 'ruleName',
    width: '40%',
  },
  {
    title: '发文字号',
    dataIndex: 'ruleNumber',
    width: '15%',
  },
  {
    title: '条文',
    dataIndex: 'ruleContent',
  },
]
const stepmodalcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '步骤',
    dataIndex: 'STEP_NAME',
  },
  {
    title: '重要性',
    dataIndex: 'IMPORTANCE',
    width: '100px',
  },
  {
    title: '是否执行',
    dataIndex: 'IS_EXECITED',
    width: '120px',
  },
]
const findcolumns = [
  {
    title: '序号',
    dataIndex: 'sortNum',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '问题标题',
    dataIndex: 'docTitle',
  },
  {
    title: '问题编号',
    dataIndex: 'problemNumber',
    width: '150px',
  },
  {
    title: '问题描述',
    dataIndex: 'problemDesc',
  },
  {
    title: '',
    dataIndex: 'iamAuditLawsRelEntitys',
    sdHidden: true,
  },
]
export default {
  name: 'IamAuditFindForm',
  metaInfo: {
    title: '审计问题表单',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    AuditFindCatTree,
    auditMattersSelectModal,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      basicExpand: true,
      moneyExpand: true,
      ruleExpand: true,
      dataReady: false,
      stepnamevisible: false,
      stepmodalcolumns,
      steplist: [],
      selectedRowKeys: [],
      expressions: [],
      findcolumns,
      rulecolumns,
      findcatvisible: false,
      findcat: '',
      mode: null,
      readOnly: false,
      findType: '',
      problemCategoryFunction: '',
      projectId: this.$route.query.projectId, // 项目ID
      projectInfo: null, // 项目信息
      recodeId: this.$route.query.record ? parseInt(this.$route.query.record) : null,

      childMoneyFields: [
        {
          title: '序号',
          dataIndex: 'xh',
          customRender: (text, record, index) => `${index + 1}`,
          width: '30px',
          name: 'xh',
        },
        {
          title: '问题金额性质',
          dataIndex: 'assetType',
          name: 'assetType',
        },
        {
          title: '问题金额（万元）',
          dataIndex: 'amount',
          name: 'amount',
        },
      ],
      moneycolumns,
      childMoneyColumns: [
        {
          title: '序号',
          dataIndex: 'xh',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '问题金额性质',
          dataIndex: 'assetType',
          name: 'assetType',
          widht: '50%',
        },
        {
          title: '问题金额（万元）',
          dataIndex: 'amount',
          name: 'amount',
        },
      ],
      childRuleFields: [
        {
          title: '序号',
          dataIndex: 'xh',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
          name: 'xh',
        },
        {
          title: '标题',
          dataIndex: 'ruleName',
          name: 'ruleName',
        },
        {
          title: '发文字号',
          dataIndex: 'ruleNumber',
          name: 'ruleNumber',
        },
        {
          title: '条文',
          dataIndex: 'ruleContent',
          name: 'ruleContent',
        },
      ],
      childRuleColumns: [
        {
          title: '序号',
          dataIndex: 'xh',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '标题',
          dataIndex: 'ruleName',
          name: 'ruleName',
          width: '40%',
        },
        {
          title: '发文字号',
          dataIndex: 'ruleNumber',
          name: 'ruleNumber',
          width: '15%',
        },
        {
          title: '条文',
          dataIndex: 'ruleContent',
          name: 'ruleContent',
        },
      ],

      findvisible: false,
      flag: true,
      visible: false,
    }
  },
  mounted() {},
  methods: {
    listSelected(keys, keyinfos) {
      let iamAuditLaws = []
      if (this.$refs.docform.getFieldValue('iamAuditReportFindRuleEntitys')) {
        iamAuditLaws = this.$refs.docform.getFieldValue('iamAuditReportFindRuleEntitys')
      }

      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.ruleName = element.docTitle
        mmc.ruleNumber = element.dispatchWord ? element.dispatchWord : ''
        mmc.ruleContent = ''
        iamAuditLaws.push(mmc)
      })
      this.visible = false
      this.$refs.docform.setFieldValue('iamAuditReportFindRuleEntitys', iamAuditLaws)
    },
    // 选择法律制度
    lawsRelSelect() {
      // 选择事项分类
      this.visible = true
    },
    changetable(data) {
      if (!data) return
      data.forEach((item, index) => {
        item.sortNum = index + 1
      })
    },
    changetablemoney(data) {
      let money = 0
      if (!data) return
      data.forEach((item, index) => {
        // item.sortNum = index + 1
        if (item.amount !== undefined) {
          money = money + parseFloat(item.amount)
        }
      })
      this.$refs.docform.setFieldValue(
        'reservestring1',
        this.getTotal(data) ? this.getTotal(data).toString() : ''
      )
    },
    showfind() {
      this.findvisible = !this.findvisible
    },
    handlefindOk() {
      const selectInfo = this.$refs.findTable.getSelectedRows()
      if (selectInfo.length === 1) {
        this.$refs.docform.setFieldValue('findTitle', selectInfo[0].docTitle)
        // this.$refs.docform.setFieldValue('findCode', selectInfo[0].problemNumber)
        this.$refs.docform.setFieldValue('suggestion', selectInfo[0].problemDesc)

        this.$refs.docform.setFieldValue(
          'iamAuditReportFindRuleEntitys',
          selectInfo[0].iamAuditLawsRelEntitys.map((item) => {
            return {
              ruleName: item.lawsTitle,
              ruleNumber: item.lawsDocNumber,
              ruleContent: item.lawsArticle,
            }
          })
        )
        this.showfind()
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择一个发现问题!',
        })
        return false
      }
    },
    handlefindCancel() {
      this.showfind()
    },
    showSelection() {
      return true
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    showstep() {
      this.stepnamevisible = !this.stepnamevisible
    },
    handlestepOk() {
      const selectInfo = this.$refs.stepTable.getSelectedRows()
      if (selectInfo.length) {
        this.$refs.docform.setFieldValue('stepName', selectInfo[0].STEP_NAME)
        this.showstep()
      }
      // let stepname = ''
      // this.selectedRowKeys.forEach((i) => {
      //   stepname = stepname + this.steplist[i - 1].stepName
      // })
      // this.$refs.docform.setFieldValue('stepName', stepname)
      // this.showstep()
    },
    handlestepCancel() {
      this.showstep()
    },
    showfindcat() {
      this.findcatvisible = !this.findcatvisible
    },
    handlefindcatOk() {
      if (this.findcat !== '') {
        if (this.findcat.isLeaf || !this.findcat.expandable) {
          this.$refs.docform.setFieldValue(
            'problemCategoryFunction',
            JSON.stringify({
              id: this.findcat.id,
              text: this.findcat.text,
            })
          )
          this.showfindcat()
          this.problemCategoryFunction = this.findcat.text
          this.findcat = ''
        } else {
          Modal.warning({
            title: '提示',
            content: '请选择末级分类!',
          })
          return false
        }
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择末级分类!',
        })
        return false
      }
    },
    handlefindcatCancel() {
      this.showfindcat()
    },
    treeSelect(select, info) {
      if (info.node) {
        this.findcat = info.node.dataRef
      } else {
        this.findcat = info
      }
      // this.$refs.docform.setFieldValue('paperId', this.findcat)
    },
    // 页面加载完成后
    sdFormReady() {
      // debugger
      const projectStatus = this.$root.$route.query.projectStatus
      if (projectStatus === '05') {
        this.mode = 'VIEW'
        this.readOnly = true
      } else {
        const edit = this.$route.query.edit
        // 控制编辑
        if (edit === 'false') {
          this.mode = 'VIEW'
          this.readOnly = true
        } else {
          this.mode = this.$refs.docform.formData.pageFormData.pageFieldInfos.find(
            (item) => item.name === 'findTitle'
          ).readonly
            ? 'VIEW'
            : 'EDIT'
        }
      }
      // this.mode = this.$refs.docform.formData.pageFormData.pageFieldInfos.find(
      //   (item) => item.name === 'findTitle'
      // ).readonly
      //   ? 'VIEW'
      //   : 'EDIT'
    },
    // 增加序号列
    getFields(fields) {
      const xh = [
        {
          caption: '序号',
          dataType: 'string',
          index: 0,
          mobileVisible: true,
          name: 'xh',
          width: '30px',
        },
      ]
      // fields.reverse()
      fields = fields.filter((item) => {
        return item.name !== 'projectId'
      })
      // return [...fields]
      return [...xh, ...fields]
    },
    initData(model) {
      if (this.flag) {
        if (this.recodeId != null) {
          this.projectId = model.projectId
        } else {
          // 设置审计单位默认值
          iamAuditService.getAuditedUnitById(this.projectId).then((res) => {
            setTimeout(() => {
              this.$refs.docform.setFieldValue('auditedUnit', res.data)
            }, 1000)
            // 设置projectId
            setTimeout(() => {
              this.$refs.docform.setFieldValue('projectId', parseInt(this.projectId))
            }, 1000)
          })
        }

        const findcatstr = model.problemCategoryFunction
        if (findcatstr !== '' && findcatstr !== undefined) {
          const findcatobj = JSON.parse(findcatstr)
          this.problemCategoryFunction = findcatobj.text
        }

        let child = this.$refs.docform.formData.pageFormData.pageFieldInfos.find((item) => {
          return item.name === 'iamAuditReportFindMoneyEntitys'
        })
        if (child) {
          this.childMoneyFields = this.getFields(child.attr.dync)
        }

        child = this.$refs.docform.formData.pageFormData.pageFieldInfos.find((item) => {
          return item.name === 'iamAuditReportFindRuleEntitys'
        })
        if (child) {
          this.childRuleFields = this.getFields(child.attr.dync)
        }
        this.flag = false
        setTimeout(() => {
          this.changetablemoney(model.iamAuditReportFindMoneyEntitys)
        }, 1000)
      }
      return false
    },

    handleBeforeAdd(val, callback) {
      const fileCode = this.$refs.docform.getFieldValue('findCode')
      if (fileCode && fileCode !== '') {
        callback(false)
      } else {
        // 生成文件编号
        iamAuditService.getFileCode(this.projectId).then((res) => {
          this.$refs.docform.setFieldValue('findCode', res.data)
          callback(false)
        })
      }
    },
    // 保存
    saveForm() {
      let toSubmit = true // 默认可以提交
      let iamAuditMoneys = []
      if (this.$refs.docform.getFieldValue('iamAuditReportFindMoneyEntitys')) {
        iamAuditMoneys = this.$refs.docform.getFieldValue('iamAuditReportFindMoneyEntitys')
      }
      const newMoneysList = []
      iamAuditMoneys.forEach((item, index) => {
        if (
          item.problemClass === '01' &&
          (item.complianceProblem === '' ||
            item.complianceProblem === null ||
            item.complianceProblem === undefined)
        ) {
          toSubmit = false
          // newMoneysList.push(item)
        } else if (
          item.problemClass === '02' &&
          (item.complianceProblem === '' ||
            item.complianceProblem === 'null' ||
            item.complianceProblem === undefined)
        ) {
          item.complianceProblem = null
          newMoneysList.push(item)
        } else {
          newMoneysList.push(item)
        }
      })
      // this.$refs.docform.setFieldValue('iamAuditReportFindMoneyEntitys', newMoneysList)
      if (toSubmit) {
        this.$refs.docform.setFieldValue('iamAuditReportFindMoneyEntitys', newMoneysList)
        this.handleBeforeAdd(null, (res) => {
          this.$refs.docform.saveBtnClick()
        })
      } else {
        Modal.warning({
          title: '提示',
          content: '问题分类选择“合规性问题”时，合规性问题分类字段必填!',
        })
      }
      // this.handleBeforeAdd(null, (res) => {
      //   this.$refs.docform.saveBtnClick()
      // })
    },
    saved() {
      Message.success('保存成功', 1).then((res) => {
        this.close(true)
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';
.first-tr {
  td {
    background: #fff !important;
    border: none !important;
  }
}

.btnselect {
  position: relative;
  left: 20px;
  float: right;
  .batchselect {
    top: 4px;
    z-index: 100;
    margin-right: 120px;
  }
}
.form {
  :global .iamauditfindform > td.ant-form-item-label > label {
    width: 220px;
    white-space: break-spaces !important;
  }
}
</style>
