<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="isReadOnly ? null : saveForm"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        :read-only="isReadOnly"
        page-id="audit/work/frame/iamAuditFrameItem"
        :record-id="this.$route.query.recordId ? parseInt(this.$route.query.recordId) : null"
        :class="$style.form"
        @close="close(true)"
        @saved="saved"
      >
        <template v-slot="{ model, fields }">
          <table>
            <tr style="display:none">
              <!-- 审计分类ID -->
              <sd-form-item-td name="catalogId" />
              <!-- 审计分类名称 -->
              <sd-form-item-td name="catalogName" />
              <!-- 审计框架ID -->
              <sd-form-item-td name="frameId" />
            </tr>
            <tr>
              <!-- 审计事项编号 -->
              <sd-form-item-td name="itemCode">
                <a-form-model-item
                  :rules="[
                    {
                      validator: validator,
                      trigger: ['change', 'blur'],
                    },
                  ]"
                  prop="itemCode"
                  name="itemCode"
                >
                  <a-input v-model.trim="model.itemCode" />
                </a-form-model-item>
              </sd-form-item-td>

              <!-- 审计事项名称 -->
              <sd-form-item-td name="itemName" @keyup="initParam" />
            </tr>
            <tr>
              <!-- 风险点名称 -->
              <sd-form-item-td name="riskPointName" :colspan="3" />
            </tr>

            <!-- 风险点描述 -->
            <tr>
              <sd-form-item-td name="riskPointDesc" :colspan="3">
                <a-textarea v-model="model.riskPointDesc" :auto-size="{ maxRows: 3, minRows: 3 }" />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 检查资料 -->
              <sd-form-item-td name="checkMaterial" :colspan="3">
                <a-textarea v-model="model.checkMaterial" :auto-size="{ maxRows: 3, minRows: 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="itemStatus">
                <template v-solt:read>
                  {{ model.itemStatus }}
                </template>
              </sd-form-item-td>
              <!-- 审计人员 -->
              <sd-form-item-td name="auditorName" label="审计人员">
                <template v-solt:read>
                  {{ model.auditorName }}
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <sd-form-item-td
                name="orgId"
                :hidden="true"
                :colspan="3"
                :input-props="{ defaultValue: '-1' }"
              />
            </tr>
            <!-- <tr>
              <sd-form-item-td name="creatorName" />

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

            <tr
              ><td :colspan="4">
                <sd-form-item name="iamAuditFrameProgrameEntitys" :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
                          v-model="model.iamAuditFrameProgrameEntitys"
                          :read-only="!editable"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.iamAuditFrameProgrameEntitys.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="iamAuditFrameRuleEntitys" :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
                          v-model="model.iamAuditFrameRuleEntitys"
                          :read-only="!editable"
                          label=""
                          :fields="
                            [
                              {
                                caption: '序号',
                                name: 'sortNum',
                                dataType: 'number',
                                attr: {},
                              },
                            ].concat(Array.from(fields.iamAuditFrameRuleEntitys.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 '../../../../matters/audit-matters-select-modal.vue'
import components from './_import-components/iam-audit-frame-item-create-import'
export default {
  name: 'IamAuditFrameItemCreate',
  metaInfo: {
    title: '审计事项',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    auditMattersSelectModal,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      expandC: true,
      expandY: true,
      visible: false,
      orgId: '-1',
      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' },
      ],
    }
  },
  computed: {
    isReadOnly() {
      return this.$route.query.readonly === '1'
    },
  },
  mounted() {
    axios({
      url: `api/xcoa-mobile/v1/iam-audit-frame-item/getAuditOrgId`,
    }).then((res) => {
      if (res.data) {
        this.orgId = res.data + ''
      }
    })
  },
  created() {},
  methods: {
    validator(rule, value, callback) {
      const frameId = this.$route.query.frameId
        ? this.$route.query.frameId
        : this.$refs.docform.getFieldValue('frameId')
      const url = `api/xcoa-mobile/v1/iam-audit-frame-item/check-item-code`
      // if (this.orgId === null) {
      //   url = `api/xcoa-mobile/v1/iam-audit-frame-item/check-item-code?itemCode=${value}&frameId=`
      // }
      const id = this.$route.query.recordId || ''
      debounce(() => {
        const params = {
          itemCode: value,
          frameId: parseFloat(frameId),
          id: id,
        }
        axios({
          method: 'get',
          params: params,
          url: url,
        }).then((res) => {
          if (res?.data === true) {
            callback()
          } else {
            callback('已存在事项编码，不可以重复')
          }
        })
      }, 500)()
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    saved() {
      Message.success({ content: '保存成功！' })
      setTimeout(() => {
        this.close(true)
      }, 800)
    },
    // 保存
    saveForm() {
      this.$refs.docform.saveBtnClick()
    },

    initParam() {
      // 设置父id
      const catalogId = this.$route.query.catalogId
      const catalogName = this.$route.query.catalogName
      const frameId = this.$route.query.frameId
      if (!this.$refs.docform.getFieldValue('catalogId')) {
        this.$refs.docform.setFieldValue('catalogId', parseFloat(catalogId))
        this.$refs.docform.setFieldValue('catalogName', catalogName)
        this.$refs.docform.setFieldValue('frameId', parseFloat(frameId))
      }
    },
    // 序号赋值
    changeNum(data) {
      data.forEach((item, index) => {
        item.sortNum = index + 1
      })
    },

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

      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.lawsTitle = element.docTitle
        mmc.lawsDocNumber = element.dispatchWord ? element.dispatchWord : ''
        mmc.lawsArticle = ''
        iamAuditLaws.push(mmc)
      })
      this.$refs.docform.setFieldValue('iamAuditFrameRuleEntitys', 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 {
    top: 3px;
    z-index: 100;
    margin-left: 10px;
  }
}
</style>
