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";
import Config from "@/util/config";
class UserConfig {
    static LOGINURI: string = "/v1/anth/login/pc"
    static LIST: string = "/v1/user/list"
    static ADD: string = "/v1/user"
    static DELETE: string = "/v1/user"
    static EDIT: string = "/v1/user"
    static pover: string = "/v1/user/userPower"
    static team: string = "/v1/team/list"
    static per: string = "/v1/persMgmt/list"
    static serch: string = "/v1/user/serch"
    static getPatrol: string = "/v1/user/getPatrol"
    static videoLogin: string = "/api/v1/login"
    static siteList: string = "/v1/user/site"
    static reSetPwd: string = "/v1/user/reSetPwd"
    



}
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,
            getSite: 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, {})
    }

    async getSite(id) {
        return await baseHttp.put(UserConfig.siteList + "/" + id.id, id)
    }

    async reSetPwd(id) {
     await baseHttp.put(UserConfig.reSetPwd+"/"+id,{})
        message.success("密码重置成功")
    }



    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 = "";
        window.location.href = '#/login'
    }
    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
        }
    }

    async loginVideo() {
        try {
            let data = await baseHttp.get(Config.videoApi + UserConfig.videoLogin, {
                username: "easycvr",
                password: "4092c09db0af030641a977d76044de4f",
            })
            window.localStorage.setItem("video-token", data.EasyDarwin.Body.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;