import { defineStore } from "pinia";
import { getUserInfo } from "@/api/user.js";
import { LOGIN_STATUS, UID, PLATFORM, EXPIRES_TIME } from "@/config/cache.js";
import Cache from "@/utils/cache.js";
import { USER_INFO } from "@/config/cache.js";
import { getMiniProgramData } from "@/api/api";

export const useAppStore = defineStore("app", {
  state: () => {
    return {
      token: Cache.get(LOGIN_STATUS) || "",
      backgroundColor: "#fff",
      userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
      uid: Cache.get(UID) || null,
      homeActive: false,
      chatUrl: Cache.get("chatUrl") || "",
      systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : "",
      productType: Cache.get("productType") || "",
      navbarHeight: 0,
      userPanelInfo: {},
      refreshArticles: false,
      indexRefreshArticles: false,
      wxConfig: {
        auditModeEnabled: false, // 此字段在此小程序停用
        storeAuditModeEnabled: false, // 使用此字段控制审核时隐藏与显示
        carouselImages: [
          {
            id: 0,
            imageUrl: "",
            jumpUrl: "",
            sort: 0,
          },
        ],
        homePopupImage: "",
        inviteImage: "",
        isOpen: true,
        logoImage: "",
        mailingAddress: "",
        signCoinValue: 0,
        signGrowthValue: 0,
      },
      // 店铺信息
      shopInfo: {
        name: "水贝001号店铺",
        shopId: "001",
      },
      merchantId: null,
      // 首页展示刷新标识，当浏览了新店铺主页/扫店铺码时，改为true，首页onshow时判断true刷新列表并改为false
      indexRefreshFlag: false,
      cartRefreshFlag: false,
      cProductAttr: [],
      cProductAttrValue: [],
    };
  },
  getters: {
    uidComputed: (state) => state.uid,
    tokenComputed: (state) => state.token,
    isLogin: (state) => !!state.token,
    homeActiveComputed: (state) => state.homeActive,
    productTypeComputed: (state) => state.productType,
    chatUrlComputed: (state) => state.chatUrl,
    $userInfo: (state) => state.userInfo,
    navbarHeightGetter: (state) => state.navbarHeight,
    userPanelInfoGetter: (state) => state.userPanelInfo,
    $wxConfig: (state) => state.wxConfig,
    shopInfoGetter: (state) => state.shopInfo,
    merchantIdGetter: (state) => state.merchantId,
  },
  actions: {
    SET_REFRESH(val) {
      this.refreshArticles = val;
    },
    SET_CPATTR(val) {
      this.cProductAttr = val;
    },
    SET_CPATTRVALUE(val) {
      this.cProductAttrValue = val;
    },
    SET_CART_REFRESH(val) {
      this.cartRefreshFlag = val;
    },
    setIndexRefersh(val) {
      this.indexRefreshFlag = val;
    },
    SET_NAVBAR_HEIGHT(val) {
      this.navbarHeight = val;
    },
    LOGIN(opt) {
      this.token = opt.token;
      Cache.set(LOGIN_STATUS, opt.token);
    },
    SETUID(val) {
      this.uid = val;
      Cache.set(UID, val);
    },
    UPDATE_LOGIN(token) {
      this.token = token;
    },
    LOGOUT() {
      this.token = undefined;
      this.uid = undefined;
      this.merchantId = null;
      this.userInfo = null;
      Cache.clear(LOGIN_STATUS);
      Cache.clear(UID);
      Cache.clear(USER_INFO);
      Cache.clear(EXPIRES_TIME);
    },
    BACKGROUND_COLOR(color) {
      this.color = color;
      document.body.style.backgroundColor = color;
    },
    UPDATE_USERINFO(userInfo) {
      this.userInfo = userInfo;
      Cache.set(USER_INFO, userInfo);
    },
    OPEN_HOME() {
      this.homeActive = true;
    },
    CLOSE_HOME() {
      this.homeActive = false;
    },
    SET_CHATURL(chatUrl) {
      this.chatUrl = chatUrl;
    },
    SYSTEM_PLATFORM(systemPlatform) {
      this.systemPlatform = systemPlatform;
      Cache.set(PLATFORM, systemPlatform);
    },
    changInfo(payload) {
      this.userInfo[payload.amount1] = payload.amount2;
      Cache.set(USER_INFO, this.userInfo);
    },
    PRODUCT_TYPE(productType) {
      this.productType = productType;
      Cache.set("productType", productType);
    },
    async USERINFO(force) {
      try {
        const res = await getUserInfo();
        this.UPDATE_USERINFO(res.data);
        return res.data;
      } catch (e) {}
    },
    // 获取小程序基本配置
    async GET_WX_CONFIG() {
      try {
        const { data } = await getMiniProgramData();
        const configDate = {
          ...data,
          alipayList: data.alipayList ? JSON.parse(data.alipayList) : [],
          bankCardList: data.bankCardList ? JSON.parse(data.bankCardList) : [],
        };
        this.wxConfig = configDate;
        return configDate;
      } catch (e) {
        console.log("GET_WX_CONFIG-stores-res", e);
        throw e;
      }
    },
    UPDATE_userPanelInfo(userPanelInfo) {
      this.userPanelInfo = userPanelInfo;
    },
    UPDATE_MERCHANT_ID(merchantId) {
      this.merchantId = merchantId;
    },
  },
});
