import {
	listApply,
} from "@/api/workbench/apply";
import {
	listMaintenanceRecord
} from "@/api/maintenanceRecord/maintenanceRecord";

import Store from '@/store'
import modal from '@/plugins/modal'
import {
	h
} from 'vue'
import router from '@/router'
import {
	checkPermi
} from "@/utils/permission"

export default function(showMessage = true) {
	async function getApplyList() {
		await listApply({
			pageNum: 1,
			pageSize: 5,
		}).then((response) => {
			Store.state.data.applyList = response.rows;
			if (response.total > 5) {
				for (let i = 0; i < (response.total - 5); i++) {
					Store.state.data.applyList.push({})
				}
			}
			if (response.rows.length > 0 && showMessage) {
				modal.notify(h('div', {
					style: 'cursor:pointer',
					onclick: () => router.push(`/workbench/todo`)
				}, `你有${response.total}条待办，点击查看`), `通知`, `bottom-right`)
			}

			if (response.rows.length > 0) {
				let workbenchList = Store.getters.sidebarRouters.filter(item => item.name == "Workbench")
				// console.log(Store.getters.sidebarRouters)
				// console.log(workbenchList)
				if (workbenchList.length == 1) {
					// workbenchList[0].meta.title = Store.state.data.applyList.length > 0 ? `工作台 (${Store.state.data.applyList.length})` : `工作台`
					workbenchList[0].meta.badge = Store.state.data.applyList.length
					// let todoList = workbenchList.children.filter(item => item.name == "Todo")
					let todoList = workbenchList[0].children.filter(item => {
						// console.log(item.value)
						return item.name == "Todo"
					})
					if (todoList.length == 1) {
						let title = todoList[0].meta.title.split(" ")[0]
						todoList[0].meta.title = Store.state.data.applyList.length > 0 ?
							`${title} (${Store.state.data.applyList.length})` : `${title}`
						todoList[0].meta.badge = Store.state.data.applyList.length
					}
				}
			}

		});
	}

	async function getMaintenanceRecordList() {
		// console.log(`Store.state.data.maintenanceRecordList before`)
		// console.log(JSON.parse(JSON.stringify(Store.state.data.maintenanceRecordList)))
		await listMaintenanceRecord({
			pageNum: 1,
			pageSize: 5,
			state: 10
		}).then((response) => {
			Store.state.data.maintenanceRecordList = response.rows
			if (response.total > 5) {
				for (let i = 0; i < (response.total - 5); i++) {
					Store.state.data.maintenanceRecordList.push({})
				}
			}
			// console.log(`Store.state.data.maintenanceRecordList after`)
			// console.log(JSON.parse(JSON.stringify(Store.state.data.maintenanceRecordList)))
			// .filter(item => item.state === '10');
			// let length = Store.state.data.maintenanceRecordList.length
			if (response.total > 0 && showMessage) {
				modal.notify(h('div', {
					style: 'cursor:pointer',
					onclick: () => router.push(`/maintentance/maintenanceRecord`)
				}, `你有${response.total}条维保记录待处理，点击查看`), `通知`, `bottom-right`)
			}
			if (response.rows.length > 0) {
				let maintentanceList = Store.getters.sidebarRouters.filter(item => item.name ==
					"Maintentance")
				if (maintentanceList.length == 1) {
					maintentanceList[0].meta.badge = Store.state.data.maintenanceRecordList.length
					let maintenanceRecordList = maintentanceList[0].children.filter(item => {
						return item.name == "MaintenanceRecord"
					})
					if (maintenanceRecordList.length == 1) {
						let title = maintenanceRecordList[0].meta.title.split(" ")[0]
						maintenanceRecordList[0].meta.title = Store.state.data.maintenanceRecordList
							.length > 0 ? `${title} (${Store.state.data.maintenanceRecordList.length})` :
							`${title}`
						maintenanceRecordList[0].meta.badge = Store.state.data.maintenanceRecordList.length
					}
				}
			}
		});
	}
	console.log(showMessage)

	if (checkPermi(['apply:apply:list'])) {
		getApplyList();
	}
	if (checkPermi(['maintenanceRecord:maintenanceRecord:list'])) {
		getMaintenanceRecordList();
	}
}