<template>
  <audit-form-top-banner
    :handel-publish="publishFlag ? handelPublish : null"
    :handel-save-form="saveFlag ? saveForm : null"
    @handelSaveForm="saveForm"
    @handelPublish="handelPublish"
  >
    <sd-detail-form
      ref="docform"
      page-id="core/law/iamLaw"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="!saveFlag"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ model, fields }">
        <table
          ><tr
            ><td style="padding-left:5px;border:none">
              <audit-advanced-group
                :expand="basicExpand"
                :expand-str="'basicExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group> </td></tr
        ></table>

        <table v-show="basicExpand">
          <!-- <tr>
            <sd-form-item-td name="projectTitle">
              <span>{{ model.projectTitle }}</span>
            </sd-form-item-td>
            <sd-form-item-td name="auditType">
              <span>{{
                fields.auditType.attr.selectListItem.find((item) => {
                  return item.value === model.auditType
                }).label
              }}</span>
            </sd-form-item-td>
          </tr> -->
          <tr>
            <!-- 标题 -->
            <sd-form-item-td name="docTitle" />
            <sd-form-item-td :hidden="true" name="parentId" />
            <!-- 发布单位 -->
            <sd-form-item-td name="publishUnit" />
          </tr>
          <tr>
            <!-- 发文字号 -->
            <sd-form-item-td name="dispatchWord" />
            <!-- 发布日期 -->
            <sd-form-item-td name="publishTime">
              <a-date-picker v-model="model.publishTime" style="width:100%;" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 实施日期 -->
            <sd-form-item-td name="implementTime">
              <a-date-picker v-model="model.implementTime" style="width:100%;" />
            </sd-form-item-td>
            <!-- 时效性 -->
            <sd-form-item-td name="period" @change="changeAbolishTime(model)" />
          </tr>
          <tr>
            <!-- 效力级别 -->
            <sd-form-item-td v-if="initParam(model)" name="devoteLevel" />
            <!-- 废止日期 model.devoteLevel='02' -->
            <sd-form-item-td name="abolishTime">
              <a-date-picker
                v-model="model.abolishTime"
                style="width:100%;"
                :disabled="model.period !== '02'"
              />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 内容 -->
            <!-- <sd-quill-editor v-model="model.content" /> -->
            <sd-form-item-td name="content" :colspan="3" />
          </tr>

          <tr>
            <!-- 标签 -->
            <sd-form-item-td name="label" :colspan="3">
              <a-select
                v-model="labelArray"
                :dropdown-class-name="$style.labelArray"
                mode="tags"
                @change="(evt) => handleChange(model, evt)"
              >
              </a-select>
            </sd-form-item-td>
          </tr>

          <tr>
            <!-- 维护人员 -->
            <sd-form-item-td name="maintePerson" />
            <!-- 维护日期 -->
            <sd-form-item-td name="mainteTime" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3">
              <template v-slot:read-and-edit="{ editablelaw }">
                <sd-attachment
                  v-model="model.attachment"
                  :group-id="JSON.parse(fields.attachment.value).value"
                  :read-only="!saveFlag"
                />
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 备注 -->
            <sd-form-item-td name="remark" :colspan="3">
              <a-textarea v-model="model.remark" :rows="3" />
            </sd-form-item-td>
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { getUserInfo } from '@/common/store-mixin'
import { Message } from 'ant-design-vue'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import components from './_import-components/iam-audit-auditsource-law-form-import'

export default {
  name: 'IamAuditAuditsourceLawForm',
  metaInfo: {
    title: '法律法规',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    // SdDetailForm,
    // sd-form-item-td,
    // SdQuillEditor,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      oldData: '',
      currentUser: '',
      whether: 1,
      basicExpand: true,
      saveFlag: false,
      publishFlag: false,
      labelArray: [],
      oper: '',
    }
  },
  mounted() {
    this.currentUser = getUserInfo().account
  },
  methods: {
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    changeAbolishTime(model) {
      // 如果不是失效状态，那么清除废止日期信息
      if (model.period !== '02') {
        model.abolishTime = undefined
      }
    },
    handleChange(model, event) {
      const arrLen = event.length
      if (arrLen > 0) {
        if (event[arrLen - 1].includes(',')) {
          Message.error(`标签不能包含英文逗号`)
          event.pop()
        }
        model.label = event.join(',')
        model.label = ',' + model.label + ','
        this.labelArray = [...event]
      } else {
        model.label = ''
      }
    },
    // initParam(model) {
    //   const value = model.label
    //   this.whether = model.whether
    //   if (model.whether === 1) {
    //     // 未发布的才有发布按钮
    //     this.publishFlag = true
    //   }
    //   if (model.whether === 0) {
    //     this.saveFlag = false
    //   } else {
    //     this.saveFlag = true
    //   }

    //   if (this.currentUser === model.creatorAccount) {
    //     this.publishFlag = true
    //   } else {
    //     this.publishFlag = false
    //   }
    //   if (model.creatorAccount === undefined) {
    //     // 新数据
    //     this.oldData = false
    //     this.publishFlag = true
    //   }
    //   if (this.$refs.docform.formData.mode === 'VIEW') {
    //     this.publishFlag = false
    //     this.saveFlag = false
    //   }
    //   if (value && value.length > 0) {
    //     setTimeout(() => {
    //       if (this.labelArray.length === 0) {
    //         if (value && value !== '' && value.length > 0) {
    //           const split = value.split(',')
    //           for (const item of split) {
    //             if (item && item.length > 0) {
    //               this.labelArray.push(item)
    //             }
    //           }
    //         }
    //       }
    //     }, 0)
    //   }
    //   // 设置父id
    //   const parentId = this.$route.query.parentId
    //   if (parentId) {
    //     model.parentId = Number(parentId)
    //   }
    //   return true
    // },
    // 保存
    initParam(model) {
      const value = model.label
      if (model.creatorAccount === undefined || this.currentUser === model.creatorAccount) {
        this.publishFlag = true
        this.saveFlag = true
      } else {
        this.publishFlag = false
        this.saveFlag = false
        this.whether = 0
      }

      if (model.creatorAccount === undefined) {
        this.oldData = false
      }

      if (model.whether === 0) {
        this.publishFlag = false
      }

      if (value && value.length > 0) {
        setTimeout(() => {
          const newLable = []
          if (this.labelArray.length === 0) {
            if (value && value !== '' && value.length > 0) {
              const split = value.split(',')

              for (const item of split) {
                if (item && item.length > 0) {
                  this.labelArray.push(item)
                  newLable.push(item)
                }
              }
              model.label = newLable.join(',')
            }
          }
        }, 0)
      }
      // 设置父id
      const parentId = this.$route.query.parentId
      if (parentId) {
        model.parentId = Number(parentId)
      }
      return true
    },
    saveForm() {
      this.$refs.docform.saveBtnClick()
      this.oper = '保存'
      // Message.success('保存成功')
      // this.$refs.docform.getData()
      // this.close()
    },
    // handelPublish() {
    //   // 发布，保存
    //   const wether = this.$refs.docform.getFieldValue('whether')
    //   if (wether === 0) {
    //     this.whether = 1
    //     this.$refs.docform.setFieldsValue({ whether: 1 })
    //     this.$refs.docform.saveBtnClick()
    //     crossWindowWatcher.notifyChange(true)
    //     Message.success('撤销发布成功')
    //   } else {
    //     this.whether = 0
    //     this.$refs.docform.setFieldsValue({ whether: 0 })
    //     this.$refs.docform.saveBtnClick()
    //     crossWindowWatcher.notifyChange(true)
    //     Message.success('发布成功')
    //   }
    // },
    handelPublish() {
      // 发布，保存
      this.$refs.docform.validateFields().then(() => {
        this.$refs.docform.setFieldsValue({ whether: 0 })
        this.$refs.docform.saveBtnClick()
        this.oper = '发布'
        this.oldData = false
      })
    },
    saved() {
      Message.success(this.oper + '成功', 1)
      if (this.oldData === false) {
        setTimeout(() => {
          this.close(true)
        }, 1000)
        return
      }
      this.$refs.docform.getData()
    },
  },
}
</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;
  }
}
.label-array {
  display: none;
}
</style>
