// stores/counter.js
import { defineStore } from "pinia";
import * as distributionApi from "@/http/api/market/distribution.js";
import * as consumeDiscountApi from "@/http/api/market/consumeDiscount.js";
import * as memberApi from "@/http/api/market/member.js";

export const upgradeTypes = [
  {
    value: "not_upgrade",
    label: "不自动升级",
  },
  {
    value: "invite",
    label: "邀请有效人数",
  },
  {
    value: "cost",
    label: "消费金额（不含退款）",
  },
];
// 分销
export const useDistributionStore = defineStore("distribution", {
  state: () => {
    return {
      //分销配置
      config: {
        isEnable: 0,
        openType: "pay",
        inviteCount: 1,
        inviteConsume: 0,
        payAmount: 0,
        rewardCount: 1,
        settlementDay: 1,
        upgradeType: "auto",
        notActivatedPage: null,
        levelConfigList: [],
      },
      //升级条件
      upgradeTypes,
    };
  },
  actions: {
    async getConfig() {
      const data = await distributionApi.getConfig();
      this.config = data;
      return this.config;
    },
    async editConfig(data) {
      const res = await distributionApi.editConfig({ ...this.config, ...data });
      this.getConfig();
      return res;
    },
  },
  unistorage: true, // 开启后对 state 的数据读写都将持久化
});

//新客立减
export const useNewUserDiscountStore = defineStore("newUserDiscount", {
  state: () => {
    return {
      config: {
        isEnable: 0,
      },
    };
  },
  actions: {
    async getConfig() {
      const data = await consumeDiscountApi.getConfig();
      this.config = data;
      return this.config;
    },
    async editConfig(data, isAutoResrefresh = true) {
      if (data.discountType === "FIXED") {
        data.randomDiscountList = [];
      }
      const res = await consumeDiscountApi.editConfig({
        ...this.config,
        ...data,
      });
      if (isAutoResrefresh) {
        setTimeout(() => {
          this.getConfig();
        }, 1500);
      }
      return res;
    },
  },
  unistorage: true, // 开启后对 state 的数据读写都将持久化
});

//超级会员
export const useSuperVipStore = defineStore("superVip", {
  state: () => {
    return {
      config: {
        isOpen: '',
        configList:[]
      },
      vipLevelList:[]
    };
  },
  actions: {
    setVipLevelList(list){
      this.vipLevelList = list||[];
    },
    editPlan(index,plan){
      this.config.configList[index] = plan;
    },
    addPlan(plan){
      this.config.configList.push(plan);
    },
    async getConfig() {
      const data = await memberApi.getConfig();
      data.memberPriceShopType = data.memberPriceShopType.toLowerCase();
      this.config = data;
      return this.config;
    },
    async editConfig(data) {
      const res = await memberApi.editConfig({ ...this.config,memberPriceShopType:this.config.memberPriceShopType.toUpperCase(), ...data });
      this.getConfig();
      return res;
    },
  },
  unistorage: true, // 开启后对 state 的数据读写都将持久化
});
