<template>
  <div class="approval-button-item sava-button">
    <el-button :disabled="disabled" :size="btnSize" @click="handleSavaDraft">
      {{ name }}
    </el-button>
    <el-drawer
      :title="$t('task.SaveSettings')"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :modal="false"
      :close-on-click-modal="false"
      :size="380"
      :wrapper-closable="false"
      class="right-drawer-wrap"
      :class="{
        'dialog-model-drawer': isDialog && !isFromTaskManager,
        'horizontal-layout-drawer': layout === 'horizontal',
        'flow-module-drawer': isFromApplicationModule,
      }"
      append-to-body
    >
      <div class="dialog__content">
        <el-form
          v-form
          :model="formData"
          label-width="100px"
          data-vv-scope="savaForm"
        >
          <ht-form-item :label="$t('task.SaveType')">
            <ht-radio
              v-model="savaType"
              validate="required"
              :rdlist="JSON.stringify(savaTypeOptions)"
              @change="handleSavaTypeChange"
            ></ht-radio>
          </ht-form-item>
          <ht-form-item
            v-show="savaType === savaFormAndOpinion"
            :label="$t('task.SaveOpinion')"
          >
            <approval-comments
              :approval-opinion="approvalOpinion"
              @get-opinion="getOpinion"
            ></approval-comments>
          </ht-form-item>
        </el-form>
      </div>
      <div class="dialog-footer__btns">
        <el-button
          type="primary"
          :loading="loading"
          :disabled="isDisabledSaveButton"
          @click="confirm"
        >
          {{ $t('Common.Confirm') }}
        </el-button>
        <el-button @click="handleClose">{{ $t('Common.Cancel') }}</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import approvalButton from '@/mixins/approvalButton'
  import { savaTaskDraft } from '@/api/process'
  import { savaApprovalComments } from '@/api/flow'
  import { resultJudge } from '@/utils/event.js'
  const SAVA_TYPE_OPTIONS = [
    {
      key: '1',
      value: '仅保存表单数据',
    },
    {
      key: '2',
      value: '保存表单数据和审批意见',
    },
  ]
  const ONLY_SAVA_FORM = '1'
  const SAVA_FORM_OPINION = '2'
  export default {
    name: 'SavaButton',
    mixins: [approvalButton],
    props: {
      beforeClick: {
        type: Function,
        default: null,
      },
    },
    data() {
      return {
        savaTypeOptions: SAVA_TYPE_OPTIONS,
        savaFormAndOpinion: SAVA_FORM_OPINION,
        savaType: ONLY_SAVA_FORM,
        loading: false,
        formData: {},
        isDisabledSaveButton: false,
        timer: '',
      }
    },
    methods: {
      handleSavaTypeChange(val) {
        if (val === this.savaFormAndOpinion) {
          this.getApprovalOpinionByInstIdAndTaskId()
        }
      },
      async handleSavaDraft() {
        //表单校验
        let res = true
        if (this.beforeClick) {
          res = await resultJudge(this.beforeClick(this.alias))
        }
        if (!res) return
        this.getTaskIsLock('savaDraft')
      },
      enableSaveButton() {
        clearTimeout(this.timer)
        this.isDisabledSaveButton = false
      },
      confirm() {
        this.isDisabledSaveButton = true
        setTimeout(this.enableSaveButton, 1000)
        const params = {
          ...this.getSubmitCommonData(),
          defId: this.bpmTask.procDefId,
          approval: true,
          formKey: this.formKey,
        }
        if (this.dialogBeforeClick) {
          resultJudge(this.dialogBeforeClick(this.alias)).then((result) => {
            if (result) {
              this.loading = true
              this.submitBefore('saveDraft', params)
            } else {
              this.loading = false
            }
          })
        } else {
          this.submitBefore('saveDraft', params)
        }
        this.submitAfter('saveDraft')
      },
      onSubmit(params) {
        //删除多余参数
        if (params.hasOwnProperty('defId')) {
          delete params.defId
        }
        if (params.hasOwnProperty('approval')) {
          delete params.approval
        }
        savaTaskDraft(params)
          .then((res) => {
            if (res.state) {
              // 保存版本号自增1
              Object.keys(this.data).forEach((key) => {
                if (
                  this.data[key].form_data_rev_ ||
                  this.data[key].form_data_rev_ === 0
                ) {
                  this.data[key].form_data_rev_++
                }
              })
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
            if (this.savaType === this.savaFormAndOpinion) {
              this.temporaryApprovalComments()
            }
            this.dialogAfterClick(this.alias)
          })
          .finally(() => {
            this.loading = false
            this.handleClose()
          })
      },
      //暂存审批意见
      temporaryApprovalComments() {
        const params = {
          procInstId: this.bpmTask.procInstId, //实例Id
          taskId: this.taskId,
          opinion: this.opinionValue,
        }
        savaApprovalComments(params).then((res) => {
          if (res.state) {
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped></style>
