<template>
	<view :class="['content',{'gray':store.isgray==1}]">
		<extendCom title="我的任务" img="process" :list="todoArr" :total="todoTotal"></extendCom>
		<extendCom title="历史任务" img="done" :list="doneArr" :total="doneTotal"></extendCom>
		<extendCom title="我发起流程" img="self" :list="selfArr" :total="selfTotal"></extendCom>
	</view>
</template>

<script setup>
	import extendCom from '../../bpm/extendCom.vue';
	import {
		ref
	} from 'vue';
	import {
		taskListApi,
		myApplyProcessListApi,
		taskHistoryListApi
	} from '@/api/api.js';
	import {
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app';
	import {
		useStore
	} from '@/store'
	const store = useStore()
	onLoad(() => {
		taskList()
		taskHistoryList()
		myApplyProcessList()
	})
	const todoArr = ref([])
	const todoTotal = ref(0)
	/**待办事项*/
	const taskList = () => {
		taskListApi({
			pageNo: 1,
			pageSize: 4,
			_t: new Date().getTime()
		}).then((res) => {
			if (res?.success) {
				if (res?.result?.total > 4) {
					taskListApi({
						pageNo: 1,
						pageSize: res?.result?.total,
						_t: new Date().getTime()
					}).then((res1) => {
						if (res1?.success) {
							todoArr.value = [...todoArr.value, ...handleData(res1?.result?.records)]
							todoTotal.value = res1?.result?.total
						}
					}).catch((err) => {
						console.log('err', err);
					})

				} else {
					todoArr.value = [...todoArr.value, ...handleData(res?.result?.records)]
					todoTotal.value = res?.result?.total
				}
			}
		}).catch((err) => {
			console.log(err);
		})

	}
	const doneArr = ref([])
	const doneTotal = ref(0)
	/**已办事项*/
	const taskHistoryList = () => {
		taskHistoryListApi({
			pageNo: 1,
			pageSize: 4,
			_t: new Date().getTime()
		}).then((res) => {
			if (res.success) {
				if (res.result.total > 4) {
					taskHistoryListApi({
						pageNo: 1,
						pageSize: res.result.total,
						_t: new Date().getTime()
					}).then((res1) => {
						if (res1.success) {
							doneArr.value = [...doneArr.value, ...handleData(res1.result.records)]
							doneTotal.value = res1.result.total
						}
					}).catch((err) => {
						console.log(err);
					})

				} else {
					doneArr.value = [...doneArr.value, ...handleData(res.result.records)]
					doneTotal.value = res.result.total
				}
			}
		}).catch((err) => {
			console.log(err);
		})
	}
	const selfArr = ref([])
	const selfTotal = ref(0)
	const selfArrquery = ref([])
	/**本人发起*/
	const myApplyProcessList = () => {
		myApplyProcessListApi({
			pageNo: 1,
			pageSize: 4,
			_t: new Date().getTime()
		}).then((res) => {
			if (res.success) {
				if (res.result.total > 4) {
					myApplyProcessListApi({
						pageNo: 1,
						pageSize: res.result.total,
						_t: new Date().getTime()
					}).then((res1) => {
						if (res1.success) {
							// selfArrquery.value=res1.result.records
							selfArr.value = [...selfArr.value, ...handleData(res1.result.records)]
							selfTotal.value = res1.result.total
						}
					}).catch((err) => {
						console.log(err);
					})

				} else {

					selfArr.value = [...selfArr.value, ...handleData(res.result.records)]
					selfTotal.value = res.result.total
				}
			}
		}).catch((err) => {
			console.log(err);
		})
	}
	const handleData = (titlearr) => {
		let titleArr = titlearr.length ? titlearr.map(item => item.processDefinitionName || item
			.prcocessDefinitionName) : []
		let res = titleArr.reduce((obj, title) => {
			if (title in obj) {
				obj[title]++
			} else {
				obj[title] = 1
			}
			return obj
		}, {})
		return Object.entries(res).map(([k, v]) => ({
			title: k,
			num: v
		}))
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
	}
</style>
<style lang="scss" scoped>

</style>