"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
const _sfc_main = {
  __name: "productmain",
  setup(__props) {
    const products = common_vendor.ref([]);
    const current = common_vendor.ref(1);
    const allCheck = common_vendor.ref(false);
    const checkedData = common_vendor.ref([]);
    const totalPrice = common_vendor.ref(0);
    wx.getStorageSync("userInfo");
    common_vendor.onMounted(() => {
    });
    common_vendor.onShow(() => {
      getProductCart();
    });
    const getProductCart = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/cart/list",
        method: "POST",
        header: {
          "cookie": wx.getStorageSync("cookie")
        }
      });
      if (res.data.code === 1) {
        products.value = res.data.data;
        products.value.forEach((item) => {
          item.checked = false;
        });
      }
      console.log("res--->", res);
      console.log("products-->", products.value);
    };
    const changeType = async (num) => {
      current.value = num;
    };
    const getTextStyle = (num) => ({
      "border-bottom": current.value === num ? "2px solid orange" : "2px solid  #ffffff"
    });
    const checkBoxChange = (event) => {
      console.log("方法被触发");
      console.log("length---->", event.detail.value.length);
      if (event.detail.value.length == products.value.length) {
        allCheck.value = true;
      } else {
        allCheck.value = false;
      }
      checkedData.value = [{}];
      totalPrice.value = 0;
      let count = 0;
      console.log("event--->", event.detail);
      products.value[parseInt(event.detail.value)].checked = true;
      for (let key in event.detail.value) {
        let temp = parseInt(event.detail.value[parseInt(key)]);
        computed(products.value[temp].goodVO.price, products.value[temp].quantity);
      }
      event.detail.value.forEach((item) => {
        products.value[parseInt(item)].checked = true;
      });
      products.value.forEach((x) => {
        event.detail.value.forEach((y) => {
          if (x.value == y) {
            x.checked = true;
          }
        });
      });
      event.detail.value.forEach((item) => {
        console.log("products.value[parseInt(item)].goodId", products.value[parseInt(item)].goodId);
        if (products.value[parseInt(item)] !== null) {
          checkedData.value[count] = {
            goodId: products.value[parseInt(item)].goodId,
            quantity: products.value[parseInt(item)].quantity
          };
          count += 1;
        }
      });
      console.log("checkedData--->", checkedData.value);
    };
    const allChecked = () => {
      allCheck.value = !allCheck.value;
      totalPrice.value = 0;
      if (allCheck.value == true) {
        products.value.forEach((item) => {
          item.checked = true;
          computed(item.goodVO.price, item.quantity);
        });
        checkedData.value = products.value.map((item) => ({
          goodId: item.goodId,
          quantity: item.quantity
        }));
        console.log("checkedData--->", checkedData.value);
      } else {
        products.value.forEach((item) => {
          item.checked = false;
        });
      }
    };
    const decrease = (index, item) => {
      if (products.value[index].quantity > 1) {
        products.value[index].quantity -= 1;
        if (item && products.value[index].quantity >= 1) {
          computed(-products.value[index].goodVO.price, 1);
        }
      }
    };
    const increase = (index, item) => {
      if (products.value[index].quantity <= 99) {
        products.value[index].quantity += 1;
      }
      if (item) {
        computed(products.value[index].goodVO.price, 1);
      }
    };
    const computed = (price, quantity) => {
      totalPrice.value += price * quantity;
      return totalPrice.value;
    };
    const jump_product = () => {
      common_vendor.index.navigateTo({
        url: "../../../pages/store-home/ProductDetails/ProductDetails"
      });
    };
    const goToBuy = () => {
      console.log("要传到订单详情页面的checked--->", checkedData.value);
      common_vendor.index.navigateTo({
        url: "/pages/order/product-waitpay/product-waitpay?cartInfo=" + JSON.stringify(checkedData.value)
      });
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.s(getTextStyle(1)),
        b: common_vendor.o(($event) => changeType(1)),
        c: common_vendor.s(getTextStyle(2)),
        d: common_vendor.o(($event) => changeType(2)),
        e: common_vendor.f(products.value, (item, index, i0) => {
          return common_vendor.e(current.value == 1 ? {
            a: index,
            b: item.checked,
            c: item.goodVO.goodImg,
            d: common_vendor.o(jump_product),
            e: common_vendor.t(item.goodVO.name),
            f: common_vendor.o(jump_product),
            g: common_vendor.t(item.goodVO.type),
            h: common_vendor.o(jump_product),
            i: common_vendor.t(item.goodVO.price),
            j: common_vendor.o(jump_product),
            k: common_vendor.o(($event) => decrease(index, item.checked)),
            l: common_vendor.t(item.quantity),
            m: common_vendor.o(($event) => increase(index, item.checked))
          } : {}, current.value == 2 ? {} : {}, {
            n: index
          });
        }),
        f: current.value == 1,
        g: current.value == 2,
        h: common_vendor.o(checkBoxChange),
        i: allCheck.value,
        j: common_vendor.o(allChecked),
        k: common_vendor.t(totalPrice.value),
        l: common_vendor.o(goToBuy)
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-641af6de"], ["__file", "D:/jiangchengfeiyi-xiaochengxu/pages/Shopping-cart/productmain/productmain.vue"]]);
my.createPage(MiniProgramPage);
