<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="数据唯一标识" v-bind="validateInfos.sourceDataKey">
            <a-row>
              <a-col :span="12">
                <a-input v-model:value="formData.sourceDataKey" placeholder="请输入数据唯一标识" :disabled="disabled"></a-input>
              </a-col>
              <a-col :span="12">
                <a-button @click="getSourceDataKey" type="primary">生成数据唯一标识</a-button>
              </a-col>
            </a-row>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目编号" v-bind="validateInfos.projectCode">
            <a-row>
              <a-col :span="12">
                <a-input v-model:value="formData.projectCode" placeholder="请输入项目编号" :disabled="disabled"></a-input>
              </a-col>
              <a-col :span="12">
                <a-button @click="getProjectCode" type="primary">生成项目编号</a-button>
              </a-col>
            </a-row>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="项目名称" v-bind="validateInfos.projectName">
            <a-input v-model:value="formData.projectName" placeholder="请输入项目名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="栏目类型" v-bind="validateInfos.projectType">
            <j-dict-select-tag type='radio' v-model:value="formData.projectType" dictCode="project_type" placeholder="请选择栏目类型" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目创建时间" v-bind="validateInfos.pcreateTime">
            <a-date-picker placeholder="请选择项目创建时间"  v-model:value="formData.pcreateTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目所在行政区域代码" v-bind="validateInfos.regionCode">
            <JSelectRegionCode v-model:value="formData.regionCode" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易系统标识码" v-bind="validateInfos.platformCode">
            <JSelectPlatformCode v-model:value="formData.platformCode" :disabled="disabled"/>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="项目类别关联代码" v-bind="validateInfos.relateCode">
            <a-input v-model:value="formData.relateCode" placeholder="请输入项目类别关联代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="数据来源系统的编码" v-bind="validateInfos.sourceDataCode">
            <a-input v-model:value="formData.sourceDataCode" placeholder="请输入数据来源系统的编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="投资项目统一代码" v-bind="validateInfos.investProjectCode">
            <a-input v-model:value="formData.investProjectCode" placeholder="请输入投资项目统一代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目地址" v-bind="validateInfos.address">
            <a-input v-model:value="formData.address" placeholder="请输入项目地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目法人" v-bind="validateInfos.legalPerson">
            <a-input v-model:value="formData.legalPerson" placeholder="请输入项目法人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="资金来源" v-bind="validateInfos.fundSource">
            <a-input v-model:value="formData.fundSource" placeholder="请输入资金来源" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="出资比例" v-bind="validateInfos.contributionScale">
            <a-input v-model:value="formData.contributionScale" placeholder="请输入出资比例" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目规模" v-bind="validateInfos.projectScale">
            <a-input v-model:value="formData.projectScale" placeholder="请输入项目规模" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="招标人名称" v-bind="validateInfos.tendererName">
            <a-input v-model:value="formData.tendererName" placeholder="请输入招标人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="招标人代码" v-bind="validateInfos.tendererCode">
            <a-input v-model:value="formData.tendererCode" placeholder="请输入招标人代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="联系人" v-bind="validateInfos.contactor">
            <a-input v-model:value="formData.contactor" placeholder="请输入联系人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="联系方式" v-bind="validateInfos.contactInformation">
            <a-input v-model:value="formData.contactInformation" placeholder="请输入联系方式" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目审批文件名称" v-bind="validateInfos.approvalName">
            <a-input v-model:value="formData.approvalName" placeholder="请输入项目审批文件名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目审批文号" v-bind="validateInfos.approvalNumber">
            <a-input v-model:value="formData.approvalNumber" placeholder="请输入项目审批文号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目审批单位" v-bind="validateInfos.approvalAuthority">
            <a-input v-model:value="formData.approvalAuthority" placeholder="请输入项目审批单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否重点项目" v-bind="validateInfos.isEmphasisProject">
            <a-input v-model:value="formData.isEmphasisProject" placeholder="请输入是否重点项目" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="行业监管部门" v-bind="validateInfos.industryRegulatoryAuthorities">
            <a-input v-model:value="formData.industryRegulatoryAuthorities" placeholder="请输入行业监管部门" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否国有资金占控股或者主导地位" v-bind="validateInfos.isNationalCapitalHolding">
            <a-input v-model:value="formData.isNationalCapitalHolding" placeholder="请输入是否国有资金占控股或者主导地位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="交易服务系统标识码" v-bind="validateInfos.pubServicePlatCode">
            <a-input v-model:value="formData.pubServicePlatCode" placeholder="请输入交易服务系统标识码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易平台或发布工具标识码" v-bind="validateInfos.tradePlat">
            <a-input v-model:value="formData.tradePlat" placeholder="请输入交易平台或发布工具标识码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公共服务平台标识码" v-bind="validateInfos.pubServicePlat">
            <a-input v-model:value="formData.pubServicePlat" placeholder="请输入公共服务平台标识码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="价款形式代码" v-bind="validateInfos.priceFormCode">
            <a-input v-model:value="formData.priceFormCode" placeholder="请输入价款形式代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="投资总额单位" v-bind="validateInfos.investmentUnit">
            <a-input v-model:value="formData.investmentUnit" placeholder="请输入投资总额单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="投资总额币种代码" v-bind="validateInfos.investmentCurrency">
            <a-input v-model:value="formData.investmentCurrency" placeholder="请输入投资总额币种代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="投资总额" v-bind="validateInfos.totleInvestment">
	          <a-input-number v-model:value="formData.totleInvestment" placeholder="请输入投资总额" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="投资总额比率或其他" v-bind="validateInfos.investmentPercent">
            <a-input v-model:value="formData.investmentPercent" placeholder="请输入投资总额比率或其他" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目法人统一社会信用代码" v-bind="validateInfos.legalCreditCode">
            <a-input v-model:value="formData.legalCreditCode" placeholder="请输入项目法人统一社会信用代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目法人地址" v-bind="validateInfos.legalAddress">
            <a-input v-model:value="formData.legalAddress" placeholder="请输入项目法人地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="财政资金" v-bind="validateInfos.financialFund">
	          <a-input-number v-model:value="formData.financialFund" placeholder="请输入财政资金" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="自筹资金" v-bind="validateInfos.selfFund">
	          <a-input-number v-model:value="formData.selfFund" placeholder="请输入自筹资金" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="其他资金" v-bind="validateInfos.otherFund">
	          <a-input-number v-model:value="formData.otherFund" placeholder="请输入其他资金" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否采用代建制" v-bind="validateInfos.isAgentConstruct">
            <a-input v-model:value="formData.isAgentConstruct" placeholder="请输入是否采用代建制" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="法定代表人" v-bind="validateInfos.legalRepresentative">
            <a-input v-model:value="formData.legalRepresentative" placeholder="请输入法定代表人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易中心编码" v-bind="validateInfos.dataSourceCode">
            <a-input v-model:value="formData.dataSourceCode" placeholder="请输入交易中心编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../GcProject.api';
  import { Form } from 'ant-design-vue';
  import JSelectRegionCode from '/@/components/Form/src/jeecg/components/JSelectRegionCode.vue';
  import JSelectIndustriesType from '/@/components/Form/src/jeecg/components/JSelectIndustriesType.vue';
  import JSelectPlatformCode from '/@/components/Form/src/jeecg/components/JSelectPlatformCode.vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    sourceDataKey: '',
    sourceDataCode: '',
    projectCode: '',
    projectName: '',
    regionCode: '',
    relateCode: '',
    investProjectCode: '',
    address: '',
    legalPerson: '',
    industriesType: '',
    fundSource: '',
    contributionScale: '',
    projectScale: '',
    tendererName: '',
    tendererCode: '',
    contactor: '',
    contactInformation: '',
    approvalName: '',
    approvalNumber: '',
    approvalAuthority: '',
    isEmphasisProject: '',
    pcreateTime: '',
    industryRegulatoryAuthorities: '',
    isNationalCapitalHolding: '',
    platformCode: '',
    pubServicePlatCode: '',
    tradePlat: '',
    pubServicePlat: '',
    priceFormCode: '',
    investmentUnit: '',
    investmentCurrency: '',
    totleInvestment: undefined,
    investmentPercent: '',
    legalCreditCode: '',
    legalAddress: '',
    financialFund: undefined,
    selfFund: undefined,
    otherFund: undefined,
    isAgentConstruct: '',
    legalRepresentative: '',
    dataSourceCode: '',
    remark: '',
    projectType: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    sourceDataKey: [{ required: true, message: '请输入数据唯一标识!'},],
    platformCode: [{ required: true, message: '请输入交易系统标识码!'},],
    projectCode: [{ required: true, message: '请输入项目编号!'},],
    pcreateTime: [{ required: true, message: '请输入项目创建时间!'},],
    projectName: [{ required: true, message: '请输入项目名称!'},],
    regionCode: [{ required: true, message: '请输入项目所在行政区域代码!'},],
    projectType: [{ required: true, message: '请输入项目栏目类型'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });


  /**
   * 新增
   */
  function add() {
    editAdd({});
  }

  function editAdd(record) {
    nextTick(() => {
      resetFields();
      //赋值
      formData.projectType = '5'
      Object.assign(formData, record);
    });
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  function getSourceDataKey() {
    defHttp.get({
      url:'/gcproject/gcProject/getSourceDataKey',
      timeout:100000,
    }).then((res)=>{
      formData.sourceDataKey = res;
    })
  }
  function getProjectCode() {
    defHttp.get({
      url:'/gcproject/gcProject/getProjectCode',
      timeout:100000,
    }).then((res)=>{
      formData.projectCode = res;
    })
  }

  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
