<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :handel-publish="handelPublish"
      :form-data="mode"
      :hiden-or-show="hidenOR0Show"
      @handelSaveForm="saveForm"
      @handelPublish="handelPublish"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/spiccompositedatabase/spicCompositeDatabasePublish"
        :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
        :class="$style.form"
        :read-only="readOnly"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="sdFormReady"
      >
        <template v-slot="{ model, fields }">
          <table>
            <tr>
              <sd-form-item-td name="title" />
              <sd-form-item-td name="reportYear">
                <a-date-picker
                  v-model="model.reportYear"
                  mode="year"
                  picker="YYYY"
                  format="YYYY"
                  :allow-clear="false"
                  placeholder="选择年度"
                  :input-read-only="true"
                  :open="endOpen"
                  :value="year"
                  @panelChange="yearChange"
                  @openChange="handleEndOpenChange"
                ></a-date-picker>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 上报单位 -->
              <sd-form-item-td name="reportUnitCode" />
              <!-- 在线反馈截至时间 -->
              <sd-form-item-td name="endTime" />
            </tr>
            <!-- 附件 -->
            <!-- <tr>
              <sd-form-item-td name="attachment" :colspan="3" />
            </tr> -->
            <tr>
              <!-- 参考模板 -->
              <sd-form-item-td name="referenceTemplate" :colspan="3" />
            </tr>
            <tr>
              <!-- 资料描述 -->
              <sd-form-item-td name="infoDesc" :colspan="3">
                <a-textarea v-model="model.infoDesc" :rowspan="4" />
              </sd-form-item-td>
            </tr>
            <tr>
              <sd-form-item-td name="remark" :colspan="3">
                <a-textarea v-model="model.remark" :rowspan="3" />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 查看人员 -->
              <sd-form-item-td name="viewRanges" :colspan="3" />
            </tr>
            <tr>
              <!-- 编制人员 -->
              <sd-form-item-td name="creatorName" />
              <!-- 编制日期 -->
              <sd-form-item-td name="creationTime" />
            </tr>
            <tr>
              <sd-form-item-td name="uuid" hidden="true" />
            </tr>
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
  </span>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedGroupMixins from '@product/iam/components/audit-advanced-group-mixins'
import components from './_import-components/xm-composite-database-publish-form-import'

export default {
  name: 'XmCompositeDatabasePublishForm',
  metaInfo() {
    return {
      title: '综合资料下发',
    }
  },
  components: { auditFormTopBanner, ...components },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      endOpen: false,
      year: null,
      mode: null,
      hidenOR0Show: true,
      readOnly: false,
      publish: 0, // 为1 时是发布
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 页面加载完成后
    sdFormReady() {
      const ini = setInterval(() => {
        if (
          this.$refs.docform &&
          (this.$refs.docform.$refs.form || this.$refs.docform.$refs.sdForm)
        ) {
          clearInterval(ini)
          this.mode = this.$refs.docform.formData.mode
          if (this.$refs.docform.formData.pageFormData.beanId) {
            const docStatus = this.$refs.docform.getFieldValue('docStatus')
            if (docStatus === '已发布') {
              this.hidenOR0Show = false
              this.readOnly = true
              this.mode = 'VIEW'
            } else {
              const creatorAccount = this.$refs.docform.getFieldValue('creatorAccount')
              if (creatorAccount !== getUserInfo().account) {
                this.hidenOR0Show = false
                this.readOnly = true
                this.mode = 'VIEW'
              }
            }
          }
        }
      }, 100)
      axios({
        url: 'api/xcoa-mobile/v1/spicannualreportpublish/createSpicAnnualUuid',
        method: 'post',
      }).then((res) => {
        const uuid = res.data
        this.$refs.docform.setFieldValue('uuid', uuid)
      })
    },
    yearChange(value) {
      this.year = value
      this.$refs.docform.setFieldValue('reportYear', value.format('YYYY'))
      this.$refs.docform.validateField('reportYear').then((res) => {
        console.log(res)
      })
      if (document.getElementsByClassName('ant-calendar-picker-container').length > 0) {
        document.getElementsByClassName('ant-calendar-picker-container')[0].style.display = 'none'
      }
    },
    handleEndOpenChange(open) {
      this.endOpen = open
    },
    handelPublish() {
      this.$refs.docform.validateFields().then(() => {
        Modal.confirm({
          title: '提示',
          content: '请确认是否发布当前数据',
          onOk: () => {
            // 发布，保存
            this.$refs.docform.setFieldValue('docStatus', '已发布')
            this.publish = 1
            this.$refs.docform.save().then((res) => {
              const id = this.$refs.docform.getFieldValue('id')
              const uuid = this.$refs.docform.getFieldValue('uuid')
              axios({
                url:
                  'api/xcoa-mobile/v1/spiccompositedatabasepublish/createSpicCompositeDatabaseFlow',
                method: 'post',
                data: { uuid: uuid },
              })
                .then((res) => {
                  if (res.data.isRequestSuccess === 'false') {
                    Message.error('年度资料下发失败，请联系系统管理员')
                  } else {
                    Message.success('发布成功', 1).then((res) => {
                      this.close(true)
                    })
                  }
                })
                .catch(() => {
                  Message.error('服务异常')
                })
            })
          },
          onCancel: () => {},
        })
      })
    },
    // 保存
    saveForm() {
      // debugger
      if (this.publish !== 1) {
        this.$refs.docform.setFieldValue('docStatus', '起草中')
      }
      this.$refs.docform.saveBtnClick()
    },
    saved() {
      if (this.publish === 1) {
        Message.success('发布成功', 1).then((res) => {
          this.close(true)
        })
      } else {
        Message.success('保存成功', 1).then((res) => {
          this.close(true)
        })
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.path, flag)
      window.close()
    },
  },
}
</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>
