<template>
  <view class="bg-content">
    <image
      class="bg-image"
      mode="widthFix"
      src="https://qtzl.etcjz.cn/default-bucket/20240324/3eba2095f5204798a1f8101b_beijing.jpg"
    ></image>
    <view class="content-re">
      <custom-header
        title="车辆信息"
        :back="config.hasReturn === 'true'"
      ></custom-header>
      <view style="padding: 20rpx 0">
        <form-builder-vue
          @radioChange="radioChange"
          :formData="formData"
          :config="config"
          @uploadImgOcr="uploadImg"
          @submit="submit"
          @handleBlurs="handleBlurs"
          @handleChange="handleChange"
		  @handleUseUserType="handleUseUserType"
        >
        </form-builder-vue>
      </view>
    </view>
  </view>
</template>
<script setup lang='ts'>
import CustomHeader from "@/components/CustomHeader.vue";
import FormBuilderVue from "@/components/form-builder/form-builder-vue3.vue";
import { TypeData, Index } from "@/components/form-builder/tools";
import { reactive, ref, nextTick } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { useQdOrderStore } from "@/stores/qdOrder.js";

import ocrHandle from "./ocrHandle";
import fromData from "./fromData.js";
import { requestNew, request } from "@/utils/network/request";

// 初始化响应式数据
const orderNo = ref("");
const vanType = ref(0);
const qdOrderStore = useQdOrderStore();
const config = ref<any>({
  submitName: "下一步",
  titleWidth: 250,
  isHeader: true,
  hasReturn: "true",
});
const formData = ref<any>(fromData);
const isUpOwner = ref(false);
const qdOrderVal = ref<any>({});
const ocrDatas = ref<any>(null); // 添加OCR数据的ref
const ownerType = ref(0); // 0: 个人, 1: 企业

let orderType = "";//订单类型

const handleUseUserType = (e, item) => {
	if(e != 0) {
		formData.value[Index('道路运输许可证', formData.value)].show = false;
		formData.value[Index('经营许可证编号', formData.value)].show = false;
	} else {
		formData.value[Index('道路运输许可证', formData.value)].show = true;
		formData.value[Index('经营许可证编号', formData.value)].show = true;
	}
}
// 定义个人车主信息表单项
const personalOwnerFields = [
  {
    underline: false,
    titleShow: false,
    disabled: false,
    vertical: "1",
    id: "id_1687057765030",
    title: "车主信息",
    type: "100",
    required: false,
  },
  {
    underline: false,
    titleShow: false,
    disabled: false,
    vertical: "1",
    id: "id_owner_type_toggle",
    title: "车主类型",
    type: "7",
    value: "ownerType",
    required: false,
    itemData: [
      { value: 0, name: "个人", checked: true },
      { value: 1, name: "企业", checked: false }
    ],
    ownerType: 0,
  },
  {
    placeholderImg: "image/applyCard/renxiang.png",
    underline: false,
    titleShow: false,
    hint: "上传身份证的人像面",
    disabled: false,
    vertical: "1",
    id: "id_1686971502045",
    title: "人像面",
    type: "14",
    value: "userPosImgUrl",
    required: true,
    show: false,
    componentKey: Date.now(),
  },
  {
    placeholderImg: "image/applyCard/guohui.png",
    underline: false,
    titleShow: false,
    hint: "上传身份证的国徽面",
    disabled: false,
    vertical: "1",
    id: "id_1686971729660",
    title: "国徽面",
    type: "14",
    value: "userNegImgUrl",
    required: true,
    show: false,
    componentKey: Date.now(),
  },
  {
    placeholderImg: "image/applyCard/weituoshu.png",
    underline: false,
    titleShow: false,
    hint: "上传文字清晰的委托书",
    disabled: false,
    vertical: "1",
    id: "id_1686971922491",
    title: "委托书",
    type: "14",
    value: "proxyUrl",
    required: true,
    show: false,
    componentKey: Date.now(),
  },
  {
    underline: true,
    maxlength: "20",
    titleShow: true,
    hint: "请输入姓名",
    disabled: false,
    vertical: "1",
    id: "id_1686974639025",
    title: "姓名",
    type: "2",
    value: "userName",
    required: true,
    show: false,
  },
  {
    underline: true,
    maxlength: "11",
    titleShow: true,
    vertical: "1",
    title: "联系方式",
    type: "2",
    required: true,
    checkReg: "^1[3456789]\\d{9}$",
    hint: "请输入联系方式",
    checkPrompt: "请输入正确的联系方式",
    disabled: false,
    inputType: "tel",
    id: "id_1686974684640",
    value: "mobile",
    mobile: "",
    show: false,
  },
  {
    underline: true,
    maxlength: "18",
    titleShow: true,
    vertical: "1",
    title: "证件号码",
    type: "2",
    required: true,
    checkReg: "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X)$)",
    hint: "请输入证件号码",
    checkPrompt: "请输入正确的身份证号",
    disabled: false,
    id: "id_1686974751416",
    value: "userIdNum",
    show: false,
  },
  {
    underline: true,
    titleShow: true,
    hint: "请输入住址",
    disabled: false,
    vertical: "1",
    id: "id_1686974837416",
    title: "住址", 
    type: "2",
    value: "address",
    required: true,
  }
];

// 定义企业车主信息表单项
const enterpriseOwnerFields = [
  {
    underline: false,
    titleShow: false,
    disabled: false,
    vertical: "1",
    id: "id_1687057765030",
    title: "车主信息",
    type: "100",
    required: false,
  },
  {
    underline: false,
    titleShow: false,
    disabled: false,
    vertical: "1",
    id: "id_owner_type_toggle",
    title: "车主类型",
    type: "7",
    value: "ownerType",
    required: false,
    itemData: [
      { value: 0, name: "个人", checked: false },
      { value: 1, name: "企业", checked: true }
    ],
    ownerType: 1,
  },
  {
    placeholderImg: "image/applyCard/zhizhao.png",
    underline: false,
    titleShow: false,
    hint: "上传企业营业执照",
    disabled: false,
    vertical: "1",
    id: "id_1686971502046",
    title: "营业执照",
    type: "14",
    value: "businessLicenseUrl",
    required: true,
    show: false,
    componentKey: Date.now(),
  },
  {
    placeholderImg: "image/applyCard/weituoshu.png",
    underline: false,
    titleShow: false,
    hint: "上传文字清晰的委托书",
    disabled: false,
    vertical: "1",
    id: "id_1686971922492",
    title: "委托书",
    type: "14",
    value: "proxyUrl",
    required: true,
    show: false,
    componentKey: Date.now(),
  },
  {
    underline: true,
    maxlength: "100",
    titleShow: true,
    hint: "请输入公司名称",
    disabled: false,
    vertical: "1",
    id: "id_1686974639026",
    title: "公司名称",
    type: "2",
    value: "unitName",
    required: true,
    show: false,
  },
  // {
  //   underline: true,
  //   maxlength: "11",
  //   titleShow: true,
  //   vertical: "1",
  //   title: "联系方式",
  //   type: "2",
  //   required: true,
  //   checkReg: "^1[3456789]\\d{9}$",
  //   hint: "请输入联系方式",
  //   checkPrompt: "请输入正确的联系方式",
  //   disabled: false,
  //   inputType: "tel",
  //   id: "id_1686974684641",
  //   value: "mobile",
  //   mobile: "",
  //   show: false,
  // },
  {
    underline: true,
    maxlength: "18",
    titleShow: true,
    vertical: "1",
    title: "社会信用代码",
    type: "2",
    required: true,
    hint: "请输入社会信用代码",
    disabled: false,
    id: "id_1686974751417",
    value: "screditCode",
    show: false,
  },
  {
    underline: true,
    titleShow: true,
    hint: "请输入住址",
    disabled: false,
    vertical: "1",
    id: "id_1686974837417",
    title: "住址",
    type: "2",
    value: "adress",
    required: true,
	show: false,
  }
];

onLoad((opin) => {
  // 调用trackEvent接口获取订单信息
  orderNo.value = opin.orderNo;
  const params = {
    orderNo: orderNo.value,
  };
  trackEvent(params);
});

function trackEvent(params) {
  requestNew("/iaw/h5/order/web/queryOrder", {
    data: params,
  }).then((res) => {

    if (res.code === 0) {
      qdOrderVal.value = res.data;
      vanType.value = res.data.vanType;
      qdOrderStore.qdOrderVal = res.data; // 将整个订单数据存入store
	  // 设置车牌号
		const vehiclePlateIndex = Index("车牌号", formData.value);
      if (vehiclePlateIndex !== -1 && res.data.vehiclePlate) {
        formData.value[vehiclePlateIndex][formData.value[vehiclePlateIndex].value] = res.data.vehiclePlate;
      }
      
      // 设置车轴数默认值为2
      const axleCountIndex = Index("车轴数", formData.value);
      if (axleCountIndex !== -1) {
        formData.value[axleCountIndex][formData.value[axleCountIndex].value] = "2";
      }
	  
	  orderType = res.data.userType;
      // handleHide()
    }
  });
}

async function handleBlurs(e, item) {
  if (item.value === "axleCount") {
    // await handleVehicCharge(formData);
    // handleVehicUsePropert(formData);
  }
}

async function handleChange(e, item) {
  if (item.value === "userName") {
    submitQuery.idName = e.detail.value;
  }
  if (item.value === "ownerName") {
    submitQuery.man = e.detail.value;
  }
  if (item.value === "mobile") {
    submitQuery.mobile = e.detail.value;
  }
  // 处理企业相关字段
  if (item.value === "unitName") {
    submitQuery.companyName = e.detail.value;
  }
  if (item.value === "screditCode") {
    submitQuery.socialCreditCode = e.detail.value;
  }
  if (item.value === "adress") {
    submitQuery.companyAddress = e.detail.value;
  }
}

// 点击收费车型时获取选项
async function handleClick(e, item) {
  if (item.value === "feeVehicleType" || item.title === "收费车型") {
    const ocrData = ocrDatas.value || {};
    const axleCountItem = formData.value[Index("车轴数", formData.value)];
    const approvedCount = formData.value[Index("核定载人数", formData.value)];
    const totalMass = formData.value[Index("总质量(kg)", formData.value)];
    const vehicleDimensions = formData.value[Index("外廓尺寸", formData.value)];
    // 检查是否输入了车轴数
    if (!axleCountItem || !axleCountItem[axleCountItem.value]) {
      uni.showToast({
        title: "请输入车轴数",
        icon: "none",
        duration: 2000,
      });
      return;
    }

    // 获取收费车型选项
    const feeVehicleParams = {
      approvedCount: approvedCount[approvedCount.value],
      axleCount: Number(axleCountItem[axleCountItem.value]),
      totalMass: totalMass[totalMass.value],
      vanType: qdOrderStore.qdOrderVal.vanType,
      vehicleDimensions: vehicleDimensions[vehicleDimensions.value],
    };

    // 调用获取收费车型选项接口
    const res = await requestNew("/iaw/h5/order/web/vehicCharge", {
      data: feeVehicleParams,
    });

    if (res.code === 0) {
      // 更新收费车型选项
      const feeVehicleTypeItem =
        formData.value[Index("收费车型", formData.value)];
      const vehicChargeVal = [res.data];
      if (feeVehicleTypeItem) {
        feeVehicleTypeItem.options = vehicChargeVal.map((item) => ({
          label: item.feeVehicleTypeStr,
          value: item.feeVehicleType,
        }));
        // 默认选中第一个选项
        if (vehicChargeVal.length > 0) {
          feeVehicleTypeItem[feeVehicleTypeItem.value] =
            vehicChargeVal[0].feeVehicleTypeStr;
        }
      }
    }
  }
}

function isTractorHandle() {
  const index = Index("车辆用户类型", formData.value);
  formData.value[index].disabled = false;
}
//单项选择器
function radioChange(e: any, item: TypeData) {
  if (item.title === "是否牵引车") {
    if (parseInt(e.detail.value) == 1) {
      formData.value[Index("道路运输证", formData.value)].show = false;
    } else {
      formData.value[Index("道路运输证", formData.value)].show = true;
    }
  }
  
  // 处理车主类型切换
  if (item.title === "车主类型") {
    const selectedType = parseInt(e.detail.value);
    ownerType.value = selectedType;
    switchOwnerType(selectedType);
  }
}

// 切换车主类型表单
function switchOwnerType(type: number) {
  // 找到车主信息开始的位置
  const ownerInfoIndex = formData.value.findIndex(item => item.title === "车主信息");
  
  if (ownerInfoIndex !== -1) {
    // 先清除当前车主信息相关数据（在移除表单项之前）
    clearOwnerData(type);
    
    // 移除现有的车主信息相关表单项
    let removeCount = 0;
    for (let i = ownerInfoIndex; i < formData.value.length; i++) {
      removeCount++;
      // 如果遇到下一个主要部分，停止计数
      if (i > ownerInfoIndex && formData.value[i].type === "100") {
        removeCount--;
        break;
      }
    }
    
    // 移除旧的车主信息表单项
    formData.value.splice(ownerInfoIndex, removeCount);
    
    // 根据类型添加对应的表单项，并强制更新componentKey
    const fieldsToAdd = type === 0 ? personalOwnerFields : enterpriseOwnerFields;
    // 为每个图片组件生成新的componentKey
    const updatedFields = fieldsToAdd.map(field => {
      if (field.type === "14") { // 图片组件
        return { ...field, componentKey: Date.now() + Math.random() };
      }
      return field;
    });
    formData.value.splice(ownerInfoIndex, 0, ...updatedFields);
    
    // 强制清除所有相关数据
    nextTick(() => {
      // 清除formData中的字段
      clearOwnerData(type);
      
      // 强制清除submitQuery中的数据
      if (submitQuery) {
        // 清除所有可能的图片相关数据
        submitQuery.ownerPosImgUrl = '';
        submitQuery.ownerNegImgUrl = '';
        submitQuery.proxyUrl = '';
        submitQuery.businessLicenseUrl = '';
        submitQuery.idName = '';
        submitQuery.mobile = '';
        submitQuery.idno = '';
        submitQuery.ownerIdAddress = '';
        submitQuery.companyName = '';
        submitQuery.socialCreditCode = '';
        submitQuery.companyAddress = '';
      }
    });
	
	//单位订单不需要委托书，个人需要
	if(orderType != "PERSONAL_USER"){
		// 找到委托书的位置
		const weiTuoIndex = formData.value.findIndex(item => item.title === "委托书");
		if (weiTuoIndex !== -1) {
			formData.value.splice(weiTuoIndex,1)
		}
	}
	console.log(type, "type")
  }
}

// 清除车主信息相关数据
function clearOwnerData(type: number) {
  // 通用清除函数
  const clearFields = (fields: string[]) => {
    fields.forEach(field => {
      const index = formData.value.findIndex(item => item.value === field);
      if (index !== -1) {
        // 清除字段值
        formData.value[index][field] = '';
        // 如果是图片字段，还需要清除imgUrl属性并强制重新渲染
        if (field.includes('ImgUrl') || field === 'proxyUrl') {
          formData.value[index].imgUrl = '';
          // 通过修改key来强制重新渲染组件
          formData.value[index].componentKey = Date.now() + Math.random();
          console.log(`清除图片字段: ${field}, 新componentKey: ${formData.value[index].componentKey}`);
        }
      }
    });
  };

  // 清除所有可能的图片字段，不管是什么类型
  const allImageFields = ['userPosImgUrl', 'userNegImgUrl', 'proxyUrl', 'businessLicenseUrl'];
  allImageFields.forEach(field => {
    const index = formData.value.findIndex(item => item.value === field);
    if (index !== -1) {
      formData.value[index][field] = '';
      formData.value[index].imgUrl = '';
      formData.value[index].componentKey = Date.now() + Math.random();
    }
  });

  // 清除submitQuery中的相关数据
  if (type === 0) {
    // 清除个人车主信息相关数据
    const personalFields = ['userPosImgUrl', 'userNegImgUrl', 'proxyUrl', 'userName', 'mobile', 'userIdNum', 'address'];
    clearFields(personalFields);
    
    // 清除submitQuery中的个人相关数据
    if (submitQuery) {
      submitQuery.ownerPosImgUrl = '';
      submitQuery.ownerNegImgUrl = '';
      submitQuery.proxyUrl = '';
      submitQuery.idName = '';
      submitQuery.mobile = '';
      submitQuery.idno = '';
      submitQuery.ownerIdAddress = '';
    }
  } else {
    // 清除企业车主信息相关数据
    const enterpriseFields = ['businessLicenseUrl', 'proxyUrl', 'unitName', 'screditCode', 'adress'];
    clearFields(enterpriseFields);
    
    // 清除submitQuery中的企业相关数据
    if (submitQuery) {
      submitQuery.businessLicenseUrl = '';
      submitQuery.proxyUrl = '';
      submitQuery.companyName = '';
      submitQuery.socialCreditCode = '';
      submitQuery.companyAddress = '';
    }
  }
}
//有哪些字段隐藏哪些字段
function handleHide() {
  if (formData.value[Index("道路运输证", formData.value)]) {
    formData.value[Index("道路运输证", formData.value)].show = true;
  }
}
// 货车或者客车切换
function checkCarTpe(key) {
  // true为客车，false为货车
  // formData.value[Index('总质量(kg)', formData.value)].show = key
  // formData.value[Index('整备质量(kg)', formData.value)].show = key
  formData.value[Index("核定载质量(kg)", formData.value)].show = key;
  formData.value[Index("准牵引总质量(kg)", formData.value)].show = key;
  if (formData.value[Index("道路运输证", formData.value)]) {
    formData.value[Index("道路运输证", formData.value)].show = key;
    formData.value[Index("是否牵引车", formData.value)].show = key;
  }

  // formData.value[Index('车轴数', formData.value)].show = key
  if (formData.value[Index("车头照", formData.value)]) {
    formData.value[Index("车头照", formData.value)].show = key;
  }
  const dataItem = formData.value[Index("车辆用户类型", formData.value)];
  dataItem.disabled = true;
  dataItem[dataItem.value] = 0;
}

// ocr图片处理和额外请求参数处理
const {
  uploadImgHandle,
  submitQuery,
  ocrDataFu,
  handleVehicCharge,
  handleVehicUsePropert,
  uploadImgHandleId,
} = ocrHandle();

function uploadImg(fileList, item) {
  uploadImgHandle(fileList, item, formData);
  uploadImgHandleId(fileList, item, formData);
}

let isLock = false;

async function submit(item: any) {
  if (submitQuery.idName !== undefined) {
    isUpOwner.value = true;
  }

  // 构建请求参数
  const requestData: any = {
    orderNo: orderNo.value,
    loginSource: "431bfdc8b9c645e0b293b85d6ce1948n",
  };

  // 从表单数据中提取值并添加到请求数据中
  const vehiclePlateItem = formData.value[Index("车牌号", formData.value)];
  const axleCountItem = formData.value[Index("车轴数", formData.value)];
  const approvedCountItem = formData.value[Index("核定载人数", formData.value)];
  const totalMassItem = formData.value[Index("总质量(kg)", formData.value)];
  const maintenaceMassItem =
    formData.value[Index("整备质量(kg)", formData.value)];
  const permittedWeightItem =
    formData.value[Index("核定载质量(kg)", formData.value)];
  const permittedTowWeightItem =
    formData.value[Index("准牵引总质量(kg)", formData.value)];
  const vehicleDimensionsItem =
    formData.value[Index("外廓尺寸", formData.value)];
  const vehicleTypeItem = formData.value[Index("车辆类型", formData.value)];
  const vehicleModelItem = formData.value[Index("品牌型号", formData.value)];
  const engineNumItem = formData.value[Index("发动机号码", formData.value)];
  const vinItem = formData.value[Index("车辆识别代码", formData.value)];
  const ownerNameItem = formData.value[Index("所有人", formData.value)];
  const issueDateItem = formData.value[Index("发证日期", formData.value)];
  const registerDateItem = formData.value[Index("注册日期", formData.value)];
  const vehPosImgUrlItem = formData.value[Index("行驶证主页", formData.value)];
  const vehNegImgUrlItem = formData.value[Index("行驶证副页", formData.value)];
  const feeVehicleTypeItem = formData.value[Index("收费车型", formData.value)];
  const useCharacter = formData.value[Index("使用性质", formData.value)];
  const tractorSignItem = formData.value[Index("是否牵引车", formData.value)];
  const useUserType = formData.value[Index("车辆用户类型", formData.value)];
  
  // 获取企业信息表单项
  const companyNameItem = formData.value[Index("公司名称", formData.value)];
  const socialCreditCodeItem = formData.value[Index("社会信用代码", formData.value)];
  const businessLicenseItem = formData.value[Index("营业执照", formData.value)];
  const companyAddressItem = formData.value[Index("住址", formData.value)];
  const roadTransportPermitPicItem = formData.value[Index('道路运输许可证', formData.value)];
  const licenseIdNumItem = formData.value[Index('经营许可证编号', formData.value)];
  
  if (roadTransportPermitPicItem && roadTransportPermitPicItem[roadTransportPermitPicItem.value]) {
  	  requestData.roadTransportPermitPicUrl = roadTransportPermitPicItem[roadTransportPermitPicItem.value];
  }
  if (licenseIdNumItem && licenseIdNumItem[licenseIdNumItem.value]) {
	  requestData.licenseIdNum = licenseIdNumItem[licenseIdNumItem.value];;// = vehiclePlateItem[vehiclePlateItem.value];
  }
  // 添加表单数据到请求参数
  if (vehiclePlateItem && vehiclePlateItem[vehiclePlateItem.value]) {
    requestData.vehiclePlate = vehiclePlateItem[vehiclePlateItem.value];
  }
  if (axleCountItem && axleCountItem[axleCountItem.value]) {
    requestData.axleCount = parseInt(axleCountItem[axleCountItem.value]);
  }
  if (approvedCountItem && approvedCountItem[approvedCountItem.value]) {
    requestData.approvedCount = parseInt(
      approvedCountItem[approvedCountItem.value]
    );
  }
  if (totalMassItem && totalMassItem[totalMassItem.value]) {
    requestData.totalMass = parseInt(totalMassItem[totalMassItem.value]);
  }
  if (maintenaceMassItem && maintenaceMassItem[maintenaceMassItem.value]) {
    requestData.maintenaceMass = parseInt(
      maintenaceMassItem[maintenaceMassItem.value]
    );
  }
  if (permittedWeightItem && permittedWeightItem[permittedWeightItem.value]) {
    requestData.permittedWeight = parseInt(
      permittedWeightItem[permittedWeightItem.value]
    );
  }
  if (
    permittedTowWeightItem &&
    permittedTowWeightItem[permittedTowWeightItem.value]
  ) {
    requestData.permittedTowWeight = parseInt(
      permittedTowWeightItem[permittedTowWeightItem.value]
    );
  }
  if (
    vehicleDimensionsItem &&
    vehicleDimensionsItem[vehicleDimensionsItem.value]
  ) {
    requestData.vehicleDimensions = vehicleDimensionsItem[
      vehicleDimensionsItem.value
    ]
      .replace("mm", "")
      .trim();
  }
  if (vehicleTypeItem && vehicleTypeItem[vehicleTypeItem.value]) {
    requestData.vehicleType = vehicleTypeItem[vehicleTypeItem.value];
  }
  if (vehicleModelItem && vehicleModelItem[vehicleModelItem.value]) {
    requestData.vehicleModel = vehicleModelItem[vehicleModelItem.value];
  }
  if (engineNumItem && engineNumItem[engineNumItem.value]) {
    requestData.engineNum = engineNumItem[engineNumItem.value];
  }
  if (vinItem && vinItem[vinItem.value]) {
    requestData.vin = vinItem[vinItem.value];
  }
  if (ownerNameItem && ownerNameItem[ownerNameItem.value]) {
    requestData.ownerName = ownerNameItem[ownerNameItem.value];
  }
  if (issueDateItem && issueDateItem[issueDateItem.value]) {
    requestData.issueDate = issueDateItem[issueDateItem.value];
  }
  if (registerDateItem && registerDateItem[registerDateItem.value]) {
    requestData.registerDate = registerDateItem[registerDateItem.value];
  }
  if (vehPosImgUrlItem && vehPosImgUrlItem[vehPosImgUrlItem.value]) {
    requestData.vehPosImgUrl = vehPosImgUrlItem[vehPosImgUrlItem.value];
  }
  if (vehNegImgUrlItem && vehNegImgUrlItem[vehNegImgUrlItem.value]) {
    requestData.vehNegImgUrl = vehNegImgUrlItem[vehNegImgUrlItem.value];
  }
  if (feeVehicleTypeItem && feeVehicleTypeItem.feeVehicleType) {
    requestData.feeVehicleType = feeVehicleTypeItem.feeVehicleType;
  }
  if (tractorSignItem && tractorSignItem[tractorSignItem.value] !== undefined) {
    requestData.tractorSign =
      parseInt(tractorSignItem[tractorSignItem.value]) || 0;
  }
  if (useUserType && useUserType[useUserType.value] !== undefined) {
    requestData.useUserType = parseInt(useUserType[useUserType.value]) || 0;
  }
  if (useCharacter && useCharacter[useCharacter.value] !== undefined) {
    requestData.character = parseInt(useCharacter[useCharacter.value]) || 0;
  }
  // 添加订单相关数据
  requestData.vanType = qdOrderVal.value.vanType;
  requestData.vehiclePlateColor = qdOrderVal.value.vehiclePlateColor || 0;
  // requestData.character = 2; // 固定值
  requestData.vehicleSign = 2; // 固定值：后装
  requestData.tractorSign = 0;

     // 添加 submitQuery 中的数据
   
   // 处理图片URL，提取路径部分
   const extractImagePath = (url) => {
     if (!url) return '';
     if (url.includes('default-bucket')) {
       return url.split('default-bucket/')[1];
     } else if (url.includes('zhywpt-issuer')) {
       return url.split('qtzl.etcjz.cn/')[1];
     }
     return url;
   };
   console.log(submitQuery, "submitQuery");
   console.log("当前车主类型:", ownerType.value);
   console.log("企业表单数据:", {
     companyName: companyNameItem ? companyNameItem[companyNameItem.value] : null,
     socialCreditCode: socialCreditCodeItem ? socialCreditCodeItem[socialCreditCodeItem.value] : null,
     companyAddress: companyAddressItem ? companyAddressItem[companyAddressItem.value] : null,
     businessLicense: businessLicenseItem ? businessLicenseItem[businessLicenseItem.value] : null
   });
   
   if (submitQuery.mobile) requestData.ownerTel = submitQuery.mobile;
   if (submitQuery.userIdType) requestData.ownerIdType = submitQuery.userIdType;
   if (submitQuery.proxyUrl) requestData.proxyUrl = submitQuery.proxyUrl;
   if (submitQuery.vehBodyUrl) requestData.vehBodyUrl = submitQuery.vehBodyUrl;
   
   console.log("图片上传状态:", {
     proxyUrl: submitQuery.proxyUrl,
     vehBodyUrl: submitQuery.vehBodyUrl,
     businessLicenseUrl: submitQuery.businessLicenseUrl
   });
   if (submitQuery.ownerIdAddress) requestData.ownerIdAddress = submitQuery.ownerIdAddress;
   
   // 处理企业信息
   if (ownerType.value == 1) {
     // 企业模式：传递企业相关信息
     if (companyNameItem && companyNameItem[companyNameItem.value]) {
       requestData.idName = companyNameItem[companyNameItem.value];
     }
     if (socialCreditCodeItem && socialCreditCodeItem[socialCreditCodeItem.value]) {
       requestData.ownerIdNum = socialCreditCodeItem[socialCreditCodeItem.value];
    //    requestData.licenseldNum = socialCreditCodeItem[socialCreditCodeItem.value]; // 也作为licenseldNum传递
     }
     if (companyAddressItem && companyAddressItem[companyAddressItem.value]) {
       requestData.ownerIdAddress = companyAddressItem[companyAddressItem.value];
     }
     if (businessLicenseItem && businessLicenseItem[businessLicenseItem.value]) {
       requestData.ownerPosImgUrl = businessLicenseItem[businessLicenseItem.value];
     }
     // 从submitQuery中获取营业执照图片（OCR处理过的）
     if (submitQuery.businessLicenseUrl) {
       requestData.ownerPosImgUrl = submitQuery.businessLicenseUrl;
     }
     requestData.ownerType = 1; // 标识为企业
     requestData.userIdType = "203"; // 企业信用代码类型
   } else {
     // 个人模式：传递个人身份证信息
	 if (submitQuery.idno) requestData.ownerIdNum = submitQuery.idno;
	 if (submitQuery.idName) requestData.idName = submitQuery.idName;
     if (submitQuery.ownerNegImgUrl) requestData.ownerNegImgUrl = extractImagePath(submitQuery.ownerNegImgUrl);
     if (submitQuery.ownerPosImgUrl) requestData.ownerPosImgUrl = extractImagePath(submitQuery.ownerPosImgUrl);
     requestData.ownerType = 0; // 标识为个人
   }
  return requestNew("/iaw/h5/order/web/upVehicle", {
    data: requestData,
  }).then((res) => {
    //判断是否需要输入身份信息
    if (res.code === 0 && res.data.flag === 2) {
		uni.navigateTo({
				url: `/pages/payment/payment?orderNo=${orderNo.value}`,
				animationType: 'pop-in',
				animationDuration: 500
			})
    } else {
      // uni.showToast({
      // 	title: res.message,
      // 	icon: 'none',
      // 	duration: 2000
      // });
      // submitQuery.idName = undefined
      uni.showModal({
        title: "提示",
        content: res.message,
        success: function (res) {
          if (res.confirm) {
            // 根据当前车主类型添加对应的表单项
            const fieldsToAdd = ownerType.value === 0 ? personalOwnerFields : enterpriseOwnerFields;
            formData.value.push(...fieldsToAdd);
			//单位订单不需要委托书，个人需要
			if(orderType != "PERSONAL_USER"){
				// 找到委托书的位置
				const weiTuoIndex = formData.value.findIndex(item => item.title === "委托书");
				formData.value.splice(weiTuoIndex,1)
			}
          } else if (res.cancel) {
          }
        },
      });
    }
  });
}
</script>
<style lang='scss' scoped>
</style>