import { action, computed, makeObservable, observable } from "mobx";
// 用户信息
import baseHttp from "@/service/base";
import BaseStore from "./baseStore";
import { UserDataType, UserInfos } from "@/model/userModel";
import { message } from "antd";
class UserConfig {
    static LOGINURI: string = "anth/login"
    static LIST: string = "user/list"
    static ADD: string = "user"
    static DELETE: string = "user"
    static EDIT: string = "user"
    static pover: string = "user/userPower"
    static team: string = "team/list"
    static per: string = "persMgmt/list"
    static serch: string = "user/serch"
    static getPatrol: string = "user/getPatrol"
    

}
class UserStore extends BaseStore<UserDataType> {
    _userinfo: UserInfos = {}; // 用户信息
    userDetail = {}
    isNeedLogin: boolean = false; // 是否需要登录
    poverDetail: boolean = false; // 是否展示民兵详情

    constructor() {
        super(UserConfig)
        makeObservable(this, {
            logOut: action,
            login: action,
            getTeam: action,
            getPer: action,
            serchUser: action,
            setPoverDe: action,
            setUserDetaul: action,
            getPatrol:action,
            _userinfo: observable,
            isNeedLogin: observable,
            poverDetail: observable,
            userDetail: observable,
            openLoginDilog: action,
            userInfo: computed,
        })
    }
    async getTeam() {
        return await baseHttp.get(UserConfig.team, null)
    }
    async getPer() {
        return await baseHttp.get(UserConfig.per, null)
    }
    async getPover() {
        return await baseHttp.get(UserConfig.pover, null)
    }
    async serchUser(params: any) {
        return await baseHttp.get(UserConfig.serch, {user_name:params})
    }
    async getPatrol() {
        return await baseHttp.get(UserConfig.getPatrol, {})
    }
    
    get userInfo(): UserInfos {
        if (!this._userinfo.token) {
            let token = window.localStorage.getItem("token")
            this._userinfo.token = token;

        }
        return this._userinfo;
    }
    logOut() {
        this._userinfo.token = "";
        window.localStorage.clear();
        this.item = "";
    }
    async login(params: UserInfos) {
        let param = {
            account: params.userName,
            password: params.passWord,
            genre: 1
        }
        try {
            let data = await baseHttp.post(UserConfig.LOGINURI, param)
            if (data.code !== 200) {
                message.error(data.msg)
                return false
            }

            window.localStorage.setItem("token", data.data.token ?? "");
            return true
        } catch (error) {
            console.log(error)
            return false
        }
    }
    openLoginDilog() {
        this.isNeedLogin = true;
    }
    closeLoginDilog() {
        this.isNeedLogin = false;
    }
    setPoverDe(status: boolean) {
        this.poverDetail = status
    }
    setUserDetaul(data) {
        this.userDetail = data
    }
}
const userStore = new UserStore();
export default userStore;