"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
const _sfc_main = {
  __name: "testproductmain",
  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);
    const searchText = common_vendor.ref("");
    const stateText = common_vendor.ref({
      topBtn: "编辑",
      bottomBtn: "去结算"
    });
    common_vendor.onShow(() => {
      getProductCart();
    });
    common_vendor.onLoad(() => {
      getProductCart();
      getFonts();
    });
    const getProductCart = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/cart/list",
        method: "POST",
        header: {
          "cookie": common_vendor.wx$1.getStorageSync("cookie")
        }
      });
      console.log("res--->", res.data);
      if (res.data.code === 1) {
        products.value = res.data.data;
        products.value.forEach((item) => {
          item.checked = false;
        });
      }
      console.log("products-->", products.value);
    };
    const deleteProduct = async (idArr) => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/cart/delete",
        method: "POST",
        data: idArr,
        header: {
          "cookie": common_vendor.wx$1.getStorageSync("cookie")
        }
      });
      console.log(res.data);
      if (res.data.code === 1) {
        common_vendor.index.reLaunch({
          url: "/pages/Shopping-cart/testproductmain/testproductmain"
        });
        totalPrice.value = 0;
      }
    };
    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("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;
      console.log("event--->", event.detail);
      if (event.detail.value.length) {
        products.value[parseInt(event.detail.value)].checked = true;
      } else {
        products.value.forEach((item) => {
          item.checked = false;
        });
      }
      for (let key in event.detail.value) {
        let temp = parseInt(event.detail.value[parseInt(key)]);
        computed(products.value[temp].cartGoodVO.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.push(products.value[parseInt(item)].id);
        }
      });
      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.cartGoodVO.price, item.quantity);
        });
        checkedData.value = products.value.map((item) => item.id);
        console.log("checkedData--->", checkedData.value);
      } else {
        products.value.forEach((item) => {
          item.checked = false;
        });
      }
    };
    const decrease = (index, item) => {
      console.log("decrease,index-->", index);
      console.log("decrease,item-->", item);
      if (products.value[index].quantity > 1) {
        products.value[index].quantity -= 1;
        if (item && products.value[index].quantity >= 1) {
          computed(-products.value[index].cartGoodVO.price, 1);
        }
      }
    };
    const increase = (index, item) => {
      console.log("increase,index-->", index);
      console.log("increase,item-->", item);
      if (products.value[index].quantity < products.value[index].cartGoodVO.inventory) {
        if (item && products.value[index].quantity <= products.value[index].cartGoodVO.inventory) {
          computed(products.value[index].cartGoodVO.price, 1);
        }
        products.value[index].quantity += 1;
      }
    };
    const computed = (price, quantity) => {
      totalPrice.value = common_vendor.round(totalPrice.value + price * quantity, 2);
      return totalPrice.value;
    };
    const jump_product = (gid) => {
      common_vendor.index.navigateTo({
        url: "../../../pages/store-home/ProductDetails/ProductDetails?gid=" + JSON.stringify(gid)
      });
    };
    const editOrSettle = async () => {
      if (stateText.value.bottomBtn === "去结算") {
        if (checkedData.value.length === 0) {
          return;
        }
        const res = await common_vendor.index.request({
          url: api_request.baseUrl + "/cart/submit/error",
          method: "POST",
          header: {
            "cookie": common_vendor.wx$1.getStorageSync("cookie")
          }
        });
        console.log(res.data);
        if (res.data.data.length != 0) {
          common_vendor.index.showModal({
            title: "提示",
            content: "购物车商品发生改变，点击移除变化商品",
            showCancel: false,
            success: (e) => {
              if (e.confirm) {
                common_vendor.index.reLaunch({
                  url: "/pages/Shopping-cart/productmain/productmain"
                });
              }
            }
          });
        } else {
          common_vendor.index.navigateTo({
            url: "/pages/order/product-waitpay/product-waitpay?cartInfo=" + JSON.stringify(checkedData.value)
          });
          checkedData.value.splice(0, checkedData.value.length);
        }
      } else {
        common_vendor.index.showModal({
          title: "提示",
          content: "是否删除商品?",
          success: (e) => {
            if (e.confirm) {
              deleteProduct(checkedData.value);
            } else if (e.cancel)
              return;
          }
        });
      }
    };
    const changeState = () => {
      if (stateText.value.topBtn === "编辑") {
        stateText.value.topBtn = "完成", stateText.value.bottomBtn = "删除";
      } else {
        stateText.value.topBtn = "编辑", stateText.value.bottomBtn = "去结算";
      }
    };
    const getFonts = () => {
      common_vendor.index.loadFontFace({
        family: "FangZhengFonts",
        source: `url("https://carbon2.obs.cn-north-4.myhuaweicloud.com/fonts/FangZhengFonts.TTF")`,
        success: (res) => {
          console.log("success", res);
        },
        fail: (err) => {
          console.log("err", err);
        }
      });
    };
    return (_ctx, _cache) => {
      return {
        a: searchText.value,
        b: common_vendor.o(($event) => searchText.value = $event.detail.value),
        c: common_vendor.t(stateText.value.topBtn),
        d: common_vendor.o(changeState),
        e: common_vendor.s(getTextStyle(1)),
        f: common_vendor.o(($event) => changeType(1)),
        g: common_vendor.s(getTextStyle(2)),
        h: common_vendor.o(($event) => changeType(2)),
        i: common_vendor.f(products.value, (item, index, i0) => {
          return common_vendor.e(current.value == 1 ? {
            a: index,
            b: item.checked,
            c: item.cartGoodVO.goodImg.split(";")[0],
            d: common_vendor.o(($event) => jump_product(item.goodId), index),
            e: common_vendor.t(item.cartGoodVO.name),
            f: common_vendor.o(($event) => jump_product(item.goodId), index),
            g: common_vendor.t(item.cartGoodVO.type),
            h: common_vendor.o(($event) => jump_product(item.goodId), index),
            i: common_vendor.t(item.cartGoodVO.price),
            j: common_vendor.o(($event) => jump_product(item.goodId), index),
            k: common_vendor.o(($event) => decrease(index, item.checked), index),
            l: common_vendor.t(item.quantity),
            m: common_vendor.o(($event) => increase(index, item.checked), index)
          } : {}, current.value == 2 ? {} : {}, {
            n: index
          });
        }),
        j: current.value == 1,
        k: current.value == 2,
        l: common_vendor.o(checkBoxChange),
        m: allCheck.value,
        n: common_vendor.o(allChecked),
        o: common_vendor.t(totalPrice.value),
        p: common_vendor.t(stateText.value.bottomBtn),
        q: common_vendor.o(editOrSettle)
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-25074af5"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/Shopping-cart/productmain/testproductmain.vue"]]);
wx.createPage(MiniProgramPage);
