<template>
  <div class="disburse-approval-content">
    <cy-info-title>条件</cy-info-title>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" class="rule-form-orange">
      <el-form-item label="单笔指导性单笔业务满足条件" style="width: 100%">
        <cy-comm-table
          :columns="columns"
          :data="generalConditionList"
          :count="0"
        />
      </el-form-item>
      <el-form-item label="授信决议放款条件" style="width: 100%">
        <cy-comm-table
          :columns="columns"
          :data="meetingLoanConditionList"
          :count="0"
        />
      </el-form-item>
      <cy-info-title>放款条件</cy-info-title>
      <el-form-item label="授信决议放款条件" style="width: 100%">
        <cy-comm-table
          :columns="loanColumns"
          :data="loanConditionList"
          :count="0"
        />
      </el-form-item>
      <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 label="veto">否决</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" :disabled="disabled" type="textarea" style="width: 100%;" rows="3" clearable></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        approvalConclusion: 'pass',
        approvalComments: ''
      },
      rules: {
        approvalConclusion: [
          { required: true, message: '请选择审批结论', trigger: 'change' }
        ]
      },
      generalConditionList: [],
      meetingLoanConditionList: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '条件',
          prop: 'content'
        }
      ],
      loanConditionList: [],
      loanColumns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '放款日期',
          prop: 'loanDate'
        },
        {
          label: '金额（CNY）',
          prop: 'amount'
        },
        {
          label: '放款条件',
          prop: 'loanCondition',
          ruleRequired: true,
          render: (h, row) => {
            return <el-input v-model={row.loanCondition} disabled={this.disabled} placeholder='请输入放款条件'/>
          }
        }
      ]
    }
  },
  watch: {
    detailsInfo: {
      handler(newV) {
        if (newV === undefined) return
        const { generalConditionList, meetingLoanConditionList, loanConditionList, approvalConclusion, approvalComments } = JSON.parse(JSON.stringify(newV))
        this.generalConditionList = generalConditionList || []
        this.meetingLoanConditionList = meetingLoanConditionList  || []
        this.loanConditionList = loanConditionList  || []
        this.ruleForm.approvalConclusion = approvalConclusion
        this.ruleForm.approvalComments = approvalComments
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            for (let i = 0; i < this.loanConditionList.length; i++) {
              const { loanCondition } = this.loanConditionList[i]
              if (!loanCondition) {
                this.$message.warning('请输入放款条件')
                return
              }
            }
            const parmas = {
              ...this.ruleForm,
              loanConditionList: this.loanConditionList
            }
            resolve(parmas)
          } else {
            reject()
            return false
          }
        })
      })
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
