"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const utils_api = require("../../utils/api.js");
if (!Array) {
  const _easycom_wd_icon2 = common_vendor.resolveComponent("wd-icon");
  const _easycom_wd_popup2 = common_vendor.resolveComponent("wd-popup");
  (_easycom_wd_icon2 + _easycom_wd_popup2)();
}
const _easycom_wd_icon = () => "../../uni_modules/wot-design-uni/components/wd-icon/wd-icon.js";
const _easycom_wd_popup = () => "../../uni_modules/wot-design-uni/components/wd-popup/wd-popup.js";
if (!Math) {
  (_easycom_wd_icon + _easycom_wd_popup)();
}
const _sfc_main = {
  __name: "chooseCar",
  setup(__props) {
    const userInfo = common_vendor.index.getStorageSync("carUserInfo");
    const popShow_rule = common_vendor.ref(false);
    const popShow_common = common_vendor.ref(false);
    const info = common_vendor.ref({});
    const pageSize = common_vendor.ref(8);
    const currentPage = common_vendor.ref(1);
    const total = common_vendor.ref(0);
    const dataList = common_vendor.ref([]);
    const isLoading = common_vendor.ref(false);
    const hasMore = common_vendor.ref(true);
    const selectedPassengers = common_vendor.ref([]);
    common_vendor.ref("");
    const popShow_pay = common_vendor.ref(false);
    const payList = common_vendor.ref([
      {
        text: "微信支付",
        icon: "/static/images/pay/wx.png",
        isCheck: false
      },
      {
        text: "电子交通卡支付",
        icon: "/static/images/pay/card.png",
        isCheck: false
      }
    ]);
    const selectedPay = common_vendor.ref(null);
    const guizea1 = common_vendor.ref(0);
    const guizea2 = common_vendor.ref(0);
    const guizea3 = common_vendor.ref(0);
    const guizea4 = common_vendor.ref(0);
    const guizea5 = common_vendor.ref(0);
    const guizea6 = common_vendor.ref(0);
    const payParams = common_vendor.ref({
      timeStamp: "",
      nonceStr: "",
      signType: "",
      paySign: "",
      appId: "",
      // prepay_id:'',
      package: ""
    });
    const getRouteParts = (str) => {
      if (typeof str === "string" && str.length > 0) {
        return str.split("→");
      }
      return [];
    };
    common_vendor.onLoad((options) => {
      if (options.info) {
        info.value = JSON.parse(decodeURIComponent(options.info));
        console.log(info.value);
      }
    });
    function handleRule() {
      popShow_rule.value = true;
      getTuigai();
    }
    function handleCloseRule() {
      popShow_rule.value = false;
    }
    async function getTuigai() {
      const res = await utils_api.myRequest({
        url: "/carBook/cnqueryHb.action",
        data: {}
      });
      if (res.code == 200) {
        guizea1.value = res.data.refundRule.a1.start;
        guizea2.value = res.data.refundRule.a2.start;
        guizea3.value = res.data.refundRule.a2.end;
        guizea4.value = res.data.refundRule.a2.fee * 100;
        guizea5.value = res.data.refundRule.a3.end;
        guizea6.value = res.data.refundRule.a3.fee * 100;
      } else {
        common_vendor.index.showToast({
          title: res.message,
          icon: "none"
        });
      }
    }
    function handleMore() {
      popShow_common.value = true;
      getCommonData();
    }
    const getCommonData = async () => {
      if (isLoading.value || !hasMore.value)
        return;
      isLoading.value = true;
      let data = {
        page: currentPage.value,
        rows: pageSize.value,
        // mobile: userInfo.mobile?userInfo.mobile:''
        uid: userInfo.id ? userInfo.id : ""
      };
      try {
        const res = await utils_api.myRequest({
          url: "/ctUserlist.action",
          data
        });
        if (res.code === 200) {
          const formattedRows = res.rows.map((item) => ({
            ...item,
            // 保留原有数据
            isCheck: false
            // 新增 isCheck 参数，默认 false
          }));
          formattedRows.forEach((row) => {
            const isSelected = selectedPassengers.value.some((p) => p.id === row.id);
            if (isSelected) {
              row.isCheck = true;
            }
          });
          if (currentPage.value === 1) {
            dataList.value = formattedRows;
          } else {
            dataList.value.push(...formattedRows);
          }
          total.value = res.total;
        } else {
          common_vendor.index.showToast({ title: "数据获取失败", icon: "none" });
        }
      } catch (err) {
        console.error("请求异常", err);
        common_vendor.index.showToast({ title: "网络异常", icon: "none" });
      } finally {
        isLoading.value = false;
      }
    };
    function loadNextPage() {
      if (dataList.value.length < total.value) {
        currentPage.value++;
        getCommonData();
      } else {
        common_vendor.index.showToast({
          title: "没有更多数据了",
          icon: "none"
        });
      }
    }
    const toggleCheck = (item) => {
      item.isCheck = !item.isCheck;
      if (item.isCheck) {
        selectedPassengers.value.push(item);
      } else {
        selectedPassengers.value = selectedPassengers.value.filter((p) => p.id !== item.id);
      }
    };
    function handleCloseCommon() {
      popShow_common.value = false;
    }
    function handleConfirm() {
      popShow_common.value = false;
    }
    function handleAdd() {
      common_vendor.index.navigateTo({
        url: "/pages/handlePeople/handlePeople"
      });
    }
    function handleUp(item) {
      let info2 = encodeURIComponent(JSON.stringify(item));
      common_vendor.index.navigateTo({
        url: `/pages/handlePeople/handlePeople?info=${info2}`
      });
    }
    const handlePay = () => {
      popShow_pay.value = true;
    };
    const handleClose = () => {
      popShow_pay.value = false;
    };
    const togglePay = (item) => {
      payList.value.forEach((pay) => {
        pay.isCheck = false;
      });
      item.isCheck = true;
      selectedPay.value = item;
    };
    const payTime = () => {
      if (!selectedPay.value) {
        common_vendor.index.showModal({
          content: "请选择支付方式"
        });
        return;
      }
      if (selectedPay.value.text == "微信支付") {
        payWei();
      } else if (selectedPay.value.text == "电子交通卡支付") {
        jiaotongka();
      }
    };
    const jiaotongka = async () => {
      const isPassengerEmpty = common_vendor.computed(() => selectedPassengers.value.length === 0);
      if (isPassengerEmpty.value) {
        common_vendor.index.showToast({
          title: "请选择乘车人",
          icon: "none"
        });
        return;
      }
      const userListStr = common_vendor.computed(() => {
        return selectedPassengers.value.map((item) => item.id).join(",");
      });
      const res = await utils_api.myRequest({
        url: "/cardorder.action",
        method: "POST",
        // 明确指定请求方法为POST
        data: {
          userId: userInfo.id,
          schedueId: info.value.id,
          userList: userListStr.value,
          payAmount: info.value.price,
          carNumber: info.value.car_number,
          contain: info.value.contain,
          ciDate: info.value.s_date,
          ciTime: info.value.ci_time,
          route: info.value.route,
          routeEnd: info.value.route_end
        }
      });
      if (res.code == 200) {
        common_vendor.index.showToast({
          title: res.message,
          icon: "none"
        });
      } else {
        common_vendor.index.showToast({
          title: res.message,
          icon: "none"
        });
      }
    };
    const payWei = async () => {
      const isPassengerEmpty = common_vendor.computed(() => selectedPassengers.value.length === 0);
      if (isPassengerEmpty.value) {
        common_vendor.index.showToast({
          title: "请选择乘车人",
          icon: "none"
        });
        return;
      }
      const userListStr = common_vendor.computed(() => {
        return selectedPassengers.value.map((item) => item.id).join(",");
      });
      const res = await utils_api.myRequest({
        url: "/tApporder_pay.action",
        method: "POST",
        // 明确指定请求方法为POST
        data: {
          userId: userInfo.id,
          schedueId: info.value.id,
          userList: userListStr.value,
          payAmount: info.value.price,
          carNumber: info.value.car_number,
          contain: info.value.contain,
          ciDate: info.value.s_date,
          ciTime: info.value.ci_time,
          route: info.value.route,
          routeEnd: info.value.route_end
        }
      });
      if (res.code == 200) {
        payParams.value = {
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: "prepay_id=" + res.data.prepay_id,
          signType: res.data.signType,
          paySign: res.data.paySign
          // prepay_id:res.data.prepay_id
        };
        invokeWechatPay();
      }
    };
    const invokeWechatPay = () => {
      console.log(payParams.value, "支付");
      common_vendor.index.requestPayment({
        ...payParams.value,
        success: (res) => {
          common_vendor.index.showToast({ title: "支付成功" });
          setTimeout(function() {
            common_vendor.index.switchTab({
              url: "/pages/order/order"
            });
          }, 1e3);
        },
        fail: (err) => {
          common_vendor.index.showToast({ title: "支付失败", icon: "none" });
          console.error("支付失败", err);
        }
      });
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.t(info.value.s_date),
        b: common_vendor.t(info.value.ci_time),
        c: common_vendor.t(info.value.car_number),
        d: common_vendor.t(info.value.contain),
        e: common_assets._imports_2$1,
        f: common_vendor.f(getRouteParts(info.value.route_end), (item2, index2, i0) => {
          return {
            a: common_vendor.t(item2),
            b: index2 > 0 ? 1 : "",
            c: index2
          };
        }),
        g: common_vendor.o(handleRule),
        h: common_vendor.f(selectedPassengers.value, (item, index, i0) => {
          return {
            a: common_vendor.t(item.username),
            b: "ce976c48-0-" + i0,
            c: index,
            d: common_vendor.o(($event) => toggleCheck(item), index)
          };
        }),
        i: common_vendor.p({
          name: "check",
          color: "#fff",
          size: "8"
        }),
        j: common_vendor.o(handleMore),
        k: common_vendor.f(selectedPassengers.value, (item, index, i0) => {
          return {
            a: common_vendor.o(($event) => toggleCheck(item), index),
            b: "ce976c48-1-" + i0,
            c: common_vendor.t(item.username),
            d: common_vendor.t(item.sfzh),
            e: common_vendor.t(item.cardNumber),
            f: index
          };
        }),
        l: common_vendor.p({
          name: "minus-circle",
          color: "#FF8205",
          size: "22"
        }),
        m: common_vendor.p({
          name: "phone",
          color: "#FF8205",
          size: "25"
        }),
        n: _ctx.value,
        o: common_vendor.o(($event) => _ctx.value = $event.detail.value),
        p: common_assets._imports_1$3,
        q: common_vendor.p({
          name: "chevron-right",
          color: "#ABA6A6",
          size: "18"
        }),
        r: common_vendor.t(info.value.price),
        s: common_vendor.o(handlePay),
        t: common_vendor.t(guizea1.value),
        v: common_vendor.t(guizea2.value),
        w: common_vendor.t(guizea3.value),
        x: common_vendor.t(guizea4.value),
        y: common_vendor.t(guizea5.value),
        z: common_vendor.t(guizea6.value),
        A: common_vendor.o(handleCloseRule),
        B: common_vendor.o(($event) => popShow_rule.value = $event),
        C: common_vendor.p({
          position: "bottom",
          ["safe-area-inset-bottom"]: true,
          ["close-on-click-modal"]: false,
          modelValue: popShow_rule.value
        }),
        D: common_vendor.p({
          name: "close",
          color: "#ABA6A6",
          size: "20"
        }),
        E: common_vendor.o(handleCloseCommon),
        F: common_vendor.o(handleAdd),
        G: common_vendor.f(dataList.value, (item, index, i0) => {
          return {
            a: common_vendor.o(($event) => handleUp(item), index),
            b: "ce976c48-7-" + i0 + ",ce976c48-5",
            c: common_vendor.t(item.username),
            d: common_vendor.t(item.sfzh),
            e: common_vendor.t(item.mobile),
            f: common_vendor.t(item.cardNumber),
            g: item.isCheck,
            h: common_vendor.o(($event) => toggleCheck(item), index),
            i: index
          };
        }),
        H: common_vendor.p({
          name: "edit-outline",
          color: "#ABA6A6",
          size: "17"
        }),
        I: isLoading.value
      }, isLoading.value ? {} : {}, {
        J: common_vendor.o(loadNextPage),
        K: common_vendor.o(handleConfirm),
        L: common_vendor.o(($event) => popShow_common.value = $event),
        M: common_vendor.p({
          position: "bottom",
          ["safe-area-inset-bottom"]: true,
          ["close-on-click-modal"]: false,
          modelValue: popShow_common.value
        }),
        N: common_vendor.p({
          name: "close-bold",
          color: "#ABA6A6",
          size: "22"
        }),
        O: common_vendor.o(handleClose),
        P: common_vendor.f(payList.value, (item, index, i0) => {
          return {
            a: item.icon,
            b: common_vendor.t(item.text),
            c: item.isCheck,
            d: common_vendor.o(($event) => togglePay(item), index),
            e: index
          };
        }),
        Q: common_vendor.o(payTime),
        R: common_vendor.o(($event) => popShow_pay.value = $event),
        S: common_vendor.p({
          position: "bottom",
          ["safe-area-inset-bottom"]: true,
          ["close-on-click-modal"]: false,
          modelValue: popShow_pay.value
        })
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-ce976c48"], ["__file", "D:/创海项目/CH2025/约车小程序/yueche_app/pages/chooseCar/chooseCar.vue"]]);
wx.createPage(MiniProgramPage);
