/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

import http from '@/utils/http.js'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    id: 0,
    name: '',
    mobile: '',
    userName: '',
    shopId: '',
    type: '',
    employeeId: '',
    shopStatus: -2,
    isPassShop: ''
  }),
  actions: {
    login () {
      return new Promise((resolve) => {
        http.request({
          url: '/shop/shopDetail/info',
          method: 'get'
        }).then(({ data }) => {
          this.updateName(data.shopName)
          this.updateMobile(data.mobile)
          this.updateType(data.type)
          this.updateUserName(data.userName)
          this.updateShopStatus(data.shopStatus)
          this.updateShopId(data.shopId)
          this.updateEmployeeId(data.employeeId)
          this.updateIsPassShop(data.isPassShop)
          resolve(data)
        })
      })
    },
    getUserInfo () {
      if (this.shopStatus !== -2) {
        // 已经获取值，直接返回
        return this
      }
      return new Promise((resolve) => {
        http.request({
          url: '/shop/shopDetail/info',
          method: 'get'
        }).then(({ data }) => {
          this.updateName(data.shopName)
          this.updateMobile(data.mobile)
          this.updateType(data.type)
          this.updateUserName(data.userName)
          this.updateShopStatus(data.shopStatus)
          this.updateShopId(data.shopId)
          this.updateEmployeeId(data.employeeId)
          this.updateIsPassShop(data.isPassShop)
          resolve(data)
        })
      })
    },
    updateId (id) {
      this.id = id
    },
    updateName (name) {
      this.name = name
    },
    updateType (type) {
      this.type = type
    },
    updateMobile (mobile) {
      this.mobile = mobile
    },
    updateUserName (userName) {
      this.userName = userName
    },
    updateShopId (shopId) {
      this.shopId = shopId
    },
    updateEmployeeId (employeeId) {
      this.employeeId = employeeId
    },
    updateShopStatus (shopStatus) {
      this.shopStatus = shopStatus
    },
    updateIsPassShop (isPassShop) {
      this.isPassShop = isPassShop
    }
  }
})
