<template>
  <div class="change-template-review-content">
    <el-form ref="ruleForm" :key="key" :model="ruleForm" :rules="rules" label-width="150px" label-position="top" class="rule-form-orange">
      <cy-info-title>审批结论</cy-info-title>
      <el-form-item label="审批结论：" prop="approvalConclusion">
        <el-radio-group v-model="ruleForm.approvalConclusion" :disabled="disabled">
          <el-radio label="pass">通过</el-radio>
          <el-radio label="back">退回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="审批意见："
        style="width: 100%;"
        class="textarea-content"
        prop="approvalComments"
        :rules="[
          { required: ruleForm.approvalConclusion === 'back', message: '请输入审批意见', trigger: 'blur' }
        ]"
      >
        <el-input v-model="ruleForm.approvalComments" type="textarea" style="width: 100%;" rows="3" :disabled="disabled" clearable></el-input>
      </el-form-item>
    </el-form>
    <sign-confirmation-content
      v-if="ruleForm.approvalConclusion === 'pass'"
      ref="signConfirmation"
      :sign-confirmation="signConfirmation"
      :sign-disabled="signDisabled"
    />
  </div>
</template>

<script>
import { salesContractChangeApproval } from '@/api/salesContractManagement/salesContractManagement'
export default {
  components: {
    SignConfirmationContent: () => import('./SignConfirmationContent.vue')
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    deatailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      key: 0,
      ruleForm: {
        approvalConclusion: 'pass',
        approvalComments: ''
      },
      rules: {
        approvalConclusion: [
          { required: true, message: '请选择审批结论', trigger: 'change' }
        ]
      },
      signConfirmation: {
        signeListDeatails: [],
        salesContractApprovalFile: [],
        signConfirm: ''
      },
      signDisabled: false
    }
  },
  watch: {
    deatailsInfo: {
      handler(newV) {
        if (Object.keys(newV).length) {
          const { approvalConclusion, approvalComments, signConfirm, files, personInfoHistoryRef } = JSON.parse(JSON.stringify(newV))
          this.ruleForm = {
            approvalConclusion,
            approvalComments
          }
          this.signConfirmation = {
            signeListDeatails: personInfoHistoryRef,
            salesContractApprovalFile: files || [],
            signConfirm
          }
          // const { type } = this.$route.query
          // if (type === 'see') {
          this.signDisabled = true
        // }
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id, taskName } = this.$route.query
      if (taskName === '变更审核') {
        salesContractChangeApproval({ id }).then(({ data }) => {
          this.signConfirmation = {
            signeListDeatails: data.personInfoHistoryRef,
            salesContractApprovalFile: data.files || []
          }
        })
      }
    },
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            const { id, taskId } = this.$route.query
            const { approvalConclusion, approvalComments } = this.ruleForm
            if (approvalConclusion === 'pass') {
              this.$refs.signConfirmation.getParams().then(data => {
                const params = {
                  Loading: true,
                  ...data,
                  businessKey: id,
                  taskId,
                  approvalConclusion,
                  approvalComments
                }
                resolve(params)
              })
            } else {
              const params = {
                Loading: true,
                businessKey: id,
                taskId,
                approvalConclusion,
                approvalComments
              }
              resolve(params)
            }
          } else {
            reject()
          }
        })
      })
    }
  }
}
</script>

<style  scoped>

</style>
