import store from '../store'
import { enterpriseAuthApi } from '@/api/public'
import { scheduleMenuPermissionApi } from '@/api/attendance'
import { frameTreeApi } from '@/api/user'
import type { Res } from '@/utils/typeHelper'
import message from '@/utils/message'
import { clickNavigateTo } from '@/utils/helper'
import { encryptCode } from '@/utils/cryptoCode'

//鑾峰彇鍏ㄥ眬鐨勬潈闄愯缃�
export const getEnterpriseAuth = () => {
	enterpriseAuthApi().then((res : Res) => {
		const auth = res.data
		store.commit('setEnterpriseAuth', auth)
	}).catch((error : Res) => {
		message.error(error.message)
	})
}

export const getScheduleMenuPermission = () => {
	scheduleMenuPermissionApi().then((res : Res) => {
		store.commit('setScheduleMenuPermission', !!res.data.is_admin)
	}).catch((error : Res) => {
		message.error(error.message)
	})
}

// 鑾峰彇缁勭粐鏋舵瀯涓墍鏈夌殑浜哄憳
export const getFrameTree = () => {
	frameTreeApi().then((res : Res) => {
		store.commit('frameTree', res.data)
	}).catch((error : Res) => {
		message.error(error.message)
	})
}

export const autoLoad = () => {
	getFrameTree()
	getEnterpriseAuth()
	getScheduleMenuPermission()
}

/**
 * 閲嶇疆閫変腑浜哄憳/閮ㄩ棬
 */
export const resetSelectDepartment = <T extends any[]>(people : T = <any>[], ids : T = <any>[]) => {
	// 閲嶇疆閫変腑浜哄憳/閮ㄩ棬
	store.commit('setDepSelectPeople', people)
	// 閲嶇疆閫変腑閮ㄩ棬/浜哄憳id
	store.commit('setDepSelectIds', ids)
	// 閲嶇疆椤甸潰璺宠浆
	store.commit('setTreeSelectBeforePage', '')
}


/**
 * 閲嶇疆鐢宠瀹℃壒涓汉鍛橀€夋嫨Index
 */
export const resetExamineIndex = () => {
	// 閲嶇疆閫変腑浜哄憳/閮ㄩ棬index
	store.commit('setDepSelectIndex', -1)
	// 閲嶇疆瀹℃壒娴佺▼涓€夋嫨鎴愬憳Index
	store.commit('setDepUserIndex', -1)
}

/**
 * 閫夋嫨鎴愬憳|閮ㄩ棬閾炬帴璺宠浆
 */
export const navigateToDepartment = (query : string, currentPageUrl : string = '', url : string = '/pages/users/organization/index') => {
	// 鍙傛暟鍔犲瘑
	const item = encryptCode(query)
	if (currentPageUrl) {
		store.commit('setTreeSelectBeforePage', currentPageUrl)
	}
	// 椤甸潰璺宠浆
	clickNavigateTo(`${url}?item=${item}`)
}