import { defineStore } from "pinia";

import { useSearchInfoArray, useSearchCodeInfoArray } from "@/hooks/useSearch"; //转换成表单可以用的数据
import piniaPersistConfig from "@/config/piniaPersist";

export const useUserStore = defineStore({
    id: "wms-user",
    state: () => ({
        newUserToken: "",
        refreshToken: "",
        phpToken: "",
        userInfo: {},
        orgIdArr: [],
        orgCode: "",
        orgId: 0,
        orgName: "",
        warehouse: [],
        dept: []
    }),
    getters: {},
    actions: {
        // Set Token
        setToken(token: string) {
            this.newUserToken = token;
        },
        setRefreshToken(refreshToken: string) {
            this.refreshToken = refreshToken;
        },
        setPhpToken(token: string) {
            this.phpToken = token;
        },
        // 用户信息
        setUserInfo(userInfo: Record<string, any>) {
            this.userInfo = userInfo;
        },
        // 组织
        setOrgIdArr(orgs: any[]) {
            this.orgIdArr = useSearchInfoArray(orgs);
        },
        //组织ID
        setOrgId(orgId: any) {
            this.orgId = orgId;
        },
        //组织code
        setOrgCode(orgCode: any) {
            this.orgCode = orgCode ? orgCode : "101";
        },
        //默认组织名称
        setOrgName(orgName: any) {
            this.orgName = orgName;
        },
        //组织下的部门
        setDept(dept: any) {
            this.dept = useSearchInfoArray(dept);
        },
        //组织下的仓库
        setWarehouse(warehouse: any) {
            this.warehouse = useSearchCodeInfoArray(warehouse);
        }
    },
    persist: piniaPersistConfig("wms-user")
});
