<template>
	<view class="uni-column" style="height: 100%; background-color: #f5f5f5;position: absolute;
    left: 0;
    right: 0;">
		<view class="scroll-container" style="padding-bottom:  150rpx">
			<view v-if="listData.length == 0" style="color: #999;margin: 50% auto;">
				<text>暂无批次</text>
			</view>
			<!-- 批次列表 -->
			<view v-else v-for="(item, index) in listData" :key="index" class="list-item">
				<view class="title-container uni-row" style="justify-content: flex-start;">
					<view class="title uni-row">
						<text class="label">批次号:</text>
						<text class="label code">{{ item['lotCode'] }}</text>
					</view>
					<view class=" uni-row" style="margin-left: 16rpx;">
						<view v-if="item['isTaksStock'] == 0" class="tag finished"><text class="label">未盘点</text></view>
						<view v-else type="default finished" class="tag turnover"><text class="label">已盘点</text></view>
					</view>
				</view>
				<view class="item-info uni-row">
					<text class="label">产品描述</text>
					<text class="label right">{{ item['productDescription'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">投产数</text>
					<text class="label right">{{ item['prodNum']}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">盘点数量</text>
					<input v-if="item.flag" class="number" type="number" v-model="item.taksStockNum" placeholder="" />
					<view v-else class="value">{{ item.taksStockNum }}</view>
				</view>
			</view>
		</view>
		<view class="bottom uni-row">
			<button class="start-batch-btn" style="margin-right: 10rpx; " type="primary" @click="handleSave">确定</button>
			<button class="start-batch-btn" style="background-color:#fe5555;" @click="handleCancel">取消</button>
		</view>
	</view>

</template>

<script setup>
	import {
		normalizeProps,
		reactive,
		onMounted,
		ref
	} from 'vue'
	import {
		onLoad,
		onReady,
		onUnload,
		onShow
	} from '@dcloudio/uni-app'
	import {
		updateTakesStock
	} from '@/api/business/taksStackLot.js'
	import {
		store
	} from '@/store/index.js'
	import {
		toHHmmss
	} from '@/utils/common.js'
	import {
		onPullDownRefresh
	} from "@dcloudio/uni-app"

	const listData = ref([])
	onShow(() => {
		reflush();

	})

	;

	function reflush() {
		init();
	}


	function init(id) {
		let temperList = store.takeStockDetails
		console.log(store.takeStockDetails)
		console.log(store.userInfo.userId)
		temperList.forEach(item => {
			item.flag = true
			if (item.isTaksStock == 0) {
				item.taksStockNum = item.prodNum
			}
			if (item.firstUpdateId != 0 && store.userInfo.userId != item.firstUpdateId) {
				item.flag = false
				uni.showToast({
					icon: "none",
					title: '该批次已被其他人盘点'
				});
			}

		})
		listData.value = temperList
	}

	function handleSave() {
		console.log(Number(listData.value[0].taksStockNum) < 0)
		for (let i = 0; i < listData.value.length; i++) {
			if (Number(listData.value[i].taksStockNum) < 0) {
				uni.showToast({
					icon: 'none',
					title: listData.value[i].lotCode + '批次盘点数量不能小于0'
				})
				return
			}
		}
		update()
	}

	function handleCancel() {
		let pages = getCurrentPages();
		let index = 0;
		for (let i = 0; i < pages.length; i++) {

			if (pages[i].$page.fullPath == "/pages/takeStock/index") {

				index = pages.length - i - 1;
			}
		}
		uni.navigateBack({
			delta: index
		});
	}

	function update() {
		updateTakesStock(listData.value).then(res => {
			uni.showToast({
				icon: 'none',
				title: '盘点成功'
			})
			// 	uni.navigateTo({
			// 	url: '/pages/takeStock/index'
			// })
			let pages = getCurrentPages();
			let index = 0;
			for (let i = 0; i < pages.length; i++) {

				if (pages[i].$page.fullPath == "/pages/takeStock/index") {

					index = pages.length - i - 1;
				}
			}
			uni.navigateBack({
				delta: index
			});
		})
		console.log(listData.value)
	}
</script>

<style lang="scss">
	$nav-height: 60rpx;

	/* 遮罩层样式 */
	.mask {
		position: fixed;
		/* 固定定位，覆盖整个屏幕 */
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.3);
		/* 黑色背景，透明度30% */
		display: flex;
		justify-content: center;
		/* 水平居中 */
		align-items: center;
		/* 垂直居中 */
		z-index: 1000;
		/* 确保遮罩层在其他元素之上 */
	}

	.box-bg {
		width: 94%;
		background-color: #F5F5F5;
		padding: 5rpx 16rpx;
		justify-content: space-around;
		align-items: center;
		margin: 24rpx auto 0;

		.input-view {
			width: 100%;
			flex: 4;
			background-color: #f8f8f8;
			height: $nav-height;
			border: 1rpx solid #999;
			border-radius: 15rpx;
			padding: 0 15rpx;
			flex-wrap: nowrap;
			margin: 0 10rpx 0;
			line-height: $nav-height;

			.input-uni-icon {
				line-height: $nav-height;
			}

			.nav-bar-input {
				width: 80%;
				height: $nav-height;
				line-height: $nav-height;
				padding: 0 5rpx;
				background-color: #f8f8f8;
			}
		}

		.search {
			width: 20%;
			text-align: center;
			color: #808080;
		}
	}

	.list-title {
		width: 100%;
		margin-top: 16rpx;
		height: 64rpx;
		line-height: 64rpx;
		align-items: center;
		margin-left: 32rpx;

		.label {

			font-size: 32rpx;
			margin-right: 24rpx;
		}

		.icon-gear {
			font-size: 56rpx;
		}
	}

	.switch {
		margin-top: -8rpx;
		transform: scale(0.7);
	}

	.scroll-container {
		width: 92%;
		margin: 24rpx auto 0 auto;
		height: calc(90% - 100rpx);
		overflow: auto;
	}

	.list-item {
		background-color: #fff;
		position: relative;
		padding: 16rpx;
		padding-bottom: 24rpx;
		margin-bottom: 24rpx;
		border-radius: 24rpx;

		.title-container {

			margin-top: 8rpx;
			margin-bottom: 16rpx;

			.title {
				height: 48rpx;
				align-items: center;

				.label {
					font-size: 32rpx;
					font-weight: bold;

					&.code {
						margin-left: 8rpx;
					}
				}
			}

			.tag {
				border: 1px solid #1CE5B0;
				background-color: #F6FFFD;
				padding: 8rpx;
				border-radius: 8rpx;

				.label {
					color: #1CE5B0;
					font-size: 24rpx;
				}

				&.finished {
					border: 1px solid #BBBBBB;
					background-color: #F5F5F5;

					.label {
						color: #BBBBBB;
					}
				}

				&.turnover {
					border: 1px solid #FF7901;
					background-color: #F6FFFD;

					.label {
						color: #FF7901;
					}
				}
			}
		}

		.item-info {
			margin-bottom: 8rpx;

			.label {
				font-size: 28rpx;
				width: 220rpx;
				color: #808080;

				&.right {
					flex: 1;
					color: #000000;
				}
			}

			input {
				width: 280rpx;
				height: 56rpx;
				border: 1px solid #9f9f9f;
				font-size: 28rpx;

				&.number {
					width: 220rpx;
					text-align: left;
				}
			}
		}

		.status-btn {
			justify-content: flex-end;
			align-items: center;

			.turnover-tag {
				padding-right: 12rpx;
				padding-left: 12rpx;
				border-radius: 8rpx;
				border: 1rpx solid #FF7901;
				background-color: #FF7901;
				font-size: 28rpx;
				color: #FFFFFF;

			}

			.reporting-tag {
				padding-right: 12rpx;
				padding-left: 12rpx;
				border-radius: 8rpx;
				margin-left: 16rpx;
				border: 1rpx solid #1684fc;
				background-color: #1684fc;
				font-size: 28rpx;
				color: #FFFFFF;

			}
		}
	}

	.bottom {
		height: 10%;
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100rpx;
		border-top: 1px solid #999999;
		padding: 16rpx 32rpx;
		align-items: center;
		background-color: #fff;
		justify-content: space-evenly;

		.start-batch-btn {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 8rpx;
			color: #FFFFFF;
			font-size: 28rpx;
		}
	}
</style>