<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :form-data="$route.query.mode"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        page-id="risk/event/riskEvent"
        :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
        :read-only="$route.query.mode === 'VIEW'"
        :class="$style.form"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="sdFormReady"
      >
        <template v-slot="{ model, fields }">
          <table
            ><tr
              ><td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="expandA"
                  :expand-str="'expandA'"
                  :group-label="'基本信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group> </td></tr
          ></table>

          <table v-show="expandA">
            <colgroup>
              <col style="width: 15%;"/>
              <col style="width: 35%;"/>
              <col style="width: 15%;"/>
              <col style="width: 35%;"
            /></colgroup>
            <tr v-show="false"
              ><sd-form-item-td name="riskCategoryId"/> <sd-form-item-td name="auditOrgId"
            /></tr>
            <tr v-show="false"
              ><sd-form-item-td name="versionId" /><sd-form-item-td name="id" />
            </tr>
            <tr
              ><sd-form-item-td name="riskCategoryPath" :colspan="3" :label="'风险分类'">
                <a-input
                  v-model="model.riskCategoryPath"
                  style="width:calc(100% - 80px);"
                  read-only
                />
                <span style="padding:5px">
                  <a-button @click="showflpath">选择</a-button>
                </span>
              </sd-form-item-td></tr
            >
            <tr>
              <!-- 措施编号 -->
              <sd-form-item-td v-if="initParam(model)" name="eventCode" />
              <!-- 措施名称 -->
              <sd-form-item-td name="eventName"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 措施描述 -->
              <sd-form-item-td name="eventDesc" :colspan="3">
                <a-textarea v-model="model.eventDesc" :rows="3" />
              </sd-form-item-td>
            </tr>
            <tr>
              <sd-form-item-td
                name="riskSource"
                :input-props="{
                  defaultValue: 'within',
                }"
              />
              <td class="ant-form-item-label ant-form-item-label">
                <label
                  title="主管部门"
                  :class="{
                    ['ant-form-item-required']: $route.query.mode !== 'VIEW',
                  }"
                >
                  主管部门
                </label>
              </td>
              <td class="ant-form-item-control-wrapper">
                <a-form-model
                  v-if="$route.query.mode !== 'VIEW'"
                  ref="recommendRuleForm"
                  :rules="{
                    competentDeptOpt: [
                      {
                        required: true,
                        message: '请选择主管部门',
                        trigger: ['change', 'blur'],
                      },
                    ],
                  }"
                  :model="model1"
                >
                  <a-form-model-item prop="competentDeptOpt" :label="null">
                    <sd-group-picker
                      v-model="model1.competentDeptOpt"
                      style="width:calc(100% - 120px)"
                      @change="changecompetentDeptformodel"
                    /><sd-group-picker
                      v-show="false"
                      v-model="model.competentDeptOpt"
                      style="width:calc(100% - 120px)"
                      @change="changecompetentDept"
                    />
                    <a-input v-show="false" v-model="model.competentDeptName" />
                    <a-input v-show="false" v-model="model.competentDeptId" />
                    <a-button
                      type="primary"
                      icon="plus"
                      block
                      style="width:110px;margin-left:5px;"
                      @click="manualEntry('competentDeptOpt', 'docform')"
                    >
                      手工添加
                    </a-button>
                  </a-form-model-item>
                </a-form-model>
                <div v-else>{{ model.competentDeptName }}</div>
              </td>
              <!-- <sd-form-item-td name="competentDeptOpt">
                <sd-group-picker
                  v-model="model.competentDeptOpt"
                  style="width:calc(100% - 120px)"
                  @change="changecompetentDept"
                  v-show="false"
                />
                <a-button
                  type="primary"
                  icon="plus"
                  block
                  @click="manualEntry('competentDeptOpt', 'docform')"
                  style="width:110px;margin-left:5px;"
                >
                  手工添加
                </a-button>
              </sd-form-item-td> -->
            </tr>
            <tr
              ><sd-form-item-td name="riskReasonDesc" :colspan="3">
                <a-textarea v-model="model.riskReasonDesc" :rows="3" /> </sd-form-item-td
            ></tr>
            <tr>
              <sd-form-item-td
                name="riskLevel"
                component="a-select"
                :input-props="{
                  disabled: true,
                }"
              />
              <sd-form-item-td
                name="status"
                component="a-select"
                :input-props="{
                  disabled: true,
                  defaultValue: 'enable',
                }"
              />
            </tr>
            <tr>
              <sd-form-item-td
                name="occurPossibility"
                component="a-select"
                :input-props="{
                  disabled: true,
                }"
              />
              <sd-form-item-td
                name="riskImpactDegree"
                component="a-select"
                :input-props="{
                  disabled: true,
                }"
              />
            </tr>
            <tr>
              <sd-form-item-td name="creationTime" />
              <sd-form-item-td name="creatorName" />
            </tr>
            <tr>
              <sd-form-item-td name="remark" :colspan="3">
                <a-textarea v-model="model.remark" :rows="3" />
              </sd-form-item-td>
            </tr> </table

          ><table>
            <tr
              ><td :colspan="4">
                <sd-form-item name="riskEventLawEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandC"
                      :expand-str="'expandC'"
                      :group-label="'相关法规制度'"
                      @changedClick="changedClick"
                      ><template>
                        <template>
                          <div :class="$style.wrapper">
                            <div v-show="expandY && editable" :class="$style.btnselect">
                              <a-button
                                type="link"
                                :class="$style.batchselect"
                                @click="lawsRelSelect"
                              >
                                <a-icon type="check-circle" :theme="'filled'" />
                                选择
                              </a-button>
                            </div>
                          </div>
                        </template>
                        <xm-child-table
                          ref="docformlawlist"
                          v-model="model.riskEventLawEntitys"
                          :read-only="!editable"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.riskEventLawEntitys.attr.dync))
                          "
                          :columns="childlawlistColumns"
                          :width="1200"
                          :addfun="addfun"
                        >
                        </xm-child-table>
                      </template>
                    </audit-advanced-group>
                  </template> </sd-form-item
              ></td>
            </tr>

            <tr
              ><td :colspan="4">
                <sd-form-item name="riskEventResultEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandY"
                      :expand-str="'expandY'"
                      :group-label="'关联风险评估结果'"
                      @changedClick="changedClick"
                      ><template>
                        <xm-child-table
                          ref="docformEventresult"
                          v-model="model.riskEventResultEntitys"
                          :read-only="true"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.riskEventResultEntitys.attr.dync))
                          "
                          :columns="childEventresultColumns"
                          :width="1200"
                        >
                          <template v-slot:occurPossibility="{ field, text, index, value }">
                            {{ text }}
                          </template>
                          <template v-slot:riskImpactDegree="{ field, text, index, value }">
                            {{ text }}
                          </template>
                          <template v-slot:riskLevel="{ field, text, index, value }">
                            {{ text }}
                          </template>
                        </xm-child-table></template
                      >
                    </audit-advanced-group>
                  </template>
                </sd-form-item></td
              >
            </tr>
            <tr
              ><td :colspan="4">
                <sd-form-item name="riskEventCaseEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandX"
                      :expand-str="'expandX'"
                      :group-label="'关联风险案例'"
                      @changedClick="changedClick"
                      ><template>
                        <xm-child-table
                          ref="docformEventCase"
                          v-model="model.riskEventCaseEntitys"
                          :read-only="true"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.riskEventCaseEntitys.attr.dync))
                          "
                          :columns="childEventCaseColumns"
                          :width="1200"
                        >
                        </xm-child-table>
                      </template>
                    </audit-advanced-group>
                  </template> </sd-form-item
              ></td>
            </tr>
            <tr
              ><td :colspan="4">
                <sd-form-item name="riskEventLossEventEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandZ"
                      :expand-str="'expandZ'"
                      :group-label="'关联损失事件'"
                      @changedClick="changedClick"
                      ><template>
                        <xm-child-table
                          ref="docformEventLoss"
                          v-model="model.riskEventLossEventEntitys"
                          :read-only="true"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.riskEventLossEventEntitys.attr.dync))
                          "
                          :columns="childEventLossColumns"
                          :width="1200"
                        >
                        </xm-child-table>
                      </template>
                    </audit-advanced-group>
                  </template> </sd-form-item
              ></td>
            </tr>

            <tr
              ><td :colspan="4">
                <sd-form-item name="riskEventMeasureEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandW"
                      :expand-str="'expandW'"
                      :group-label="'关联内控措施'"
                      @changedClick="changedClick"
                      ><template>
                        <xm-child-table
                          ref="docformEventMeasure"
                          v-model="model.riskEventMeasureEntitys"
                          :read-only="true"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.riskEventMeasureEntitys.attr.dync))
                          "
                          :columns="childEventMeasureColumns"
                          :width="1200"
                        >
                          <template v-slot:measureName="{ field, text, index, value }">
                            <a :title="text" @click="openMeasure(value, index)">{{ text }}</a>
                          </template>
                        </xm-child-table>
                      </template>
                    </audit-advanced-group>
                  </template>
                </sd-form-item></td
              >
            </tr>
          </table>

          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="change"
                  :expand-str="'change'"
                  :group-label="'风险事项_变更信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table
            v-for="(bg, index) in fndeal(model.iamDetailFormModifyLogEntities)"
            v-show="change"
            :key="index"
            :class="$style.tablexm"
          >
            <colgroup>
              <col style="width: 20%;" />
              <col style="width: 30%;" />
              <col style="width: 20%;" />
              <col style="width: 30%;" />
            </colgroup>
            <tr>
              <td style="width:160px;text-align:right;vertical-align:top">{{ bg.creatorName }}</td
              ><td colspan="3"
                ><div
                  style="
    width: 18px;margin-left:2px;
    font-size: 8px;
    color: #fff;
    text-align:center;
    background-color: lightgreen;
    border-radius: 100%;
"
                  >√</div
                ><div style="padding-left:10px;margin-left:9px;border-left:1px solid gray"
                  >{{ bg.creationTime | sdDateFormat('YYYY-MM-DD HH:mm:ss') }}
                  <table
                    ><tr v-for="(x, index) in bg.xx" :key="index">
                      <td style="width:150px">[{{ x.fieldDesc }}]</td
                      ><td
                        >{{ x.oldValue !== '' ? '"' : ''
                        }}{{
                          x.oldValue.indexOf('[') !== -1 ||
                          x.oldValue.indexOf('{') !== -1 ||
                          x.newValue.indexOf('[') !== -1 ||
                          x.newValue.indexOf('{') !== -1
                            ? ''
                            : x.oldValue === 'null'
                            ? ''
                            : x.oldValue
                        }}{{ x.oldValue !== '' ? '" 改为 ' : ''
                        }}<span v-if="x.oldValue === ''" style="color:green">
                          {{
                            x.newValue.indexOf('[') !== -1 || x.newValue.indexOf('{') !== -1
                              ? '修改动态表格数据'
                              : x.newValue
                          }}</span
                        >
                        <span v-else>{{
                          x.newValue.indexOf('[') !== -1 || x.newValue.indexOf('{') !== -1
                            ? '修改动态表格数据'
                            : '"' + x.newValue + '"'
                        }}</span>
                      </td></tr
                    ></table
                  >
                </div></td
              >
            </tr>
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
    <audit-matters-select-modal
      :visible="visible"
      :org-id="null"
      :show-selection="showSelectionlaw"
      @listSelected="listSelected"
    ></audit-matters-select-modal>
    <!-- 发现分类树 -->
    <a-modal
      :visible="catvisible"
      title="请选择"
      width="700px"
      :destroy-on-close="true"
      @ok="handlecatOk"
      @cancel="handlecatCancel"
    >
      <risk-category-tree
        ref="riskCategorytree"
        show-line
        top-node-text="风险库"
        :is-select-dep="true"
        manager-type="view"
        :classstyle="true"
        :history="true"
        :hisv="this.$route.query.versionId ? this.$route.query.versionId : this.versionId"
        :hisorg="this.$route.query.versionId ? this.$route.query.orgId : this.orgId"
        :hisorgn="this.$route.query.orgName ? this.$route.query.orgName : this.orgName"
        @treeSelect="treeSelect"
      />
    </a-modal>

    <a-modal v-model="ModalVisible" title="手动录入" @ok="ModalHandleOk">
      <a-form-model ref="ModalForm" :model="ModalForm">
        <a-form-model-item label="主管部门" prop="orgName">
          <a-input v-model="ModalForm.orgName" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </span>
</template>

<script>
import { Modal, Message } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import debounce from 'lodash.debounce'
import axios from '@/common/services/axios-instance'
import riskDeptSgtxMixins from '@product/iam/components/risk-dept-sgtx-mixins.vue'
import auditFormTopBanner from '../../../components/audit-form-top-banner'
import auditAdvancedGroup from '../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../components/audit-advanced-group-mixins'
import auditMattersSelectModal from '../../../audit/matters/audit-matters-select-modal.vue'
import RiskService from '../risk-service'
import riskCategoryTree from '../category/risk-category-tree'
import components from './_import-components/risk-event-form-import'

export default {
  name: 'RiskEventForm',
  metaInfo: {
    title: '风险措施',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    auditMattersSelectModal,
    riskCategoryTree,
  },
  mixins: [auditAdvancedGroupMixins, riskDeptSgtxMixins],
  data() {
    return {
      change: true,
      catvisible: false,
      visible: false,
      expandA: true,
      expandC: true,
      mode: null,
      childlawlistColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'parentId', sdHidden: true },
        { dataIndex: 'title', width: '40%' },
        { dataIndex: 'docNum', width: '100px' },
        { dataIndex: 'clauses' },
        { dataIndex: 'versionId', sdHidden: true },
      ],
      expandY: true,
      childEventresultColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'parentId', sdHidden: true },
        { dataIndex: 'year', width: '10%' },
        { dataIndex: 'estimateName', width: '10%' },
        {
          dataIndex: 'occurPossibility',
          width: '10%',
          scopedSlots: { customRender: 'occurPossibility' },
        },
        { dataIndex: 'riskImpactDegree', scopedSlots: { customRender: 'riskImpactDegree' } },
        { dataIndex: 'riskLevel', scopedSlots: { customRender: 'riskLevel' } },
        { dataIndex: 'riskTrend' },
        { dataIndex: 'versionId', sdHidden: true },
      ],
      expandX: true,
      childEventCaseColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'parentId', sdHidden: true },
        { dataIndex: 'caseName', width: '30%' },
        { dataIndex: 'caseType' },
        { dataIndex: 'year' },
        { dataIndex: 'versionId', sdHidden: true },
      ],
      expandZ: true,
      childEventLossColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '140px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'parentId', sdHidden: true },
        { dataIndex: 'year', width: '10%' },
        { dataIndex: 'lossEventName', width: '15%' },
        { dataIndex: 'lossEventType', width: '10%' },
        { dataIndex: 'lossEventNature' },
        { dataIndex: 'lossMoney', title: '损失金额(万元)' },
        { dataIndex: 'happenDate' },
        { dataIndex: 'involvedLitigation' },
        { dataIndex: 'versionId', sdHidden: true },
      ],
      expandW: true,
      childEventMeasureColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '140px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'parentId', sdHidden: true },
        { dataIndex: 'mtxPrcPath', width: '30%' },
        { dataIndex: 'measureName' },
        { dataIndex: 'measureCode', defaultSortOrder: 'ascend' },
        { dataIndex: 'controlLevel', width: '10%' },
        { dataIndex: 'controlType', width: '10%' },
        { dataIndex: 'versionId', sdHidden: true },
        { dataIndex: 'sourceId', sdHidden: true },
      ],
      versionId: null,
      orgId: null,
      orgName: '',
      selectlist: [],
      catpath: '',
      catId: null,
      flag: false,
      sortNum: null,
      model1: {
        competentDeptOpt: null,
      },
    }
  },
  mounted() {
    if (this.$route.query.versionId) {
      this.versionId = parseInt(this.$route.query.versionId)
      this.orgId = this.$route.query.orgId
    }
  },
  // 图文列表参数变化更新数据
  // watch: {
  //   model1() {
  //     console.log("---变动")
  //     setTimeout(()=>{
  //       this.$refs.docform.setFieldValue("competentDeptOpt",this.model1.competentDeptOpt)
  //     },500)
  //   },
  // },
  methods: {
    openMeasure(record, index) {
      const url = '/mtx-measure-form?record=' + record[index].sourceId+'&mode=VIEW'
      // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    changecompetentDept(value) {
      this.model1.competentDeptOpt = value
    },
    changecompetentDeptformodel() {
      this.$refs.docform.setFieldValue('competentDeptOpt', this.model1.competentDeptOpt)
    },
    initParam(model) {
      if (!this.flag) {
        if (this.$route.query.versionId) {
          this.getpath(this.$route.query.catalogId).then(() => {
            this.$refs.docform.riskCategoryPath = this.catpath
            model.riskCategoryPath = this.catpath
            this.riskCategorId = this.$route.query.catalogId
          })
        } else {
          this.getpath(model.riskCategoryId).then(() => {
            this.$refs.docform.riskCategoryPath = this.catpath
            model.riskCategoryPath = this.catpath
          })
          this.versionId = model.versionId + ''
          this.orgId = model.auditOrgId + ''
        }
        if (model.riskEventLawEntitys) {
          model.riskEventLawEntitys.forEach((i) => {
            if (i.clauses === null) {
              i.clauses = ''
            }
          })
        }
        // 处理主管部门高级字段
        const opt = model.competentDeptOpt
        const name = model.competentDeptName
        const id = model.competentDeptId
        if (name !== undefined && id !== undefined && name !== null && id !== null) {
          const names = name.split(',')
          const ids = id.split(',')
          names.forEach((n, index) => {
            if (ids[index].indexOf('_sd') > -1) {
              const no = {
                type: 'GROUP',
                code: ids[index],
                name: n,
              }
              opt.splice(index, 0, no)
            }
          })
          this.model1.competentDeptOpt = opt
          model.competentDeptOpt = opt
        }
        this.flag = true
      }
      return true
    },
    getpath(id) {
      return new Promise((resolve, reject) => {
        RiskService.findupcatinfo(id).then((res) => {
          this.catpath = res.data
          resolve(res.data)
        })
      })
    },
    getsortNum(id) {
      return new Promise((resolve, reject) => {
        RiskService.fngetsortNum(id).then((res) => {
          this.sortNum = res
          resolve(res)
        })
      })
    },
    showflpath() {
      this.catvisible = !this.catvisible
      if (this.catvisible) {
        setTimeout(() => {
          this.$refs.riskCategorytree.initDeptList('')
        }, 1000)
      }
    },
    treeSelect(select, info) {
      const id = info.selectedNodes[0].data.props.id
      this.catpath = ''
      this.catId = null
      if (info.selectedNodes[0].data.props.props) {
        if (info.selectedNodes[0].data.props.props.isEnd === '1') {
          if (id !== -1) {
            this.getpath(id)
            this.catId = id
          } else {
            this.catpath = this.$route.query.orgName + '内控矩阵'
            this.catId = -1
          }
        } else {
          Modal.info({
            content: '请选择末级分类节点',
          })
        }
      }
    },
    handlecatOk() {
      if (this.catpath !== '') {
        this.$refs.docform.setFieldValue('riskCategoryPath', this.catpath)
        this.$refs.docform.setFieldValue('riskCategoryId', this.catId)
        this.showflpath()
        // this.findType = this.findcat.text
        this.catpath = ''
        this.catId = null
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择末级分类节点!',
        })
        return false
      }
    },
    handlecatCancel() {
      this.showflpath()
    },
    showSelectionlaw(record) {
      this.selectlist = this.$refs.docformlawlist.value
      if (this.selectlist.length === 0) {
        return true
      } else if (
        this.selectlist.findIndex(
          (i) => i.title + '-' + i.docNum === record.docTitle + '-' + record.dispatchWord
        ) > -1
      ) {
        return false
      } else {
        return true
      }
    },
    addfun(data) {
      if (!this.versionId) {
        this.versionId = this.$refs.docform.getFieldValue('versionId')
        this.orgId = this.$refs.docform.getFieldValue('auditOrgId')
      }
      data.versionId = this.versionId
      return data
    },
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
      if (!this.mode) {
        this.mode = this.$route.query.mode
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    saved() {
      Message.success({ content: '保存成功！' }, 1).then(() => {
        debugger
        this.close(true)
      })
    },
    // 保存
    saveForm() {
      this.$refs.docform
        .validateFields()
        .then((res) => {
          this.$refs.recommendRuleForm.validate((valid) => {
            if (res && valid) {
              // 如果是新文档，则赋值
              // 校验编号唯一
              const eventCode = this.$refs.docform.getFieldValue('eventCode')
              const id = this.$refs.docform.getFieldValue('id')
                ? this.$refs.docform.getFieldValue('id')
                : -1
              const versionId = parseInt(this.versionId)
              const auditOrgId = parseInt(this.orgId)
              RiskService.checkMeasurecode(eventCode, id, versionId, auditOrgId).then((res) => {
                if (!res.data) {
                  Message.info('编号重复请重新填写', 1)
                } else {
                  let versionId = null
                  if (this.$route.query.record === null || this.$route.query.record === undefined) {
                    const catalogId = this.$route.query.catalogId
                    const orgId = this.$route.query.orgId
                    versionId = parseInt(this.$route.query.versionId)
                    // 没有值才赋值，如果有值说明是手动选择的不需要赋值，否则会覆盖掉
                    if (!this.$refs.docform.getFieldValue('riskCategoryId')) {
                      this.$refs.docform.setFieldValue('riskCategoryId', parseInt(catalogId))
                    }
                    this.$refs.docform.setFieldValue('auditOrgId', parseInt(orgId))
                    this.$refs.docform.setFieldValue('versionId', versionId)
                  } else {
                    versionId = this.$refs.docform.getFieldValue('versionId')
                  }
                  // 处理icMtxMeasureEventList 中的主管部门高级字段
                  const competentDeptOpt = this.$refs.docform.getFieldValue('competentDeptOpt')
                  let names = ''
                  let ids = ''
                  const nopt = []
                  competentDeptOpt.forEach((o) => {
                    names = names + o.name + ','
                    ids = ids + o.code + ','
                    if (o.code.indexOf('_sd') === -1) {
                      nopt.push(o)
                    }
                  })
                  if (names.substring(names.length - 1, names.length) === ',')
                    names = names.substring(0, names.length - 1)
                  if (ids.substring(ids.length - 1, ids.length) === ',')
                    ids = ids.substring(0, ids.length - 1)
                  this.$refs.docform.setFieldValue('competentDeptOpt', nopt)
                  this.$refs.docform.setFieldValue('competentDeptName', names)
                  this.$refs.docform.setFieldValue('competentDeptId', ids)
                  // 轮询所有子表赋值versionId
                  this.$refs.docform.saveBtnClick()
                }
              })
            } else {
              return false
            }
          })
        })
        .catch(() => {
          this.$refs.recommendRuleForm.validate((valid) => {})
        })
    },
    measureRelSelect() {},

    fndeal(obj) {
      const newobj = []
      if (obj !== undefined) {
        obj.forEach((item) => {
          if (
            [
              'riskEventLawEntitys',
              'riskEventLossEventEntitys',
              'riskEventResultEntitys',
              'riskEventMeasureEntitys',
              'riskEventCaseEntitys',
              'iamDetailFormModifyLogEntities',
              'riskCategoryId',
            ].indexOf(item.fieldName) < 0
          ) {
            if (
              item.newValue !== '' &&
              item.oldValue !== null &&
              item.fieldDesc.indexOf('ID') === -1 &&
              item.fieldDesc !== '主管部门'
            ) {
              item.xt = item.creatorName + '_' + item.creationTime
              if (item.oldValue.trim() === null || item.oldValue.trim() === 'null')
                item.oldValue = ''
              const i = newobj.find((n) => n.xt === item.xt)
              if (i !== undefined) {
                i.xx.push({
                  fieldDesc: item.fieldDesc,
                  fieldName: item.fieldName,
                  newValue: item.newValue,
                  oldValue: item.oldValue,
                })
              } else {
                item.xx = [
                  {
                    fieldDesc: item.fieldDesc,
                    fieldName: item.fieldName,
                    newValue: item.newValue,
                    oldValue: item.oldValue,
                  },
                ]
                newobj.push(item)
              }
            }
          }
        })
      }
      return newobj
    },
    // 选择法律制度
    lawsRelSelect() {
      // 选择事项分类
      this.visible = true
    },
    listSelected(keys, keyinfos) {
      let iamAuditLaws = []
      if (this.$refs.docform.getFieldValue('riskEventLawEntitys')) {
        iamAuditLaws = this.$refs.docform.getFieldValue('riskEventLawEntitys')
      }
      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.title = element.docTitle
        mmc.docNum = element.dispatchWord ? element.dispatchWord : ''
        mmc.versionId = this.versionId
        mmc.clauses = ''
        iamAuditLaws.push(mmc)
      })
      this.visible = false
      this.$refs.docform.setFieldValue('riskEventLawEntitys', iamAuditLaws)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';

.btnselect {
  position: relative;
  top: 4px;
  right: 200px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
.tablexm tr td {
  border: none !important;
}
</style>
