<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-input v-model:value="formData.sourceDataKey" placeholder="请输入数据唯一标识" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目编号" v-bind="validateInfos.projectCode">
            <a-input v-model:value="formData.projectCode" placeholder="请输入项目编号" :disabled="disabled"></a-input>
          </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.tenderProjectCode">
            <a-input v-model:value="formData.tenderProjectCode" placeholder="请输入招标项目编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告标题" v-bind="validateInfos.noticeName">
            <a-input v-model:value="formData.noticeName" placeholder="请输入公告标题" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告内容" v-bind="validateInfos.noticeContent">
	          <j-editor v-model:value="formData.noticeContent" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告发布时间" v-bind="validateInfos.noticeSendTime">
		        <a-date-picker placeholder="请选择公告发布时间"  v-model:value="formData.noticeSendTime" 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.noticeNature">
            <a-input v-model:value="formData.noticeNature" placeholder="请输入公告性质编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告性质名称" v-bind="validateInfos.noticeNatureName">
            <a-input v-model:value="formData.noticeNatureName" placeholder="请输入公告性质名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目所在行政区域代码" v-bind="validateInfos.regionCode">
            <a-input v-model:value="formData.regionCode" placeholder="请输入项目所在行政区域代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目所在行政区域名称" v-bind="validateInfos.regionName">
            <a-input v-model:value="formData.regionName" placeholder="请输入项目所在行政区域名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目行业分类编码" v-bind="validateInfos.industriesTypeCode">
            <a-input v-model:value="formData.industriesTypeCode" placeholder="请输入项目行业分类编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目行业分类名称" v-bind="validateInfos.industriesTypeName">
            <a-input v-model:value="formData.industriesTypeName" placeholder="请输入项目行业分类名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易系统标识码" v-bind="validateInfos.platformCode">
            <a-input v-model:value="formData.platformCode" placeholder="请输入交易系统标识码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易系统标识码区域名称" v-bind="validateInfos.platformName">
            <a-input v-model:value="formData.platformName" placeholder="请输入交易系统标识码区域名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告上面类型编码" v-bind="validateInfos.noticeTypeCode">
            <a-input v-model:value="formData.noticeTypeCode" placeholder="请输入公告上面类型编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告上面类型名称" v-bind="validateInfos.noticeTypeName">
            <a-input v-model:value="formData.noticeTypeName" placeholder="请输入公告上面类型名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易左侧类型编码" v-bind="validateInfos.transactionTypeCode">
            <a-input v-model:value="formData.transactionTypeCode" placeholder="请输入交易左侧类型编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易左侧类型名称" v-bind="validateInfos.transactionTypeName">
            <a-input v-model:value="formData.transactionTypeName" placeholder="请输入交易左侧类型名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告类型编码" v-bind="validateInfos.bulletinTypeCode">
            <a-input v-model:value="formData.bulletinTypeCode" placeholder="请输入公告类型编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告类型名称" v-bind="validateInfos.bulletinTypeName">
            <a-input v-model:value="formData.bulletinTypeName" placeholder="请输入公告类型名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告发布责任人" v-bind="validateInfos.bulletinDuty">
            <a-input v-model:value="formData.bulletinDuty" 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.dataSourceName">
            <a-input v-model:value="formData.dataSourceName" placeholder="请输入交易中心名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公告标段包数量" v-bind="validateInfos.noticeBidNum">
            <a-input v-model:value="formData.noticeBidNum" placeholder="请输入公告标段包数量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="相关标段包编号" v-bind="validateInfos.bidSectionCodes">
            <a-input v-model:value="formData.bidSectionCodes" placeholder="请输入相关标段包编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="招标文件资格预审文件获取时间" v-bind="validateInfos.docGetStartTime">
		        <a-date-picker placeholder="请选择招标文件资格预审文件获取时间"  v-model:value="formData.docGetStartTime" 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.docGetEndTime">
		        <a-date-picker placeholder="请选择招标文件资格预审文件获取截至时间"  v-model:value="formData.docGetEndTime" 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.bidDocReferEndTime">
		        <a-date-picker placeholder="请选择投标文件资格预审申请文件递交截止时间"  v-model:value="formData.bidDocReferEndTime" 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.bidOpenTime">
		        <a-date-picker placeholder="请选择开标时间"  v-model:value="formData.bidOpenTime" 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.validPeriod">
	          <a-input-number v-model:value="formData.validPeriod" placeholder="请输入投标有效期单位天" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="答疑澄清时间" v-bind="validateInfos.clarifyTime">
		        <a-date-picker placeholder="请选择答疑澄清时间"  v-model:value="formData.clarifyTime" 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.submitTime">
		        <a-date-picker placeholder="请选择招标文件生成时间"  v-model:value="formData.submitTime" 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.bidOpeningAddress">
            <a-input v-model:value="formData.bidOpeningAddress" placeholder="请输入开标地点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="开标时间" v-bind="validateInfos.bidOpeningTime">
		        <a-date-picker placeholder="请选择开标时间"  v-model:value="formData.bidOpeningTime" 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.bidFinishTime">
		        <a-date-picker placeholder="请选择开标结束时间"  v-model:value="formData.bidFinishTime" 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.file">
	          <j-upload v-model:value="formData.file"  :disabled="disabled" ></j-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="开标结束时间" v-bind="validateInfos.docstatus">
		        <a-date-picker placeholder="请选择开标结束时间"  v-model:value="formData.docstatus" 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.docpubtime">
		        <a-date-picker placeholder="请选择开标结束时间"  v-model:value="formData.docpubtime" 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.isSynchronous">
            <j-dict-select-tag type='radio' v-model:value="formData.isSynchronous" dictCode="is_synchronous" placeholder="请选择是否同步" :disabled="disabled"/>
          </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 JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../GgzyContentDistribution.api';
  import { Form } from 'ant-design-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: '',   
    projectCode: '',   
    projectName: '',   
    tenderProjectCode: '',   
    noticeName: '',   
    noticeContent: '',   
    noticeSendTime: '',   
    noticeNature: '',   
    noticeNatureName: '',   
    regionCode: '',   
    regionName: '',   
    industriesTypeCode: '',   
    industriesTypeName: '',   
    platformCode: '',   
    platformName: '',   
    noticeTypeCode: '',   
    noticeTypeName: '',   
    transactionTypeCode: '',   
    transactionTypeName: '',   
    bulletinTypeCode: '',   
    bulletinTypeName: '',   
    bulletinDuty: '',   
    dataSourceCode: '',   
    dataSourceName: '',   
    noticeBidNum: '',   
    bidSectionCodes: '',   
    docGetStartTime: '',   
    docGetEndTime: '',   
    bidDocReferEndTime: '',   
    bidOpenTime: '',   
    validPeriod: undefined,
    clarifyTime: '',   
    submitTime: '',   
    bidOpeningAddress: '',   
    bidOpeningTime: '',   
    bidFinishTime: '',   
    file: '',   
    docstatus: '',   
    docpubtime: '',   
    isSynchronous: '',   
  });
  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: '请输入数据唯一标识!'},],
    projectCode: [{ required: true, message: '请输入项目编号!'},],
    projectName: [{ required: true, message: '请输入项目名称!'},],
    tenderProjectCode: [{ required: true, message: '请输入招标项目编号!'},],
    noticeName: [{ required: true, message: '请输入公告标题!'},],
    noticeContent: [{ required: true, message: '请输入公告内容!'},],
    noticeSendTime: [{ required: true, message: '请输入公告发布时间!'},],
    noticeNature: [{ required: true, message: '请输入公告性质编号!'},],
    noticeNatureName: [{ required: true, message: '请输入公告性质名称!'},],
    regionCode: [{ required: true, message: '请输入项目所在行政区域代码!'},],
    regionName: [{ required: true, message: '请输入项目所在行政区域名称!'},],
    industriesTypeCode: [{ required: true, message: '请输入项目行业分类编码!'},],
    industriesTypeName: [{ required: true, message: '请输入项目行业分类名称!'},],
    platformCode: [{ required: true, message: '请输入交易系统标识码!'},],
    platformName: [{ required: true, message: '请输入交易系统标识码区域名称!'},],
    noticeTypeCode: [{ required: true, message: '请输入公告上面类型编码!'},],
    noticeTypeName: [{ required: true, message: '请输入公告上面类型名称!'},],
    transactionTypeCode: [{ required: true, message: '请输入交易左侧类型编码!'},],
    transactionTypeName: [{ required: true, message: '请输入交易左侧类型名称!'},],
    bulletinTypeCode: [{ required: true, message: '请输入公告类型编码!'},],
    bulletinTypeName: [{ required: true, message: '请输入公告类型名称!'},],
    bulletinDuty: [{ required: true, message: '请输入公告发布责任人!'},],
    dataSourceCode: [{ required: true, message: '请输入交易中心编码!'},],
    dataSourceName: [{ required: true, message: '请输入交易中心名称!'},],
    noticeBidNum: [{ required: true, message: '请输入公告标段包数量!'},],
    bidSectionCodes: [{ required: true, message: '请输入相关标段包编号!'},],
    docGetStartTime: [{ required: true, message: '请输入招标文件资格预审文件获取时间!'},],
    docGetEndTime: [{ required: true, message: '请输入招标文件资格预审文件获取截至时间!'},],
    bidDocReferEndTime: [{ required: true, message: '请输入投标文件资格预审申请文件递交截止时间!'},],
    bidOpenTime: [{ required: true, message: '请输入开标时间!'},],
    validPeriod: [{ required: true, message: '请输入投标有效期单位天!'},],
    clarifyTime: [{ required: true, message: '请输入答疑澄清时间!'},],
    submitTime: [{ required: true, message: '请输入招标文件生成时间!'},],
    bidOpeningAddress: [{ required: true, message: '请输入开标地点!'},],
    bidOpeningTime: [{ required: true, message: '请输入开标时间!'},],
    bidFinishTime: [{ required: true, message: '请输入开标结束时间!'},],
    file: [{ required: true, message: '请输入附件!'},],
    docstatus: [{ required: true, message: '请输入开标结束时间!'},],
    docpubtime: [{ required: true, message: '请输入开标结束时间!'},],
    isSynchronous: [{ 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() {
    edit({});
  }

  /**
   * 编辑
   */
  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;
      });
  }


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

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