<script name="fundsInfo" setup>
import { ref } from 'vue'
defineProps({
  detailData: {
    type: Object,
    default: () => {
      return {}
    }
  }
})
const formData = ref({
  }),
  formRef = ref(),
  moneyValidator = (rule, value, callback) => {
    if (!value) callback()
    if (!/^\d+(\.\d{1,6})?$/.test(value)) {
      callback('请输入正确格式，最多保留六位小数')
    } else if (value * 1 >= 100000000) {
      callback('请输入正确格式，小于100000000')
    } else {
      callback()
    }
  },
  rules = {
    projectYear: [{ required: true, message: '请选择预算年度' }],
    declareAmount: [{ required: true, message: '请输入申报金额', trigger: 'blur' }, { validator: moneyValidator, trigger: 'blur' }],
    declareHaveAmount: [{ required: true, message: '请输入自有资金', trigger: 'blur' }, { validator: moneyValidator, trigger: 'blur' }],
    declareGovOwnFinanceAmount: [
      {
        required: true,
        message: '请输入政府投资-本级财政资金',
        trigger: 'blur'
      },
      { validator: moneyValidator, trigger: 'blur' }
    ],
    declareGovSuperiorFinanceAmount: [
      {
        required: true,
        message: '请输入政府投资-上级补助资金',
        trigger: 'blur'
      },
      { validator: moneyValidator, trigger: 'blur' }
    ],
    declareBankLendingAmount: [{ required: true, message: '请输入银行贷款', trigger: 'blur' }, { validator: moneyValidator, trigger: 'blur' }],
    declareOtherAmount: [{ required: true, message: '请输入其他资金', trigger: 'blur' }, { validator: moneyValidator, trigger: 'blur' }],
    consultancy: [{ required: true, message: '请填写咨询公司' }],
    baseBasisAmountOri: [{ required: true, message: '请填写预算来源说明' }]
  },
  // 校验
  validForm = (callback) => {
    formRef.value.validate(valid => {
      callback(valid)
    })
  },
  // 回显
  setFormData = (data) => {
    formData.value = {
      projectYear: data.projectYear ? data.projectYear + '' : null,
      declareAmount: data.declareAmount,
      declareHaveAmount: data.declareHaveAmount,
      declareGovOwnFinanceAmount: data.declareGovOwnFinanceAmount,
      declareGovSuperiorFinanceAmount: data.declareGovSuperiorFinanceAmount,
      declareBankLendingAmount: data.declareBankLendingAmount,
      declareOtherAmount: data.declareOtherAmount,
      baseBasisAmountOri: data.baseBasisAmountOri || '',
      consultancy: data.consultancy || ''
    }
  }
defineExpose({ validForm, formData, setFormData })
</script>

<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="rules"
    label-position="right"
    label-width="180px"
    label-suffix=":"
    scroll-to-error
  >
    <el-row :gutter="40">
      <el-col :span="12">
        <el-form-item label="预算年度" prop="projectYear">
          <el-date-picker
            v-model="formData.projectYear"
            :disabled="$route.name==='declarePlan'||[10012,10013,10016].includes(detailData?.status)"
            type="year"
            placeholder="请选择"
            format="YYYY"
            value-format="YYYY"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="申报金额" prop="declareAmount">
          <el-input-number
            v-model="formData.declareAmount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            @mousewheel.prevent
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="自有资金" prop="declareHaveAmount">
          <el-input-number
            v-model="formData.declareHaveAmount"
            placeholder="请填写"
            :min="0"
            :controls="false"
            @mousewheel.prevent
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="政府投资-本级财政资金" prop="declareGovOwnFinanceAmount">
          <el-input-number
            v-model="formData.declareGovOwnFinanceAmount"
            placeholder="请填写"
            :min="0"
            :controls="false"
            @mousewheel.prevent
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="政府投资-上级补助资金" prop="declareGovSuperiorFinanceAmount">
          <el-input-number
            v-model="formData.declareGovSuperiorFinanceAmount"
            placeholder="请填写"
            :min="0"
            :controls="false"
            @mousewheel.prevent
          ><template #append>万元</template></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="银行贷款" prop="declareBankLendingAmount">
          <el-input-number
            v-model="formData.declareBankLendingAmount"
            placeholder="请填写"
            :min="0"
            :controls="false"
            @mousewheel.prevent
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="其他资金" prop="declareOtherAmount">
          <el-input-number
            v-model="formData.declareOtherAmount"
            placeholder="请填写"
            :min="0"
            :controls="false"
            @mousewheel.prevent
          />
        </el-form-item>
      </el-col>
      <el-col v-if="($route.name==='declarePlan'||[10012,10013,10016].includes(detailData?.status))&&formData.declareAmount>400" :span="12">
        <el-form-item label="咨询公司" prop="consultancy">
          <el-input
            v-model="formData.consultancy"
            placeholder="请填写"
            maxlength="100"
          />
        </el-form-item>
      </el-col>
      <el-col v-if="formData.declareOtherAmount>0" :span="24">
        <el-form-item label="预算来源说明" prop="baseBasisAmountOri">
          <el-input
            v-model="formData.baseBasisAmountOri"
            type="textarea"
            show-word-limit
            placeholder="请填写"
            :maxlength="150"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<style lang="less">
.el-upload-list{
  width: 100%;
}
</style>
