import { defineStore } from "pinia";

import { api } from "boot/axios";
import { Notify } from 'quasar'
import { notification} from 'src/utils/mobile'

export const useSelfStore = defineStore("self", {
  state: () => ({
    user: {},
    shop: {},
    orders: [],
    customers: {},
    buyer:{},
    cooperate: {
      shop_auditing_num:0,
      lottery_auditing_num:0
    },
    setting:{}
  }),
  getters: {
    isLogin: (state) => !!state.user.id
  },
  actions: {
    refresh(){
      return api.getLoginUser().then(res=>{
        this.setInfo(res)
      })
    },
    setInfo({ orders, shop, seller,customers,buyer,cooperate,...setting }) {
      let badge = 0
      let msg = [
        {key: 'pending_num',name:'待接单'},
        {key:"draft_num",name:'待出票'},
        {key:"win_num",name:'待派奖'},
        {key:"withdraw_num",name:'用户提现'}
      ].reduce((total,item)=>{
        if(orders[item.key]>0 && orders[item.key]>(this.orders[item.key]||0)) {
          total.push(`${orders[item.key]}条${item.name}`)
        }
        badge += orders[item.key]||0
        return total
      },[])
      if( msg.length && this.user.id ){
        notification({
          title:'待处理提醒',
          description: `您有${msg.join('、')}未处理，请及时处理！`,
          color: 'primary',
          sound: !!this.user.id,
          badge
        })
      }
      this.user = seller;
      this.shop = shop;
      this.orders = orders;
      this.customers = customers;
      this.buyer = buyer
      this.cooperate = cooperate || {}
      this.setting = setting || {}
    },
    clear() {
      this.user = {};
      this.shop = {};
      this.orders = [];
      this.customers = {};
      this.buyer = {}
      this.cooperate = {}
      this.setting = {};
    }
  }
});
