<template>
  <div class="foundation-info">
    <cy-info-title>基础信息</cy-info-title>
    <el-form
      ref="ruleForm"
      :key="key"
      :model="ruleForm"
      :rules="rules"
      class="rule-form-orange"
      label-width="150px"
      label-position="top"
    >
      <el-form-item label="业务编号：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.businessNo"
          placeholder="请输入业务编号"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <el-form-item label="保理合同编号：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.contractNo"
          placeholder="请输入保理合同编号"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <el-form-item label="保理申请人：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.applicantName"
          placeholder="请输入保理申请人"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.applicantSocialCreditCode"
          placeholder="请输入统一社会信用代码"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <el-form-item label="额度支用批次：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.quotaDisburseBatch"
          placeholder="请输入额度支用批次"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <el-form-item label="产品类型：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.productType"
          placeholder="请输入额度支用批次"
          :disabled="disabled || isDisabled"
          clearable
        />
        <!-- <el-select
          v-model="ruleForm.productType"
          placeholder="请选择产品类型"
          clearable
          :disabled="disabled || isDisabled"
          style="width: 100%"
        >
          <el-option
            v-for="item in constant.factoringProducts"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select> -->
      </el-form-item>
      <el-form-item label="项目经理：" prop="enterpriseCustomerInfoId">
        <el-input
          v-model="ruleForm.projectManager"
          placeholder="请输入项目经理"
          :disabled="disabled || isDisabled"
          clearable
        />
      </el-form-item>
      <template v-if="type === 'planChange'">
        <el-form-item label="变更类型" prop="changeType" style="width: 100%; margin-top: 20px">
          <el-radio-group v-if="!disabled" v-model="ruleForm.changeType" :disabled="disabled" class="form-radio-border" style="border-top:1px solid #E8F0F3;">
            <el-radio v-for="item in changeList " :key="item.id" :label="item.id">{{ item.name }}</el-radio>
          </el-radio-group>
          <div v-else class="form-radio-border" style="border-top:1px solid #E8F0F3; line-height: 17px">
            <span> {{ ruleForm.changeType.name }}</span>
          </div>
        </el-form-item>
        <el-form-item v-if="isAcceleration" label="是否为加速到期：" prop="acceleration" style="width: 100%">
          <el-radio-group v-model="ruleForm.acceleration" :disabled="disabled" class="form-radio-border">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="原因说明："
          prop="changeReason"
          style="width: 100%"
          :rules="[
            { required: changeTypeRequired, message: '请输入审批意见', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="ruleForm.changeReason"
            placeholder="请输入原因说明"
            :disabled="disabled"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <cy-upload
            ref="fileStorage"
            :upload-params="{}"
            :file-data="ruleForm.fileStorageList"
            :upload-from-orange-status="true"
            :disabled="disabled"
            placeholder="请上传附件"
          />
        </el-form-item>

      </template>

    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    foundationInfo: {
      type: Object,
      default: () => {}
    },
    // type:  planChange: 计划变更
    type: {
      type: String,
      default: ''
    },
    // 变更类型数据
    changeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      ruleForm: {},
      rules: {
        changeType: [
          { required: true, message: '请选择变更类型', trigger: 'change' }
        ],
        changeReason: [
          { required: true, message: '请输入原因说明', trigger: 'blur' }
        ]
      },
      fileStorageList: [],
      key: 0
    }
  },
  computed: {
    ...mapGetters(['constant']),
    isDisabled() {
      return this.type === 'planChange'
    },
    isAcceleration() {
      if (typeof (this.ruleForm.changeType) === 'object') {
        if (this.ruleForm.changeType.code === 'other_change') {
          return true
        } else {
          return false
        }
      } else {
        const changeTypeCode = this.$utils.codeChangeName(this.changeList, this.ruleForm.changeType, 'id', 'code')
        if (changeTypeCode === 'other_change') {
          return true
        } else {
          return false
        }
      }
    },
    changeTypeRequired() {
      let changeTypeCode = ''
      if (this.disabled) {
        changeTypeCode = this.ruleForm.changeType.code
      } else {
        changeTypeCode = this.$utils.codeChangeName(this.changeList, this.ruleForm.changeType, 'id', 'code')
      }
      if (changeTypeCode === 'normal_change' || changeTypeCode === 'pre_payment') {
        return false
      } else {
        return true
      }
    }
  },
  watch: {
    foundationInfo: {
      handler(newV) {
        if (Object.keys(newV).length) {
          this.ruleForm = JSON.parse(JSON.stringify(newV))
          this.ruleForm = {
            // changeType: '',
            // acceleration: false,
            // changeReason: '',
            ...this.ruleForm
          }
          this.key += 1
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['factoringProducts'])
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            const fileStorage = this.$refs.fileStorage.getFileLists
            if (this.changeTypeRequired && !fileStorage.length) {
              this.$message.warning('请上传附件')
              reject()
              return
            }
            const { id, changeType, changeReason, acceleration } = this.ruleForm
            const params = {
              basic: {
                id
              },
              changeType: {
                id: changeType
              },
              changeReason,
              acceleration,
              fileStorageIdList: fileStorage.map(item => item.id)
            }
            resolve(params)
          } else {
            reject()
            return false
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
