import { observable, runInAction } from "mobx";
import { api_corpUser_allOfUser } from "../API/global";
/**
 * 分组组件状态管理
 */
const teamStore: {
    data: {
        // 选中企业ID
        active_corpIds?: string;
        /**选中的企业对象*/
        active_corpIdsObj?: any;
        /**企业列表*/
        corpIds: any[];
        activeKey: string;
        showLeft: boolean;
        userId?: number;
        loading?: boolean;
    };
    /**初始化操作*/
    init(): Promise<void>;
    setUserId(value?: number): Promise<void>;
    setActiveKey(value: string): Promise<void>;
    setShowLeft(value: boolean): Promise<void>;
    setActiveCorpIds(value: string): Promise<void>;
    setActiveCorpIdsObj(value: any): Promise<void>;
    /**获取企业列表数据*/
    getCorpIds(type: 'init' | 'refresh', userId?: number): Promise<any>;
    /** 是否本人 */
    isOneself(): boolean;
} = {
    data: {
        active_corpIds: undefined,
        active_corpIdsObj: {},
        corpIds: [],
        activeKey: '1',
        showLeft: false,
        userId: undefined,
        loading: false
    },
    /**初始化操作*/
    async init() {
        let api_corpUser_allOfUser_data = await this.getCorpIds('init')
        const pageMessage = localStorage.getItem('page-message');
        if (pageMessage) {
            const { corpId } = JSON.parse(JSON.parse(pageMessage))
            runInAction(() => {
                this.data = {
                    ...this.data,
                    corpIds: api_corpUser_allOfUser_data,
                    active_corpIds: corpId || api_corpUser_allOfUser_data[0]?.value || '',
                    active_corpIdsObj: api_corpUser_allOfUser_data?.find(item => item.value === corpId) || api_corpUser_allOfUser_data[0],
                    userId: Number(sessionStorage.getItem('userId')) || undefined
                };
            });
        } else {
            runInAction(() => {
                this.data = {
                    ...this.data,
                    corpIds: api_corpUser_allOfUser_data,
                    active_corpIds: api_corpUser_allOfUser_data[0]?.value || '',
                    active_corpIdsObj: api_corpUser_allOfUser_data[0],
                    userId: Number(sessionStorage.getItem('userId')) || undefined
                };
            });
        }
    },
    /**
     * 设置tab
     * @param value 
     */
    async setActiveKey(value) {
        runInAction(() => {
            this.data = {
                ...this.data,
                activeKey: value
            };
        });
    },
    /**
     * 设置用户ID
     * @param value 
     */
    async setUserId(value) {
        runInAction(() => {
            // this.data = {
            //     ...this.data,
            //     userId: value
            // };
            this.getCorpIds('refresh', value)
        });
    },
    /**
     * 是否隐藏组员列表
     * @param value 
     */
    async setShowLeft(value) {
        runInAction(() => {
            this.data = {
                ...this.data,
                showLeft: value
            };
        });
    },
    /**
     * 设置选中企微号
     * @param value 
     */
    async setActiveCorpIds(value) {
        runInAction(() => {
            this.data = {
                ...this.data,
                active_corpIds: value,
            };
        });
    },
    /**
     * 设置选中企业所有信息
     * @param value 
     */
    async setActiveCorpIdsObj(value) {
        runInAction(() => {
            this.data = {
                ...this.data,
                active_corpIdsObj: value,
            };
        });
    },
    /**获取企业列表数据*/
    async getCorpIds(type: 'init' | 'refresh', userId) {
        runInAction(() => {
            this.data.loading = true
        })

        let params: any = this.data.userId || sessionStorage.getItem('userId')
        if (type === 'refresh') {
            params = userId
        }
        let data = await api_corpUser_allOfUser(params).then(body => {
            let newArr: any[] = []
            if (body?.data) {
                let arr = body?.data?.map((item: { t1: { corpId: any; corpName: any; }; t2: any; }) => {
                    return {
                        ...item.t1,
                        value: item.t1.corpId,
                        label: item.t1.corpName,
                        weList: item.t2
                    }
                })
                newArr = arr
            }
            if (type === 'refresh') {
                runInAction(() => {
                    this.data.corpIds = newArr
                    this.data.userId = userId
                    this.data.active_corpIdsObj = newArr?.[0] || {}
                    this.data.active_corpIds = newArr?.[0]?.value
                })
            }
            return newArr
        }).catch(() => runInAction(() => {
            this.data.loading = false
        }))
        runInAction(() => {
            this.data.loading = false
        })
        return data
    },
    isOneself() {
        return this.data?.userId?.toString() === sessionStorage.getItem("userId")
    }
};
export default observable(teamStore);