<template>
  <div class="p-2">
    <el-card shadow="never">
      <template #header>
        <div style="text-align: left; font-weight: bold; font-size: 24px;">合同{{ form.contractId ? ' - 修改' : ' - 新增' }}</div>
      </template>
      <el-form ref="contractInfoFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="有无合同" prop="contractFlag">
              <el-radio-group v-model="form.contractFlag">
                <el-radio v-for="dict in contract_flag" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contractCode">
              <el-input v-model="form.contractCode" placeholder="请输入合同编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同名称" prop="contractName">
              <el-input v-model="form.contractName" placeholder="请输入合同名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同大类" prop="contractCategory">
              <el-select v-model="form.contractCategory" placeholder="请选择合同大类">
                <el-option v-for="dict in contract_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同类型" prop="contractType">
              <el-select v-model="form.contractType" placeholder="请选择合同类型">
                <el-option v-for="dict in contract_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务方向" prop="businessDirection">
              <el-select v-model="form.businessDirection" placeholder="请选择业务方向">
                <el-option v-for="dict in business_direction" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门" prop="contractDeptId">
              <el-input v-model="form.contractDeptId" placeholder="请输入部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同时间" prop="contractDate">
              <el-date-picker clearable v-model="form.contractDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择合同时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同总价" prop="totalPrice">
              <el-input v-model="form.totalPrice" placeholder="请输入合同总价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方公司" prop="aCustomerId">
              <el-input v-model="form.aCustomerId" placeholder="请输入甲方公司" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方授权代表" prop="aRepresent">
              <el-input v-model="form.aRepresent" placeholder="请输入甲方授权代表" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方签字日期" prop="aDate">
              <el-date-picker clearable v-model="form.aDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择甲方签字日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="乙方公司" prop="bCustomerId">
              <el-input v-model="form.bCustomerId" placeholder="请输入乙方公司" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="乙方授权代表" prop="bRepresent">
              <el-input v-model="form.bRepresent" placeholder="请输入乙方授权代表" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="乙方签字日期" prop="bDate">
              <el-date-picker clearable v-model="form.bDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择乙方签字日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同状态" prop="contractStatus">
              <el-radio-group v-model="form.contractStatus">
                <el-radio v-for="dict in contract_status" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流程状态" prop="flowStatus">
              <el-input v-model="form.flowStatus" placeholder="请输入流程状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同模板ID" prop="templateId">
              <el-input v-model="form.templateId" placeholder="请输入合同模板ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件ID" prop="ossId">
              <el-input v-model="form.ossId" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款方式" prop="paymentMethod">
              <el-input v-model="form.paymentMethod" placeholder="请输入付款方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签字合同附件" prop="signatureAppendix">
              <el-input v-model="form.signatureAppendix" placeholder="请输入签字合同附件" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同税率(预留)" prop="taxRate">
              <el-input v-model="form.taxRate" placeholder="请输入合同税率(预留)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="激活标识" prop="activeFlag">
              <el-radio-group v-model="form.activeFlag">
                <el-radio v-for="dict in active_flag" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
              <el-button @click="goBack">返 回</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script setup name="ContractInfoEdit" lang="ts">
import { addContractInfo, getContractInfo, updateContractInfo } from '@/api/oa/erp/contractInfo';
import { ContractInfoForm } from '@/api/oa/erp/contractInfo/types';

const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { contract_category, business_direction, active_flag, contract_flag, contract_type, contract_status } = toRefs<any>(
  proxy?.useDict('contract_category', 'business_direction', 'active_flag', 'contract_flag', 'contract_type', 'contract_status')
);

const buttonLoading = ref(false);
const contractInfoFormRef = ref<ElFormInstance>();

const initFormData: ContractInfoForm = {
  contractId: undefined,
  contractFlag: undefined,
  contractCode: undefined,
  contractName: undefined,
  contractCategory: undefined,
  contractType: undefined,
  businessDirection: undefined,
  contractDeptId: undefined,
  contractDate: undefined,
  totalPrice: undefined,
  aCustomerId: undefined,
  aRepresent: undefined,
  aDate: undefined,
  bCustomerId: undefined,
  bRepresent: undefined,
  bDate: undefined,
  contractStatus: undefined,
  flowStatus: undefined,
  templateId: undefined,
  ossId: undefined,
  paymentMethod: undefined,
  signatureAppendix: undefined,
  taxRate: undefined,
  remark: undefined,
  activeFlag: undefined
};

const data = reactive<{ form: ContractInfoForm; rules: any }>({
  form: { ...initFormData },
  rules: {
    contractId: [{ required: true, message: '合同ID不能为空', trigger: 'blur' }]
  }
});

const { form, rules } = toRefs(data);

const goBack = () => {
  proxy?.$tab.closePage();
};

const submitForm = () => {
  contractInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.contractId) {
        await updateContractInfo(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addContractInfo(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      goBack();
    }
  });
};

onMounted(async () => {
  const id = (route.query.id || route.params.id) as string | number;
  if (id) {
    const res = await getContractInfo(id);
    Object.assign(form.value, res.data);
  }
});
</script>


