<template>
  <span :class="$style.remindconfig">
    <audit-form-top-banner :handel-save-form="saveForm" @handelSaveForm="saveForm">
      <sd-detail-form
        v-if="initialled"
        ref="docform"
        :page-id="'onl:66b2a4cdc379410f8364320c0ecac5c9'"
        :record-id="this.id ? this.id : null"
        :read-only="false"
        :class="[$style.form]"
        @close="close(true)"
        @saved="saved"
      >
        <template v-slot="{ formData, model, fields }">
          <table :class="$style.remindTable">
            <p :class="$style.title">提醒设置</p>
            <tr>
              <td colspan="4" :class="$style.subTable">
                <sd-form-by-builder
                  v-if="initialled"
                  :form-data="initDaF(formData.pageFormData)"
                ></sd-form-by-builder>
              </td>
            </tr>
          </table>
        </template>
      </sd-detail-form> </audit-form-top-banner
  ></span>
</template>

<script>
import { message } from '@/common/one-ui'
import auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import cpmService from '../../cpm-service.js'
import components from './_import-components/cpm-improve-remind-config-import'

export default {
  name: 'CpmImproveRemindConfig',
  metaInfo: {
    title: '整改提醒',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      initialled: false,
      id: this.$route.query.id ? this.$route.query.id : '',
      flag: true,
      orgId: '',
    }
  },
  created() {
    const ini = setInterval(() => {
      try {
        cpmService.getRemindConfigId().then((res) => {
          clearInterval(ini)
          if (!res.data.id) {
            this.id = null
          } else {
            this.id = res.data.id
          }
          setTimeout(() => {
            this.initialled = true
            if (this.id === null) {
              // 设置orgId
              cpmService.getCurrentUserGroup().then((res) => {
                this.orgId = res.data
              })
            }
          }, 100)
        })
      } catch {}
    }, 100)
  },
  methods: {
    saved(t) {
      const idobj = t.pageFormData.pageFieldInfos.find((i) => i.name === 'id').value
      this.id = idobj
      const id = this.$route.params.id ? this.$route.params.id : this.$route.query.id
      if (id === null || id === undefined || id === '') {
        let path = this.$route.fullPath
        path = path.replace('?id=&pageId', '?id=' + this.id + '&pageId')
        debugger
        window.location.href = '#' + path
      }
      message.success({ content: '保存成功！' }, 1).then(() => {})
    },
    // 保存
    saveForm() {
      this.$refs.docform.saveBtnClick()
    },
    close(flag) {
      window.close()
    },
    initDaF(array) {
      if (array) {
        array.attrFD.keepHiddenCell = false
      }
      if (this.flag) {
        setTimeout(() => {
          this.$refs.docform.setFieldValue('ZGJH_TXGZSD', 'true')
          this.$refs.docform.setFieldValue('ZGFK_TXGZSD', 'true')
          this.$refs.docform.setFieldValue('ZGCQ_TXGZSD', 'true')
          if (this.id === null) {
            this.$refs.docform.setFieldValue('ORG_ID', this.orgId.id)
            this.$refs.docform.setFieldValue('ORG_NAME', this.orgId.name)
          }
        }, 1000)
        this.flag = false
      }
      return array
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
.remind-table {
  table-layout: auto !important;
  :global(.ant-form-item-label) {
    width: 15% !important;
  }
}
.title {
  font-size: $btn-font-size-sm;
  font-weight: bold;
  text-align: center;
}
.remindconfig {
  :global(.ant-layout) {
    background: #fff;
  }
  :global(.ant-layout-header) {
    background: #fff;
  }
  :global(.close-btn_audit-form-top-banner_product) {
    display: none;
  }
}
.subTable {
  border: none !important;
}
</style>
