<template>
	<view class="uni-column" style="height: 100%; background-color: #f5f5f5;">
		<scroll-view class="scroll-container" scroll-y>
			<view v-for="(item, index) in listData" :key="index" class="list-item"
				@click="handleToreportingForWork(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['status'] == 1" class="tag"><text class="label">进行中</text></view>
						<view v-else-if="item['status'] == 3" class="tag turnover "><text class="label">周转中</text>
						</view>
						<view v-else-if="item['status'] == 2" class="tag turnover "><text class="label">待周转</text>
						</view>
						<view v-else-if="item['status'] == 0" class="tag turnover "><text class="label">未开始</text>
						</view>
						<view v-else-if="item['status'] == 4" type="default" class="tag finished"><text
								class="label">已完成</text></view>
					</view>
				</view>
				<view class="item-info uni-row">
					<text class="label">箱号</text>
					<text class="label right">{{ item['carrierName'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">总工时</text>
					<text class="label right">{{ item['totalWorkingHours']}}h</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">合格数/投入数</text>
					<text class="label right">{{ item['qualifiedQuantity'] }}/{{item['invest']}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">上道工序</text>
					<text
						class="label right">{{ item.prevProcess && item.prevProcess.length > 0 ? item.prevProcess[item.prevProcess.length - 1].processAlias : '无'}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">当前工序</text>
					<text class="label right">{{ item['currentProcess'].processAlias }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">下道工序</text>
					<text
						class="label right">{{item.prevProcess && item.nextProcess.length > 0 ? item.nextProcess[0].processAlias : null }}</text>
				</view>
				<view class="status-btn uni-row">
					<view v-if="item['status'] == 2" class=" uni-row">
						<button class="turnover-tag" size="mini"
							@click.stop="handleShowTurnoverApplication(item)">周转申请</button>
						<!-- <button class="reporting-tag" size="mini" @click="handleToreportingForWork">去报工</button> -->
					</view>
					<view v-else-if="item['status'] == 2" class=" uni-row">
						<button class="turnover-tag" size="mini" @click.stop="handleCancelTurnover(item)">取消周转</button>
					</view>
				</view>
			</view>
		</scroll-view>
		<view v-if="bottomStatus == 0" class="bottom uni-row">
			<button class="start-batch-btn" type="primary" @click="handleStartNewBatch">开始新批次</button>
		</view>
		<view v-else-if="bottomStatus == 1" class="bottom uni-row">
			<button class="start-batch-btn" type="primary" @click="handleScanCode">扫一扫开始新批次</button>
		</view>
		<view v-else="bottomStatus == 2" class="bottom uni-row">
			<button class="start-batch-btn" style="width: 40%;margin-right: 10rpx;" type="primary"
				@click="handleStartNewBatch">开始新批次</button>
			<button class="start-batch-btn" style="width: 40%;margin-left: 10rpx" type="primary"
				@click="handleScanCode">扫一扫开始新批次</button>
		</view>
		<dialog-lot ref="lotDialog" @submit="handleDoIt" />
		<dialog-turnoverApplication ref="turnoverApplicationDialog" @confirm="handleDoTurnoverAfter"/>
	</view>

</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue'
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app'
	import {
		getDayWorkList,
		showDaywork,
		showDayworkSave,
		turnoverDelete
	} from '@/api/business/dayWork.js'
	// import {
	// 	getDayWorkItemList
	// } from '@/api/business/dayWorkItem.js'
	import {
		store
	} from '@/store/index.js'


	const turnoverApplicationDialog = ref(null)
	const lotDialog = ref(null)
	const listData = ref([])
	const bizDayworkObj = ref({})
	const curSubPlan = ref({}) // 子计划单信息
	const processInfo = ref([]) // 员工工序信息
	const bottomStatus = ref(0) // 底部按钮显示
	const reqParam = ref(null);
	const curDayworkItem = ref({}) // 当前报工记录,回显是否显示取消周转的状态	

	onLoad(() => {
		curSubPlan.value = store.planSubDetails || {
			processSequence: []
		};
		console.log(store.planSubDetails)
		processInfo.value = store.userProcessInfo || [];
		let sunPlanProcesses = [];
		let userProcesses = [];
		for (let i = 0; i < curSubPlan.value.processSequence.length; i++) {
			sunPlanProcesses.push(curSubPlan.value.processSequence[i].id)
		}
		for (let i = 0; i < processInfo.value.length; i++) {
			userProcesses.push(processInfo.value[i].id)
		}
		// 判断员工开始批次按钮显示bottomStatus状态
		if (userProcesses.includes(sunPlanProcesses[0]) && userProcesses.length == 1) {
			bottomStatus.value = 0;
		} else if (userProcesses.includes(sunPlanProcesses[0]) && userProcesses.length > 1) {
			bottomStatus.value = 2;
		} else {
			bottomStatus.value = 1;
		}
		init(curSubPlan.value.id);
		uni.$on('batchReporting-addBatch', () => {
			init(curSubPlan.value.id);
		})
	})


	function handleShowTurnoverApplication(data) {
		let _data = data ?? {}
		// 调用子组件中的方法
		turnoverApplicationDialog.value.open(_data)
	}

	function handleStartNewBatch() {
		uni.navigateTo({
			url: "/pages/addNewBatch/index"
		})
	}

	function init(id) {
		uni.showLoading({
			title: '加载中'
		});
		let reqData = {};
		reqData.productionPlanDetailSubDetailId = id;
		console.log(reqData)
		getDayWorkList(reqData).then(res => {
			console.log(reqData)
			listData.value = res.rows;
			// 设置下道序
			for (let i = 0; i < listData.value.length; i++) {
				for (let j = 0; j < listData.value[i].processSequence.length; j++) {
					if (listData.value[i].currentProcess.processCode === listData.value[i].processSequence[j]
						.processCode) {
						listData.value[i].nextProcess = (j + 1) < listData.value[i].processSequence.length ?
							listData.value[i].processSequence.slice(j + 1) : [];
					}
				}
			}
			console.log(listData.value)
		})
		setTimeout(function(){
			uni.hideLoading();
		},2000)
	}

	function handleToreportingForWork(item) {
		store.dayworkInfo = item
		uni.navigateTo({
			url: "/pages/reportingForWork/index"
		})
	}

	function handleScanCode() {
		uni.scanCode({
			scanType: ['qrCode'],
			onlyFromCamera: true, // 只允许相机扫码
			success: function(res) {
				console.log(res.result)
				let vehicleObj = JSON.parse(res.result);
				showDaywork({
					carrierId: vehicleObj.carrierId,
					status: 7
				}).then(response => {
					if(response.code == 200){
						lotDialog.value.open(response.data);
						// reqParam.value = response.data
					}
				})
			}
		});
	}

	function handleAddDaywork(data) {
		// console.log(data)
		// for (let i = 0; i < listData.value.length; i++) {
		// 	if (Object.entries(listData.value[i]).toString() === Object.entries(data).toString()) {
		// 		uni.showToast({
		// 			icon: "error",
		// 			title: "该批次已存在"
		// 		})
		// 		console.log("该批次已存在")
		// 		return;
		// 	}
		// }
		// listData.value.push(data);
		// for (let i = 0; i < listData.value.length; i++) {
		// 	listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
		// }
		// console.log(listData.value)
		// console.log(data)
		showDayworkSave(data.daywork).then(res => {
			console.log(res)
			init(curSubPlan.value.id);
		})
	}

	function handleDoIt(data) {
		handleAddDaywork(data);
	}
	
	function handleDoTurnoverAfter(){
		init();
	}
	
	function handleCancelTurnover(data){
		turnoverDelete(data).then(res => {
			if(res.code == 200){
				uni.showToast({
					icon: "success",
					title: "取消成功",
					duration: 2000
				})
				init(curSubPlan.value.id);
			}else{
				uni.showToast({
					icon: "fail",
					title: "取消失败",
					duration: 2000
				})
			}
		})
	}
</script>

<style lang="scss">
	.scroll-container {
		width: 92%;
		margin: 24rpx auto 0 auto;
		height: 90%;
	}

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

		.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;
				}
			}
		}

		.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;

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