<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :handel-publish="handelPublish"
      :form-data="mode"
      @handelSaveForm="saveForm"
      @handelPublish="handelPublish"
    >
      <div class="ant-card ant-card-small">
        <div :class="$style.antCardBody">
          <sd-detail-form
            ref="docform"
            page-id="audit/spicproblem/spicProblem"
            form-id="spicProblem"
            :record-id="this.$route.query.id ? parseInt(this.$route.query.id) : null"
            :class="[$style.form]"
            :read-only="this.mode === 'VIEW'"
            @close="close(true)"
            @saved="saved"
            @sdFormReady="sdFormReady"
          >
            <template v-slot="{ model, fields }">
              <table>
                <tr>
                  <td>
                    <audit-advanced-group
                      :expand="basicExpand"
                      :expand-str="'basicExpand'"
                      :group-label="'基本信息'"
                      tablestyle="''"
                      @changedClick="changedClick"
                    >
                    </audit-advanced-group>
                  </td>
                </tr>
              </table>

              <table v-show="basicExpand" :class="$style.table">
                <colgroup>
                  <col :class="$style.label" />
                  <col :class="$style.input" />
                  <col :class="$style.label" />
                  <col :class="$style.input" />
                </colgroup>
                <tr>
                  <sd-form-item-td name="docTitle" :label="'案例名称'" />
                  <sd-form-item-td name="problemNumber" :label="'案例编号'">
                    <template v-slot>
                      <a-input
                        v-model="model.problemNumber"
                        placeholder="自动生成"
                        read-only
                      ></a-input> </template
                  ></sd-form-item-td>
                </tr>
                <tr>
                  <sd-form-item-td name="problemDesc" :colspan="3" :label="'描述'">
                    <a-textarea
                      v-model="model.problemDesc"
                      placeholder="请输入"
                      :auto-size="{ minRows: 3, maxRows: 5 }"
                    />
                  </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">
                    <a-textarea v-model="model.suggestion" :rows="3" />
                  </sd-form-item-td>
                </tr>
                <tr>
                  <!-- 作者 -->
                  <sd-form-item-td name="creatorName" />
                  <!-- 所属部门 -->
                  <sd-form-item-td name="creationTime" />
                  <sd-form-item-td v-if="initParam('parentId')" :hidden="true" name="parentId" />
                </tr>
                <tr>
                  <!-- 附件 -->
                  <sd-form-item-td name="attach" :colspan="3">
                    <template v-slot:read-and-edit="{ editable }">
                      <sd-attachment
                        v-model="model.attach"
                        :group-id="JSON.parse(fields.attach.value).value"
                        :read-only="!editable"
                      ></sd-attachment>
                    </template>
                  </sd-form-item-td>
                </tr>
                <tr>
                  <sd-form-item-td name="state" :hidden="true" />
                </tr>
              </table>
            </template>
          </sd-detail-form>
        </div>
      </div>
    </audit-form-top-banner>
    <audit-matters-select-modal
      :visible="visible"
      :org-id="orgId"
      @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 auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import auditAdvancedGroup from '@product/iam/components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '@product/iam/components/audit-advanced-group-mixins'
import auditMattersSelectModal from '@product/iam/audit/matters/audit-matters-select-modal.vue'
import XmChildTable from '@product/iam/components/xm-child-table.vue'
import problemService from './xm-spic-problem-service.js'
import components from './_import-components/xm-spic-problem-form-import'

export default {
  name: 'XmSpicProblemForm',
  metaInfo: {
    title: '审计案例',
  },
  components: {
    auditFormTopBanner,
    ...components,
    auditAdvancedGroup,
    auditMattersSelectModal,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      isHidden: true,
      publish: 0, // 为1 时是发布
      defaultCategory: '',
      labelArray: [],
      mode: null,
      publishFlag: false,
      basicExpand: true,
      visible: false,
      CheckExpand: true,
      orgId: null,
      checkChildColumns: [
        { dataIndex: 'lawsLibraryId', sdHidden: true },
        { dataIndex: 'lawsType', sdHidden: true },
        { dataIndex: 'lawsTitle', sdHidden: false, width: '40%' },
        { dataIndex: 'lawsDocNumber', sdHidden: false, width: '15%' },
        { dataIndex: 'lawsChapter', sdHidden: false },
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
      ],
    }
  },
  created() {},
  mounted() {},
  methods: {
    changeNum(data) {
      data.forEach((item, index) => {
        item.sortNum = index + 1
      })
    },
    handleBeforeAdd(val, callback) {
      const fileCode = this.$refs.docform.getFieldValue('problemNumber')
      if (fileCode && fileCode !== '') {
        callback(false)
      } else {
        // 生成案例编号
        problemService.getFileCode().then((res) => {
          this.$refs.docform.setFieldValue('problemNumber', res.data)
          callback(false)
        })
      }
    },
    // 页面加载完成后
    sdFormReady() {
      debugger
      // this.mode = this.$refs.docform.formData.mode
      this.mode = this.$route.query.mode
    },
    handleChange(model, event) {
      model.label = event.join('、')
      this.labelArray = [...event]
    },
    handelPublish() {
      this.handleBeforeAdd(null, (res) => {
        this.$refs.docform.validateFields().then(() => {
          Modal.confirm({
            title: '提示',
            content: '请确认是否发布当前数据',
            onOk: () => {
              // 发布，保存
              this.$refs.docform.setFieldValue('state', '已发布')
              this.publish = 1
              this.saveForm()
              Message.success('发布成功', 1).then((res) => {
                this.close(true)
              })
            },
            onCancel: () => {},
          })
        })
      })
      // this.$refs.docform.validateFields().then(() => {
      //   Modal.confirm({
      //     title: '提示',
      //     content: '请确认是否发布当前数据',
      //     onOk: () => {
      //       // 发布，保存
      //       this.$refs.docform.setFieldValue('state', '已发布')
      //       this.publish = 1
      //       this.saveForm()
      //       Message.success('发布成功', 1).then((res) => {
      //         this.close(true)
      //       })
      //     },
      //     onCancel: () => {},
      //   })
      // })
    },
    saveForm() {
      this.handleBeforeAdd(null, (res) => {
        this.$refs.docform.saveBtnClick()
      })
      return true
    },
    saved() {
      Message.success({ content: '保存成功！' }).then(() => {
        this.close(true)
      })
    },
    initParam(val) {
      setTimeout(() => {
        var flow = this.$refs.docform
        if (
          flow.getFieldValue('parentId') === null ||
          flow.getFieldValue('parentId') === undefined ||
          flow.getFieldValue('parentId') === ''
        ) {
          if (this.$route.query?.typeId) {
            this.$refs.docform.setFieldValue('parentId', this.$route.query.typeId)
          }
        }
      }, 0)
      return true
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.path, flag)
      window.close()
    },
    validForm() {
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },

    // 选择法律制度
    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.lawsChapter = ''
        mmc.lawsArticle = ''
        iamAuditLaws.push(mmc)
      })
      this.$refs.docform.setFieldValue('iamAuditLawsRelEntitys', iamAuditLaws)
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';

.ant-card-body {
  padding: 12px;

  :global(.buttons_sd-detail-form_common) {
    :global(.ant-form-item-control-wrapper) {
      padding-top: 4%;
      padding-left: 24%;
    }
  }

  .tr_border {
    box-sizing: border-box;
    border-right: 1px solid #e8e8e8;
    border-left: 1px solid #e8e8e8;
  }

  :global(.title_sd-detail-form_common) {
    font-size: 1.5em;
    text-align: center;
  }
}
.btnselect {
  position: relative;
  top: 4px;
  right: 200px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
</style>
