<template>
	<!-- 维修工单 -->
	<view class="container">
		<u-navbar back-icon-color='#fff' :background="{ background: '#409eff'}" back-text="" title-color='#fff'
			:title="params.flag == 2 ? '报修确认' : '维修工单'">
			<template v-slot:right v-if="params.flag!='2'">
				<u-icon name="plus" color="#fff" size="36" style="padding-right: 30rpx;" @click="addForm"></u-icon>
			</template>
		</u-navbar>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" >
				<view class="" @click="itemClick(item,index)">
					<view class="title">
						<view class="title-txt">
							{{item.describes}}
						</view>
						<u-tag text="待接单" v-if="item.status == 'PENDING'" bg-color='rgba(255,255,255,0)' color='#fe8463' border-color='#fe8463'  type="primary" shape='circle'/>
						<u-tag text="已撤回" v-else-if="item.status=='REJECTED'" bg-color='rgba(255,255,255,0)' color='#d7d7d7' border-color='#d7d7d7 ' type="warning" shape='circle'/>
						<u-tag text="已转办" v-else-if="item.status=='TRANSFERRED'" bg-color='rgba(255,255,255,0)' color='#e01f54' border-color='#e01f54' type="success" shape='circle'/>
						<u-tag text="已接单" v-else-if="item.status=='PECEIVED'" bg-color='rgba(255,255,255,0)' color='#005eaa' border-color='#005eaa ' type="error" shape='circle'/>
						<u-tag text="已验证" v-else-if="item.status=='VERIFIED'" bg-color='rgba(255,255,255,0)' color='#2EC7C9' border-color='#2EC7C9' type="info" shape='circle'/>
						<u-tag text="已完成" v-else-if="item.status=='COMPLETED'" bg-color='rgba(255,255,255,0)' color='#2ba471' border-color='#2ba471' type="info" shape='circle'/>
						<u-tag text="报修人已确认" v-else-if="item.status=='APPLYPASS'" bg-color='rgba(255,255,255,0)' color='#2ba471' border-color='#2ba471' type="info" shape='circle'/>
					</view>
					<view class="dec">
						工单单号:<span>{{item.number}}</span>
					</view>
					<view class="dec">
						类型:<span>{{item.type=='DEVICE'?'设备':item.type=='TECH'?'工艺':'模具'}}</span>
					</view>
					<view class="dec">
						{{`${params.type=='DEVICE'?'设备' : '模具'}`}}编号:<span>{{item.deviceNumber}}</span>
					</view>
					<view class="dec">
						{{`${params.type=='DEVICE'?'设备' : '模具'}`}}名称:<span>{{item.name}}</span>
					</view>
					<view class="dec">
						所属厂区:<span>{{item.factoryAreaName}}</span>
					</view>
					<view class="dec">
						班次:<span>{{item.classesName}}</span>
					</view>
					<view class="dec">
						故障类型:<span>{{item.faultTypeName}}</span>
					</view>
				</view>
				<view class="bottom">
					<view class="time" style="flex: 1;">
						{{`${$time.formatDate(item.createTime)}`}}
					</view>
				</view>
			</view>
		</view>
		<view style="height: 94rpx;padding-top: 30rpx;">
			<u-loadmore :status="status" v-if="status != 'loadmore'" />
		</view>
		<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
	</view>
</template>

<script setup lang="ts">
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance,
		nextTick
	} from 'vue'
	import * as repairOrderApi from "@/api/repairOrder.js"
	import { useCountStore } from '@/store'
	
	const { proxy } = getCurrentInstance()
	// 获取自定义的store
	const store = useCountStore()
	const params = ref({
		pageNo: 1,
		pageSize: 10,
		type: '',
	})
	const status = ref('loadmore') //是否显示没有更多了
	const list = ref([])
	function itemClick(item, index) {
		proxy.$tab.navigateTo(`/pages/repairOrder/detail?type=${params.value.type}&number=${item.number}`)
	}
	function addForm(item) {
		proxy.$tab.navigateTo(`/pages/repairOrder/addForm?type=${params.value.type}&data=${encodeURIComponent(JSON.stringify(item))}`)
	}
	
	async function getList() {
		if (status.value == 'nomore') return;
		status.value = 'loading';
		proxy.$modal.loading('加载中')
		await repairOrderApi.repairOrderPage(params.value).then((res) => {
			proxy.$modal.closeLoading()
			if (res.data.list.length > 0) {
				list.value = list.value.concat(res.data.list);
				params.value.pageNo++;
				status.value = 'loadmore'
			} else {
				status.value = 'nomore'
			}
		}).catch(() => {
			proxy.$modal.closeLoading()
		})
	}
	onLoad((option) => {
		if (option.type) params.value.type = option.type;
		if (option.flag) params.value.flag = option.flag;
		// nextTick(() => {
		//         // 重新渲染页面
		// 		if (option.flag) params.value.flag = option.flag;
		//       });
	})
	onShow(() => {
		params.value.pageNo = 1
		list.value = []
		status.value = 'loadmore'
		getList()
	
	})
	onReachBottom(() => {
		getList()
	})
</script>

<style lang="scss" scoped>
	.container{
		background: #f5f5f5;
		min-height: 100vh;
	}
	.list {
		background: #f5f5f5;
		margin-top: 20rpx;

		.item {
			padding: 30rpx 30rpx 0px 30rpx;
			margin-top: 20rpx;
			background: white;

			.title {
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;

				.title-txt {
					color: #409eff;
					font-weight: bold;
					font-size: 36rpx;
					width: 0px;
					flex: 1;
					word-wrap: break-word;
				}

				.time {
					color: #919191;

				}
			}

			.dec {
				padding-bottom: 20rpx;

				span {
					color: #999999;
				}
			}

			.last {
				padding-bottom: 30rpx;
			}

			.bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1px solid #E4E4E4;
				padding: 20rpx 0px;

			}
		}
	}

	.list {
		background: #f5f5f5;
		margin-top: 20rpx;

		.item {
			padding: 30rpx 30rpx 0px 30rpx;
			margin-top: 20rpx;
			background: white;

			.title {
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;

				.title-txt {
					color: #409eff;
					font-weight: bold;
					font-size: 36rpx;
					width: 0px;
					flex: 1;
				}

				.time {
					color: #919191;

				}
			}

			.dec {
				padding-bottom: 20rpx;

				span {
					color: #999999;
				}
			}

			.last {
				padding-bottom: 30rpx;
			}

			.bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1px solid #E4E4E4;
				padding: 20rpx 0px;
				position: relative;

				
				.button {
					position: absolute;
					right: 0rpx;
				}

			}
		}
	}
</style>