<template>
	<!-- 备件领用申请 -->
	<view class="container">
		<u-navbar back-icon-color='#fff' :background="{ background: '#409eff'}" back-text="" title-color='#fff'
			title="备件领用申请">
			<template v-slot:right>
				<u-icon name="plus" color="#fff" size="36" style="padding-right: 30rpx;" @click="addForm"></u-icon>
			</template>
		</u-navbar>
		<!-- <Search @search='search' @screen='screen' /> -->
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" @click="openDetail(item)">
				<view class="title">
					<view class="title-txt">
						{{item.name}}
					</view>
					<view class="time">
						{{`${$time.formatDate(item.createTime)}`}}
					</view>
				</view>
				<view class="dec">
					申请单号:<span>{{item.number}}</span>
				</view>
				<view class="dec">
					申 请 人:<span>{{item.applyer}}</span>
				</view>
				<view class="bottom">
					<view class="status">
						<u-tag text="待审批" v-if="item.status==0" bg-color='rgba(255,255,255,0)' color='#fe8463'
							border-color='#fe8463' type="primary" shape='circle' />
						<u-tag text="审批通过" v-else-if="item.status==1" bg-color='rgba(255,255,255,0)' color='#2EC7C9'
							border-color='#2EC7C9' type="info" shape='circle' />
						<u-tag text="审批驳回" v-else-if="item.status==2" bg-color='rgba(255,255,255,0)' color='#e01f54'
							border-color='#e01f54' type="success" shape='circle' />
						<u-tag text="出库中" v-else-if="item.status==3" bg-color='rgba(255,255,255,0)' color='#005eaa'
							border-color='#005eaa ' type="error" shape='circle' />
						<u-tag text="完成" v-else-if="item.status==4" bg-color='rgba(255,255,255,0)' color='#2ba471'
							border-color='#2ba471' type="info" shape='circle' />
						<u-tag text="撤单" v-else-if="item.status==5" bg-color='rgba(255,255,255,0)' color='#d7d7d7'
							border-color='#d7d7d7 ' type="warning" shape='circle' />
					</view>
					<view class="button">
						<u-button shape="circle" type="primary" size="mini" style="min-width: 120rpx;"
							v-if="item.status==0" @click="cancle(item)">撤回</u-button>
					</view>

				</view>
			</view>
		</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
	} from 'vue'
	import * as sparePartsApplicationApi from "@/api/eam/sparePartsApplication.js"
	const { proxy } = getCurrentInstance()
	const params = ref({
		pageNo: 1,
		pageSize: 10,
		type: '',
	})
	const status = ref('loadmore') //是否显示没有更多了
	const list = ref([])
	function openDetail(item, index) {
		proxy.$tab.navigateTo(`/pages/eam/sparePartsApplication/detail?number=${item.number}&masterId=${item.id}&description=${item.description}`)
	}
	function addForm(item) {
		proxy.$tab.navigateTo(`/pages/eam/sparePartsApplication/addForm`)
	}
	async function getList() {
		if (status.value == 'nomore') return;
		status.value = 'loading';
		proxy.$modal.loading('加载中')
		await sparePartsApplicationApi.sparePartsApplicationPage(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()
		})
	}
	// 撤销
	function cancle(item) {
		proxy.$modal.confirm('确定撤回申请吗？').then(() => {
			proxy.$modal.loading('加载中')
			sparePartsApplicationApi.sparePartsApplicationCancle(item.id).then(async (res) => {
				proxy.$modal.closeLoading()
				params.value.pageNo = 1
				list.value = []
				status.value = 'loadmore'
				await getList()
				proxy.$modal.showToast('撤回成功')
			}).catch(()=>{})
		})
	}
	onLoad((option) => {
		if (option.type) params.value.type = option.type;
	})
	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;
			position: relative;

			.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 {
				position: static !important;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1px solid #E4E4E4;
				padding: 20rpx 0px;
				height: 90rpx;

			}
		}
	}
</style>