<template>
	<!-- <u-navbar title="拣货扫描" placeholder border=true :autoBack="true" leftIconSize='35'></u-navbar> -->
	<BasicContainer ref="basicContainer" :option="option" :leftClick="backPreviousPage">
		<template #head>
			<view class="schbox">
				<view>拣货日期：</view>
				<view @click='showCalendar' class="inputsr">
					<view class="timeInput">
						{{datatime|| '请选择时间'}}
					</view>
					<view @click.stop.prevent>
						<u-icon @click='showCalendar' style="margin-right: 10rpx;" v-if="!datatime" name="calendar"
							color="#999999" size="50"></u-icon>
						<u-icon @click="cleartime" style="margin-right: 10rpx;" v-else name="close-circle" color="#999999"
							size="40"></u-icon>
					</view>
				</view>
			</view>
			<view class="schbox">
				<view>拣货客户：</view>

				<view class="searchBox">
					<input class="inputsr" v-model.trim='details.customName' placeholder="请输入客户名称" />
					<view class="iconBox" v-show="details.customName" @click.stop.prevent>
						<u-icon @click="()=> details.customName = ''" name="close-circle" color="#999" size="40"></u-icon>
					</view>
				</view>

				<view @click="handlSearch">
					查询
				</view>
			</view>
			<view class="tabtip">
				<view @click="setstate(0)">
					<view :class="status==0?'xz':''">未完成</view>
				</view>
				<view @click="setstate(1)">
					<view :class="status==1?'xz':''">完成</view>
				</view>
			</view>
		</template>

		<template #body>
			<scroll-view class="sccv">
				<view class="mabx">
					<view v-for="item in dataList" :key="item">
						<view class="topbox">
							<view class="type1">
								<view>
									<text style="color: #90A0AF;">计划类型：</text>{{item.deliveryTypeName}}
								</view>
								<view>
									<text style="color: #90A0AF;">备货状态：</text>{{item.stockupStatusName}}
								</view>
							</view>
							<view class="type1">
								<view>
									<text style="color: #90A0AF;">客户：</text>{{item.consignee}}
								</view>
							</view>

							<view class="type2">
								<view>
									<view>订单总数</view>
									<view>{{item.orderNum}}</view>
								</view>
								<view>
									<view>计划备货件数</view>
									<view>{{item.planNum + item.planNum}}</view>
								</view>
								<view>
									<view>备货件数</view>
									<view>{{item.scanNum}}</view>
								</view>
								<view>
									<view>库存品数</view>
									<view>{{item.inventoryNum}}</view>
								</view>
							</view>
						</view>

						<view class="bts" v-if="Number(item.stockupStatus)!=40">
							<!-- <view @click="submitsuccess">
								备货完成
							</view> -->
							<view @click="gostockuplist(item)">
								去备货
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</template>
	</BasicContainer>
	<tips ref="tip"></tips>
	<l-calendar v-model:value="show" :initStartDate='date[0]' :initEndDate='date[1]' @hide='showCalendar'
		@change="onConfirm"></l-calendar>
</template>

<script lang="ts" setup>
	import {
		onLoad,
		onShow,
		onHide,
		onUnload,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import { distributionStockuppageList, newDistributionStockuppageList } from '@/api/user.js'
	import { pageData } from '@/interfaces/pagesHome/pickingScan'
	import { timeType } from '@/interfaces/pagesHome/allType'
	import { inject, nextTick, reactive, ref, toRefs, watchEffect } from "vue";
	import useSearchInfoStore from '@/store/useSearchInfoStore.js';
	import utils from '@/utils/utils';
	import { storeToRefs } from 'pinia';
	const option = {
		title: '拣货扫描',
		haveData: true,
		async pullDownRefreshInitPage() {
			details.dataList = []
			await init()
		},
		reachBottomInitPage: jiazai,
		haveReachBottom: false,
		isFixed: true
	}
	let details = reactive({
		/** 件货日期*/
		datatime: '',
		dataList: [],
		/** 客户名称 */
		customName: '',
		show: false,
		currentPage: 1,
		size: 10,
		stockupDate: '',
		status: 0,
		/** 列表高度 */
		scrollHeight: ''
	})
	let tip = ref(null)

	const basicContainer = ref(null)

	// 初始化Pinia仓库
	const searchInfoStore = useSearchInfoStore()
	const { searchInfo } = storeToRefs(searchInfoStore)
	const { HANDLE_EDIT_INFO, HANDLE_DELETE } = searchInfoStore

	const date = ref<number | string[]>([])
	function showCalendar() {
		details.show = !details.show
	}
	function onConfirm(e : any) {
		console.log(e);
		date.value[0] = e.startDate
		date.value[1] = e.endDate
		details.datatime = date.value[0] + ' 至 ' + date.value[1]

		HANDLE_EDIT_INFO('pickingScan', {
			date: date.value,
			customName: details.customName
		})
		basicContainer.value.startPullDownRefresh()
	}

	onShow(async () => {
		uni.$off('scancodedate')

		// 初始化获取搜索值
		const pageSearchInfo = searchInfo.value['pickingScan']
		console.log('searchInfo :>> ', searchInfo);
		if (pageSearchInfo) {
			// 获取存储在Pinia中的搜索参数
			date.value = pageSearchInfo.date
			details.customName = pageSearchInfo.customName
		} else {
			date.value[0] = (uni as any).$u.timeFormat((new Date().valueOf() - 1000 * 60 * 60 * 24 * 3), 'yyyy-mm-dd')
			date.value[1] = (uni as any).$u.timeFormat((new Date().valueOf()), 'yyyy-mm-dd')
		}

		datatime.value = date.value[0] + ' 至 ' + date.value[1]

		await nextTick()
		basicContainer.value.startPullDownRefresh()
	})
	onLoad(() => {
		utils.ttsspke('请选择拣货任务')
	})
	async function init() {
		details.currentPage = 1
		details.dataList = []
		return initpage()
	}
	function jiazai() {
		details.currentPage++
		initpage()
	}
	async function initpage() {
		if (date.value.length === 0) return utils.handleToast('请选择拣货时间')

		try {
			uni.showLoading({
				title: '数据加载中',
				mask: true
			})
			let data = {
				current: details.currentPage,
				size: details.size,
				stockupDateStart: date.value[0],
				stockupDateEnd: date.value[1],
				status: details.status,
				customName: details.customName
			}
			// let response = await distributionStockuppageList(data)
			let response = await newDistributionStockuppageList(data)
			if (response.code !== 200) return
			if (details.currentPage == 1 && response.data) {
				// details.dataList = response.data.records
				details.dataList = response.data
			} else {
				if (response.data.length == 0) {
					uni.showToast({
						title: '已经到底了。',
						icon: 'none'
					})
					details.currentPage--
					return
				}
			}
			// details.dataList=response.data
		} catch (e) {
			//TODO handle the exception
		} finally {
			uni.hideLoading()
		}
	}

	function checktime(event : timeType) {
		console.log(event);
		details.datatime = (uni as any).$u.timeFormat(event.value, 'yyyy-mm-dd');
		details.show = false
		console.log(details.datatime);
	}
	function gostockuplist(item : any) {
		// uni.navigateTo({
		// 	url: '/pagesHome/pages/PickingScanList/PickingScanList?id=' + item.id + '&stockupArea=' + item.stockupArea,

		// })
		console.log('item :>> ', item);
		uni.navigateTo({
			url: '/pagesHome/pages/PickingScanList/PickingScanList?stockupId=' + item.stockupId,
		})
	}
	function cleartime() {
		// console.log(1231211);
		details.datatime = ''
		date.value = []
		HANDLE_EDIT_INFO('pickingScan', {
			date: date.value,
			customName: details.customName
		})
		basicContainer.value.startPullDownRefresh()
	}
	function setstate(state : number) {
		if (details.status === state) return
		details.status = state
		basicContainer.value.startPullDownRefresh()
	}
	function submitsuccess() {
		tip.value.setdetails({
			title: '提示',
			content: '确认备货完成？',
			confirmTxt: '确认',
			isshow: true,
			isshowcancel: true,
			success: () => {
				tip.value.setisshow(false)
			},
			cancel: () => {
				tip.value.setisshow(false)
			},
			close: () => {
				tip.value.setisshow(false)
			}
		})
	}

	/** 搜索 */
	const handlSearch = () => {
		HANDLE_EDIT_INFO('pickingScan', {
			date: date.value,
			customName: details.customName
		})

		init()
	}

	// 返回上一级页面
	function backPreviousPage() {
		HANDLE_DELETE('pickingScan')
		uni.navigateBack()
	}

	const { datatime, dataList, show, status } = toRefs(details)
</script>

<style lang="scss">
	.inputsr {
		width: 400upx !important;
		height: 64upx;
		background: #F5F5F6;
		border-radius: 8upx 8upx 8upx 8upx;
		opacity: 1;
		border: 2upx solid #EEEEEE;
		padding: 16upx 18upx;
		box-sizing: border-box;
		padding-right: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26upx;
		font-weight: 400;
		color: #092C4D;

		>.timeInput {
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			flex: 1;
			font-size: 26upx;
		}
	}

	.sccv {
		width: 100%;
		padding-top: 20upx;

		.mabx {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 100%;
			box-sizing: border-box;
			padding: 20upx 32upx;
			padding-top: 0upx;

			>view {
				width: 686upx;
				background: #FFFFFF;
				display: flex;
				flex-direction: column;
				box-sizing: border-box;
				border-bottom: 2upx solid #ebebeb;
				padding-bottom: 30upx;
				background-color: #ffffff;
				margin-bottom: 20upx;
				padding-top: 20upx;
				border-radius: 8upx;

				>view:nth-last-child(1) {
					border-bottom: none !important;
				}

				.topbox {
					display: flex;
					flex-direction: column;
					padding: 24upx;
					border-bottom: 2upx solid #EEEEEE;

					>.type1 {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						flex-wrap: wrap;
						margin-bottom: 20upx;
						font-size: 28upx;
						color: #161616;

						>view {
							min-width: 50%;
						}
					}

					>.type2 {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-around;
						margin-bottom: 15upx;
						font-size: 28upx;
						color: #161616;
						margin-top: 20upx;

						>view {
							display: flex;
							flex-direction: column;
							align-items: center;

							>view:nth-of-type(1) {
								font-size: 28upx;
								font-weight: 400;
								color: #092C4D;
							}

							&:nth-of-type(1) {
								>view:nth-of-type(2) {
									color: #092C4D;
								}
							}

							&:nth-of-type(2) {
								>view:nth-of-type(2) {
									color: #3AD8BC;
								}
							}

							&:nth-of-type(3) {
								>view:nth-of-type(2) {
									color: #FA8C16;
								}
							}

							>view:nth-of-type(2) {
								font-size: 36upx;
								margin-top: 10upx;

							}
						}
					}

					>.type3 {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 20upx;
						font-size: 26upx;
						color: #161616;
					}
				}

				>.bts {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 20upx;

					>view:nth-of-type(1) {
						width: 204upx;
						height: 64upx;
						background: #FFFFFF;
						border-radius: 8upx;
						opacity: 1;
						border: 2upx solid #0086F1;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 28upx;
						font-weight: 400;
						color: #0086F1;

					}

					>view:nth-of-type(2) {
						width: 204upx;
						height: 64upx;
						background: #FFFFFF;
						border-radius: 8upx;
						opacity: 1;
						border: 2upx solid #D3832A;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 28upx;
						font-weight: 400;
						color: #D3832A;
						margin-left: 100upx;
					}
				}
			}
		}
	}

	.tabtip {
		display: flex;
		align-items: center;
		justify-content: space-between;

		>view {
			width: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #ffffff;
			font-size: 32upx;
			font-weight: 400;
			color: #90A0AF;

			>view {
				height: 80upx;
				border-bottom: 8upx solid #ffffff;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.xz {
			color: #D3832A;
			border-bottom: 8upx solid #D3832A;
			// border-radius: 5upx;
		}
	}

	.schbox {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15upx 32upx;
		box-sizing: border-box;
		background-color: #ffffff;

		>view:nth-of-type(1) {
			// width: 150upx;
			font-size: 28upx;
			font-weight: 400;
			color: #092C4D;
		}

		>view:nth-of-type(2) {
			// width: 380upx;
			flex: 1;
			// height: 64upx;
		}

		>view:nth-of-type(3) {
			margin-left: 10upx;
			width: 128upx;
			height: 64upx;
			background: #D3832A;
			border-radius: 8upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32upx;
			font-weight: 400;
			color: #FFFFFF;
		}
	}

	// 搜索
	.searchBox {
		position: relative;

		.iconBox {
			height: 100%;
			position: absolute;
			top: 0;
			right: 30upx;
			display: flex;
			align-items: center;
		}
	}
</style>