<template>
	
	<view class="container">
		<wd-loading v-if="loading && pageNo === 1" class="loading-tip">加载中...</wd-loading>
		<template v-for="(item, i) in list" :key="i">
			<wd-card :title="item.bpmBizTitle" title-bold border-radius="8" use-footer-slot @click="goToPage(item)">
				<view class="card-content">
					<wd-row style="color: #666666;">
						<wd-col :span="6">
							<text>当前环节:</text>
						</wd-col>
						<wd-col :span="18">
							{{item.taskName}}
						</wd-col>
					</wd-row>
					<wd-row style="color: #666666;">
						<wd-col :span="6">
							<text>流程名称:</text>
						</wd-col>
						<wd-col :span="18">
							{{item.processDefinitionName}}
						</wd-col>
					</wd-row>
					<wd-row style="color: #666666;">
						<wd-col :span="6">
							<text>发起人:</text>
						</wd-col>
						<wd-col :span="18">
							{{item.processApplyUserName}}
						</wd-col>
					</wd-row>
					<wd-row style="color: #666666;">
						<wd-col :span="6">
							<text>开始时间:</text>
						</wd-col>
						<wd-col :span="18">
							{{item.taskBeginTime?item.taskBeginTime.substring(0,10):''}}
						</wd-col>
					</wd-row>
					<wd-row style="padding-bottom: 2px;color: #666666;">
						<wd-col :span="6">
							<text>结束时间:</text>
						</wd-col>
						<wd-col :span="18">
							{{item.taskEndTime?item.taskEndTime.substring(0,10):''}}
						</wd-col>
					</wd-row>
				</view>
			</wd-card>
		</template>
		<view class="load-more" v-if="loading && pageNo > 1">
			<wd-loading size="16px">正在加载...</wd-loading>
		</view>
		<wd-message-box></wd-message-box>
		<wd-toast></wd-toast>
	</view>
</template>

<script setup lang="ts">
	import { taskListApi,claim,histTasks } from '@/api/process'
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onShow,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app';
	import { useToast, useMessage } from 'wot-design-uni'
	import SelectUserModal from '@/components/SelectUser/components/SelectUserModal.vue'
	
	defineOptions({
	  name: 'historicalTasks',
	  options: {
	    styleIsolation: 'shared',
	  },
	})
	const toast = useToast()
	const message = useMessage()
	let pageNo = 1
	let pageSize = 10
	let loading = false
	const list = ref([])
	const user = ref('')
	const modalShow = ref(false)
	const userTask = ref({}) //点击的流程节点
	

	const getList = () => {
		histTasks({
			pageNo,
			pageSize
		}).then((res) => {
			console.log(res)
			if (res.success) {
				list.value = [...list.value,...res.result.records];
			}
			loading = false
		}).catch((err) => {
			loading = false
		})
	}
	
	const entrust = (item)=>{  //委托
		modalShow.value = true;
		userTask.value = {};
		userTask.value = item;
	}
	
	const goToPage = (item)=>{
		//判断是否是签收项目，提示是否签收
		if(item.taskAssigneeName&&item.taskAssigneeName!=''){
			console.log(item)
			//办理任务，直接进入办理页面
			uni.navigateTo({
				url:`/pages-process/taskHandle?data=history`,
				success:(res) =>{
					res.eventChannel.emit('taskData',JSON.stringify(item))
				}
			})
		}else{
			message
				.confirm({
					msg: '是否签收该任务？',
					title: '确认签收吗',
				})
				.then(() => {
					claim({taskId:item.id}).then((res)=>{
						if(res.success){
							toast.success(res.message)
							uni.redirectTo({
								url: './approvalTabbar'
							});
						}else{
							toast.error(res.message)
						}

					})
				})
		}
	}
	
	onReachBottom(() => {
		if (loading) return
		pageNo++
		getList()
	})
	
	onPullDownRefresh(() => {
		pageNo = 1
		list.value = []
		getList()
		uni.stopPullDownRefresh()
	})
	
	
	onShow(() => {
		list.value = []
		pageNo = 1
		pageSize = 10
		loading = false
		getList()
	})
</script>

<style lang="scss" scoped>
	.container {
		padding: 5px 0 5px;
		min-height: 100vh;
		background-color: #f7f7f7;
	}
	
	.card-content {
		padding: 8px 0;
	
		.meta-info {
			display: flex;
			align-items: center;
			font-size: 12px;
			color: #666;
	
			.meta-text {
				margin-left: 4px;
			}
		}
	}
	::v-deep .wd-card__title-content{
		border-bottom: 1px solid #efefef;
	}
</style>