import {userInfo, loginName, logout} from "../../api/auth/login";
import * as types from "../mutation-types";
import {constantRouterMap} from "../../router";
import {
    getToken,
    setToken,
    removeToken,
    getAdminId,
    setAdminId,
    removeAdminId,
    getCustomerId,
    setCustomerId,
    removeCustomerId,
    getCompanyName,
    setCompanyName,
    removeCompanyName,
} from "../../utils/auth";
// import { $NOT_NETWORK } from '../../utils/errorCode'
import {Message} from "element-ui";
import {RECEIVE_LOCINVCODE, RECEIVE_LOCINVNAME, RECEIVE_LOCSUBINVCODE, RECEIVE_LOCSUBINVNAME} from "../mutation-types";

// initial state
const state = {
    adminId: getAdminId(), // id
    userName: "", // 昵称
    employeeName: "", // 用户名
    avatar: "", // 头像
    token: getToken(), // 登录token
    authRules: [], // 权限列表
    routers: constantRouterMap, // 路由列表
    customerId: getCustomerId(),
    companyName: getCompanyName(),
    locInvCode: "",
    locSubInvCode: "",
    locInvName: "",
    locSubInvName: ""
};

// getters
const getters = {
    adminId: state => state.adminId,
    userName: state => state.userName,
    employeeName: state => state.employeeName,
    avatar: state => state.avatar,
    token: state => state.token,
    authRules: state => state.authRules,
    routers: state => state.routers,
    customerId: state => state.customerId,
    companyName: state => state.companyName,
    locInvCode: state => state.locInvCode,
    locSubInvCode: state => state.locSubInvCode,
    locInvName: state => state.locInvName,
    locSubInvName: state => state.locSubInvName,
};

// actions
const actions = {
    // 用户名登录
    loginName({commit}, userInfo) {
        const userName = userInfo.userName ? userInfo.userName.trim() : "";
        const pwd = userInfo.pwd ? userInfo.pwd : "";
        return new Promise((resolve, reject) => {
            loginName(userName, pwd)
                .then(response => {
                    console.log(response);
                    if (response.code != 20000) {
                        Message({
                            message: response.message,
                            type: "error",
                            duration: 5 * 1000
                        });
                    } else {
                        let data = response.data;
                        commit(types.RECEIVE_ADMIN_ID, data.id);
                        commit(types.RECEIVE_ADMIN_TOKEN, data.token);
                        commit(types.RECEIVE_ADMIN_AUTH_RULES, []);
                        commit(types.RECEIVE_CUSTOMERID, "");
                        commit(types.RECEIVE_COMPANYNAME, "");
                    }
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    userInfo({commit}) {
        return new Promise((resolve, reject) => {
            userInfo()
                .then(response => {
                    if (response.code !== 20000) {
                        reject("登录失效");
                    }
                    const data = response.data || {};
                    commit(types.RECEIVE_ADMIN_NAME, data.userName);
                    commit(types.RECEIVE_ADMIN_EMPLOYEE_NAME, data.employeeName);
                    commit(types.RECEIVE_ADMIN_AVATAR, data.avatar);
                    commit(types.RECEIVE_ADMIN_AUTH_RULES, data.authRules);
                    commit(types.RECEIVE_CUSTOMERID, data.customerId);
                    commit(types.RECEIVE_COMPANYNAME, data.companyName);
                    resolve(data);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    // 登出
    loginOut({commit}) {
        return new Promise((resolve, reject) => {
            logout()
                .then(() => {
                    commit(types.RECEIVE_ADMIN_ID, "");
                    commit(types.RECEIVE_ADMIN_TOKEN, "");
                    commit(types.RECEIVE_ADMIN_AUTH_RULES, []);
                    commit(types.RECEIVE_CUSTOMERID, "");
                    commit(types.RECEIVE_COMPANYNAME, "");
                    resolve();
                })
                .catch(error => {
                    reject(error);
                });
        });
    },

    // 前端 登出
    fedLogout({commit}) {
        return new Promise(resolve => {
            commit(types.RECEIVE_ADMIN_ID, "");
            commit(types.RECEIVE_ADMIN_TOKEN, "");
            commit(types.RECEIVE_ADMIN_AUTH_RULES, []);
            commit(types.RECEIVE_CUSTOMERID, "");
            commit(types.RECEIVE_COMPANYNAME, "");
            resolve();
        });
    },
    /**
     * 过滤路由列表
     * @param commit
     * @param data
     * @returns {Promise}
     */
    filterRouter({commit}, data) {
        const {accessedRouters} = data;
        if (accessedRouters && accessedRouters.length > 0) {
            commit(types.RECEIVE_ROUTERS, accessedRouters);
        }
    }
};

// mutations
const mutations = {
    [types.RECEIVE_ADMIN_ID](state, adminId) {
        state.adminId = adminId;
        if (adminId === "") {
            removeAdminId();
        } else {
            setAdminId(adminId);
        }
    },
    [types.RECEIVE_ADMIN_TOKEN](state, token) {
        state.token = token;
        if (token === "") {
            removeToken();
        } else {
            setToken(token);
        }
    },

    [types.RECEIVE_CUSTOMERID](state, customerId) {
        state.customerId = customerId;
        if (customerId === "") {
            removeCustomerId();
        } else {
            setCustomerId(customerId);
        }
    },
    [types.RECEIVE_COMPANYNAME](state, companyName) {
        state.companyName = companyName;
        if (companyName === "") {
            removeCompanyName();
        } else {
            setCompanyName(companyName);
        }
    },
    [types.RECEIVE_ADMIN_NAME](state, userName) {
        state.userName = userName;
    },
    [types.RECEIVE_ADMIN_EMPLOYEE_NAME](state, employeeName) {
        state.employeeName = employeeName;
    },
    [types.RECEIVE_ADMIN_AVATAR](state, avatar) {
        state.avatar = avatar;
    },
    [types.RECEIVE_ADMIN_AUTH_RULES](state, authRules) {
        state.authRules = authRules;
    },
    [types.RECEIVE_ROUTERS](state, routers) {
        const tempRm = constantRouterMap.concat(routers);
        state.routers = JSON.parse(JSON.stringify(tempRm));
    },
    [types.RECEIVE_LOCINVCODE](state, locInvCode) {
        state.locInvCode = locInvCode;
    },
    [types.RECEIVE_LOCSUBINVCODE](state, locSubInvCode) {
        state.locSubInvCode = locSubInvCode;
    },
    [types.RECEIVE_LOCINVNAME](state, locInvName) {
        state.locInvName = locInvName;
    },
    [types.RECEIVE_LOCSUBINVNAME](state, locSubInvName) {
        state.locSubInvName = locSubInvName;
    },
};

export default {
    state,
    getters,
    actions,
    mutations
};
