<template>
  <div>
    <a-modal
      v-if="showOption"
      :visible="showOption"
      :confirm-loading="confirmLoading"
      title="修改意见"
      :width="650"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form-model
        :model="form"
        :class="$style.modifyForm"
        :label-col="{ span: 3 }"
        :wrapper-col="{ span: 20 }"
      >
        <a-form-model-item label="意见">
          <SdOpinionModel
            ref="opinionModel"
            v-model="form.option"
            :group-id="groupId"
            :hand-write="false"
            :cyy-popup-container="cyyPopupContainer"
            v-on="$listeners"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import WorkbenchService from '@/workbench/workbench-service'
import { message } from 'ant-design-vue'
import SdOpinionModel from '@/webflow/sd-opinion-model.vue'
import components from './_import-components/sd-modify-submit-import'

export default {
  name: 'SdModifySubmit',
  components: {
    ...components,
    SdOpinionModel,
  },
  props: {
    optionRecode: {
      type: Object,
      default: undefined,
    },
    showOption: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      form: {
        option: this.optionRecode !== undefined ? this.optionRecode.opinion : '',
        attachment: [],
      },
      // isShow: this.showOption,
      // 如果附件Id为空的话则将groupId先默认为空
      groupId: this.optionRecode.processLogAttachments[0].groupCode,
      confirmLoading: false,
    }
  },
  methods: {
    cyyPopupContainer() {
      return document.body
    },
    handleOk() {
      // 意见有修改（只判断意见文本，附件更新时已实时反馈），调用更新接口
      if (this.optionRecode?.opinion !== this.form.option) {
        this.confirmLoading = true
        const submitParams = {
          id: this.optionRecode.props.opinionId,
          opinionType: this.optionRecode.actionId === 'handover' ? 1 : 0,
          content: {
            signContent: this.form.option,
            attrs: {
              attachment: this.groupId,
            },
          },
        }
        // 修改意见后进行数据绑定
        WorkbenchService.updateOptions(submitParams).then((res) => {
          message.success('保存成功')
          this.confirmLoading = false
          this.form.option = res.content.signContent
          res.isOptionShow = false
          res.updateDataFlag = true
          this.$emit('signStatusVerdict', res)
        })
      } else {
        // 当前未修改意见文本，直接关闭弹框即可
        this.handleCancel()
      }
    },
    handleCancel() {
      this.$emit('optionCancle', false)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.modify-form {
  :global(textarea.ant-input) {
    margin-bottom: 0;
  }
}
</style>
