<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :form-data="mode"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/matters/iamAuditMatters"
        :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
        :class="$style.form"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="sdFormReady"
      >
        <template v-slot="{ model, fields }">
          <table>
            <tr>
              <!-- 审计事项编号 -->
              <sd-form-item-td name="auditMattersCode">
                <a-form-model-item
                  :rules="[
                    {
                      validator: validator,
                      trigger: ['change', 'blur'],
                    },
                  ]"
                  prop="auditMattersCode"
                  name="auditMattersCode"
                >
                  <a-input v-model.trim="model.auditMattersCode" />
                </a-form-model-item>
              </sd-form-item-td>

              <!-- 审计事项名称 -->
              <sd-form-item-td name="auditMattersName" />
            </tr>
            <tr>
              <!-- 风险点名称 -->
              <sd-form-item-td name="riskPointName" :colspan="3" />
            </tr>
            <sd-form-item-td v-if="initParam(model)" :hidden="true" name="catalogId" />
            <sd-form-item-td :hidden="true" name="catalogName" />
            <sd-form-item-td :hidden="true" name="auditMattersPath" />
            <sd-form-item-td :hidden="true" name="orgId" />
            <!-- 风险点描述 -->

            <tr>
              <sd-form-item-td name="riskPointDesc" :colspan="3">
                <a-textarea v-model="model.riskPointDesc" :rows="3" />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 检查资料 -->
              <sd-form-item-td name="checkMaterial" :colspan="3">
                <a-textarea v-model="model.checkMaterial" :rows="3" />
              </sd-form-item-td>
            </tr>

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

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

            <tr
              ><td :colspan="4">
                <sd-form-item name="iamAuditProcedureEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandC"
                      :expand-str="'expandC'"
                      :group-label="'审计程序'"
                      @changedClick="changedClick"
                      ><template>
                        <xm-child-table
                          ref="docformProcedure"
                          v-model="model.iamAuditProcedureEntitys"
                          :read-only="!editable"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.iamAuditProcedureEntitys.attr.dync))
                          "
                          :columns="childColumns"
                          :width="1200"
                        >
                        </xm-child-table>
                      </template>
                    </audit-advanced-group>
                  </template> </sd-form-item
              ></td>
            </tr>

            <tr
              ><td :colspan="4">
                <sd-form-item name="iamAuditLawsRelEntitys" :label="null">
                  <template v-slot:read-and-edit="{ editable }">
                    <audit-advanced-group
                      :expand="expandY"
                      :expand-str="'expandY'"
                      :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="docformLawsRel"
                          v-model="model.iamAuditLawsRelEntitys"
                          :read-only="!editable"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.iamAuditLawsRelEntitys.attr.dync))
                          "
                          :columns="childLawsColumns"
                          :width="1200"
                        >
                        </xm-child-table
                      ></template>
                    </audit-advanced-group>
                  </template> </sd-form-item
              ></td>
            </tr>
          </table>
        </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 } 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 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-select-modal.vue'
import components from './_import-components/audit-matters-form-import'
export default {
  name: 'AuditMattersForm',
  metaInfo: {
    title: '审计事项',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    auditMattersSelectModal,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      expandC: true,
      mode: null,
      expandY: true,
      visible: false,
      orgId: this.$route.query.orgId,
      childColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'stepName', width: '60%' },
        { dataIndex: 'importance', width: '100px' },
        { dataIndex: 'attachment' },
      ],
      childLawsColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'lawsLibraryId', sdHidden: true },
        { dataIndex: 'lawsType', sdHidden: true },
        {
          dataIndex: 'lawsTitle',
          width: '40%',
        },
        { dataIndex: 'lawsDocNumber', width: '15%' },
        { dataIndex: 'lawsArticle' },
      ],
    }
  },
  created() {},
  methods: {
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
    },
    validator(rule, value, callback) {
      let id = -1
      if (this.$route.query.record) {
        id = this.$route.query.record
      }
      value = encodeURIComponent(value)
      this.orgId = this.$refs.docform.getFieldValue('orgId')
      const url =
        `api/xcoa-mobile/v1/iam-audit-matters/check-category-code?catalogCode=${value}&orgId=${this.orgId}&id=` +
        id
      debounce(() => {
        axios.get(url).then((res) => {
          if (res.data) {
            callback()
          } else {
            callback('已存在事项编码，不可以重复')
          }
        })
      }, 500)()
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    saved() {
      Message.success({ content: '保存成功！' }, 1).then(() => {
        this.close(true)
      })
    },
    // 保存
    saveForm() {
      this.$refs.docform.validateFields().then((res) => {
        // 校验动态子表
      })
      this.$refs.docformProcedure.valid().then((res) => {
        // 校验动态子表
        if (res) {
          this.$refs.docformLawsRel.valid().then((res) => {
            // 校验动态子表
            if (res) {
              this.$refs.docform.saveBtnClick()
            } else {
              return false
            }
          })
        } else {
          this.$refs.docformLawsRel.valid().then((res) => {
            // 校验动态子表
          })
          return false
        }
      })
    },

    initParam(model) {
      // 设置父id
      const catalogId = this.$route.query.catalogId
      const catalogName = this.$route.query.catalogName
      const auditMattersPath = this.$route.query.auditMattersPath
      const orgId = this.$route.query.orgId
      if (catalogId) {
        model.catalogId = Number(catalogId)
      }

      if (catalogName) {
        model.catalogName = catalogName
      }
      if (auditMattersPath) {
        model.auditMattersPath = auditMattersPath
      }
      if (orgId) {
        model.orgId = orgId
      }
    },
    // 序号赋值
    changeNum(data) {
      data.forEach((item, index) => {
        item.sortNum = index + 1
      })
    },

    // 选择法律制度
    lawsRelSelect() {
      // 选择事项分类
      this.visible = true
    },
    listSelected(keys, keyinfos) {
      let iamAuditLaws = []
      if (this.$refs.docform.getFieldValue('iamAuditLawsRelEntitys')) {
        iamAuditLaws = this.$refs.docform.getFieldValue('iamAuditLawsRelEntitys')
      }

      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.lawsTitle = element.docTitle
        mmc.lawsDocNumber = element.dispatchWord ? element.dispatchWord : ''
        mmc.lawsArticle = ''
        iamAuditLaws.push(mmc)
      })
      this.$refs.docform.setFieldValue('iamAuditLawsRelEntitys', iamAuditLaws)
    },
  },
}
</script>

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

.btnselect {
  position: relative;
  top: 2px;
  right: 200px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
</style>
