import permision from "@/js_sdk/wa-permission/permission.js";
import {
	getMacAddress
} from "../../utils/util";
import {
	systemList
} from "@/utils/commonDicts.js";
export default {
	namespaced: true,
	state: {
		userInfo: {},
		token: "",
		belongSystem: null, // 子系统实体类
		isDS: false, // 是否处于脱敏状态
		path: "", // 请求的前缀
		system: {}, // 当前选择的系统
		abcModel: false,
		spdbModel: false,
		ccbModel: false,
		dfklModel: false,
		publicModel: false,
		cibModel: false,
		icbcHModel: false,
		tjxtModel: false,
		homeOff: false, // 居家
		isOut: false, // 外访
		uuid: null, // 设备id
		netConfig: {
			// http://59.42.9.166:9520/proxy
			// http://10.0.7.100:9500
			ip: "https://crm.nanjingshiyu.com/prod-api", // ip
			// ip: "https://crmtest.nanjingshiyu.com/prod-api", // 测试环境ip
			// ip : "/api", // 测试环境ip
			// ip : "http://47.113.184.101", // 测试环境ip
			// ip: "http://172.16.7.200", // ip
			// port: "9500", // 端口
			isCallOff: "2", // 开关
			folder: "", // 文件夹
			webRtcIp: "", // 呼叫中心的ip
			webRtcPort: "", // 呼叫中心的端口s
		},
		systemList: [],

		redirectUrl: null,
	},
	mutations: {
		SET_REDIRECTURL(state, data) {
			state.redirectUrl = data;
		},
		SET_USER_ONLINE_STATUS: (state, data) => {
			state.userInfo.onlineStatus = data;
		},
		SET_USERINFO: (state, data) => {
			state.userInfo = data;
		},
		SET_TOKEN: (state, data) => {
			state.token = data;
		},
		SET_NETCONFIG: (state, data) => {
			state.netConfig = data;
		},
		SET_BELONGSYSTEM: (state, data) => {
			const system = state.userInfo.subsystemList.find(item => (item.systemCode == data.code));
			if (system) {
				state.belongSystem = system;
				data.callback && data.callback(true);
			} else {
				data.callback && data.callback(false);
			}
		},
		SET_ISDS: (state, value) => {
			state.isDS = value
		},
		SET_HOMEOFF: (state, value) => {
			state.homeOff = value
		},
		SET_ISOUT: (state, value) => {
			state.isOut = value
		},
		SET_SYSTEM(state, data) {
			state.path = data.path;
			state.system = data;
			state.abcModel = data.text == "农行催收";
			state.spdbModel = data.text == "浦发催收";
			state.ccbModel = data.text == "建行催收";
			state.publicModel = data.text == "专业催收";
			state.dfklModel = data.text == "商账催收";
			state.cibModel = data.text == "兴业催收";
			state.icbcHModel = data.text == "工行催收";
			state.tjxtModel = data.text == "调催系统";
		},
		SET_UUID(state, data) {
			state.uuid = data;
		},
		SET_SYSTEM_LIST(state, data) {
			state.systemList = data;
		}
	},
	actions: {
		setSystemlist({
			commit,
			state
		}, systemCodeList) {
			state.systemList = systemList.filter(v => systemCodeList.includes(v.value));
		},
		getUserInfo(store) {
			const {
				commit,
				state,
				dispatch
			} = store;
			return new Promise((resolve, reject) => {
				uni.$u.api.getInfo().then(res => {
					commit("SET_USERINFO", res.user);
					commit("SET_BELONGSYSTEM", {
						code: state.system.value,
						callback: (flag) => {
							resolve(res.user);
						}
					});
				});
			})
		},
		login({
			commit,
			dispatch,
			state
		}, form) {
			return new Promise((resolve, reject) => {
				uni.$u.api.login(form).then((loginRes) => {
					const token = loginRes.data.access_token;
					commit("SET_TOKEN", token);
					dispatch("getUserInfo").then((userInfo) => {
						const { userId } = userInfo;
						resolve(userId);
					}).catch(() => {
						commit("SET_TOKEN", "");
						commit("SET_USERINFO", {});
						reject();
					})
				})
			})
		},
		logout({
			commit,
			dispatch
		}) {
			return uni.$u.api.logout().then(() => {
				// 停止电话监听
				dispatch("call/stopPhoneListener", null, { root: true }).catch(error => {
					console.error('退出登录时停止电话监听失败:', error);
				});

				commit("SET_TOKEN", "");
				commit("SET_USERINFO", {});
				dispatch("app/logoutCloseData", null, {
					root: true
				});
				Promise.resolve();
			}).catch(() => {
				Promise.reject();
			})
		},
	}
}