<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="项目id" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目id" />
      </el-form-item>
      <el-form-item label="合同名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入合同名称" />
      </el-form-item>
      <el-form-item label="合同类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择合同类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户公司id" prop="customerCompanyId">
        <el-input v-model="formData.customerCompanyId" placeholder="请输入客户公司id" />
      </el-form-item>
      <el-form-item label="合同进展" prop="progress">
        <el-input v-model="formData.progress" placeholder="请输入合同进展" />
      </el-form-item>
      <el-form-item label="预计签订时间" prop="expectedTime">
        <el-date-picker
          v-model="formData.expectedTime"
          type="date"
          value-format="x"
          placeholder="选择预计签订时间"
        />
      </el-form-item>
      <el-form-item label="签订时间" prop="signingTime">
        <el-date-picker
          v-model="formData.signingTime"
          type="date"
          value-format="x"
          placeholder="选择签订时间"
        />
      </el-form-item>
      <el-form-item label="归档时间" prop="archiveTime">
        <el-date-picker
          v-model="formData.archiveTime"
          type="date"
          value-format="x"
          placeholder="选择归档时间"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="合同金额" prop="amount">
        <el-input v-model="formData.amount" placeholder="请输入合同金额" />
      </el-form-item>
      <el-form-item label="前期费用" prop="preAmount">
        <el-input v-model="formData.preAmount" placeholder="请输入前期费用" />
      </el-form-item>
      <el-form-item label="设计费" prop="designFee">
        <el-input v-model="formData.designFee" placeholder="请输入设计费" />
      </el-form-item>
      <el-form-item label="勘测费" prop="surveyFees">
        <el-input v-model="formData.surveyFees" placeholder="请输入勘测费" />
      </el-form-item>
      <el-form-item label="检测费" prop="testingFee">
        <el-input v-model="formData.testingFee" placeholder="请输入检测费" />
      </el-form-item>
      <el-form-item label="其他费" prop="otherFee">
        <el-input v-model="formData.otherFee" placeholder="请输入其他费" />
      </el-form-item>
      <el-form-item label="计费方式" prop="countType">
        <el-select v-model="formData.countType" placeholder="请选择计费方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="合同附件url" prop="contractFileUrl">
        <el-input v-model="formData.contractFileUrl" placeholder="请输入合同附件url" />
      </el-form-item>
      <el-form-item label="建安费" prop="constructionCost">
        <el-input v-model="formData.constructionCost" placeholder="请输入建安费" />
      </el-form-item>
      <el-form-item label="资金来源" prop="source">
        <el-input v-model="formData.source" placeholder="请输入资金来源" />
      </el-form-item>
      <el-form-item label="收费标准" prop="chargingStandard">
        <el-input v-model="formData.chargingStandard" placeholder="请输入收费标准" />
      </el-form-item>
      <el-form-item label="优惠" prop="discount">
        <el-input v-model="formData.discount" placeholder="请输入优惠" />
      </el-form-item>
      <el-form-item label="是否联合体" prop="consortium">
        <el-radio-group v-model="formData.consortium">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联合体单位" prop="consortiumCompany">
        <el-input v-model="formData.consortiumCompany" placeholder="请输入联合体单位" />
      </el-form-item>
      <el-form-item label="合同提示时间" prop="reminderTime">
        <el-date-picker
          v-model="formData.reminderTime"
          type="date"
          value-format="x"
          placeholder="选择合同提示时间"
        />
      </el-form-item>
      <el-form-item label="审定金额" prop="approvedAmount">
        <el-input v-model="formData.approvedAmount" placeholder="请输入审定金额" />
      </el-form-item>
      <el-form-item label="审核文件url" prop="reviewFileUrl">
        <el-input v-model="formData.reviewFileUrl" placeholder="请输入审核文件url" />
      </el-form-item>
      <el-form-item label="合同id" prop="contractId">
        <el-input v-model="formData.contractId" placeholder="请输入合同id" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ExtContractApi, ExtContractVO } from '@/api/cms-ext/extcontract'

/** 外部合同 表单 */
defineOptions({ name: 'ExtContractForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
   id: undefined,
  projectId: undefined,
  name: undefined,
  type: undefined,
  customerCompanyId: undefined,
  progress: undefined,
  expectedTime: undefined,
  signingTime: undefined,
  archiveTime: undefined,
  status: undefined,
  amount: undefined,
  preAmount: undefined,
  designFee: undefined,
  surveyFees: undefined,
  testingFee: undefined,
  otherFee: undefined,
  countType: undefined,
  remark: undefined,
  contractFileUrl: undefined,
  constructionCost: undefined,
  source: undefined,
  chargingStandard: undefined,
  discount: undefined,
  consortium: undefined,
  consortiumCompany: undefined,
  reminderTime: undefined,
  approvedAmount: undefined,
  reviewFileUrl: undefined,
  contractId: undefined,
})
const formRules = reactive({
  projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
  expectedTime: [{ required: true, message: '预计签订时间不能为空', trigger: 'blur' }],
  amount: [{ required: true, message: '合同金额不能为空', trigger: 'blur' }],
  countType: [{ required: true, message: '计费方式不能为空', trigger: 'change' }],
  chargingStandard: [{ required: true, message: '收费标准不能为空', trigger: 'blur' }],
  consortium: [{ required: true, message: '是否联合体不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ExtContractApi.getExtContract(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ExtContractVO
    if (formType.value === 'create') {
      await ExtContractApi.createExtContract(data)
      message.success(t('common.createSuccess'))
    } else {
      await ExtContractApi.updateExtContract(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
     id: undefined,
    projectId: undefined,
    name: undefined,
    type: undefined,
    customerCompanyId: undefined,
    progress: undefined,
    expectedTime: undefined,
    signingTime: undefined,
    archiveTime: undefined,
    status: undefined,
    amount: undefined,
    preAmount: undefined,
    designFee: undefined,
    surveyFees: undefined,
    testingFee: undefined,
    otherFee: undefined,
    countType: undefined,
    remark: undefined,
    contractFileUrl: undefined,
    constructionCost: undefined,
    source: undefined,
    chargingStandard: undefined,
    discount: undefined,
    consortium: undefined,
    consortiumCompany: undefined,
    reminderTime: undefined,
    approvedAmount: undefined,
    reviewFileUrl: undefined,
    contractId: undefined,
  }
  formRef.value?.resetFields()
}
</script>