<template>
	<dialog-base ref="baseDialog" title="结束报工">
		<view class="list-container">
			<view class="list-title uni-row">
				<text class="label">废品信息</text>
				<view class="uni-row" style="justify-content: flex-end;"><text class="label" style="color: blue;"
						@click="handleAddWasteInfo">+添加</text></view>
			</view>
			<view class="list-title uni-row" v-for="(item, index) in wasteInfo">
				<input class="input" @input="handleInputRejectNum(item)" style="width: 20%;" v-model="item.rejectNum"
					placeholder="废品量" />
				<uni-data-select style="margin-left: 40rpx;width: 70%;" v-model="item.reason" :localdata="reasonList"
					@change="handleReasonChange(item)" :clear="false"></uni-data-select>
				<!-- <zxz-uni-data-select v-model="item.reason" filterable :localdata="reasonList" placeholder="请选择废品原因"
					@change="handleReasonChange(item)"></zxz-uni-data-select> -->

				<uni-icons class="icon-gear" type="close" size="40" color="red"
					@click="handleDeleteWasteInfo(index)"></uni-icons>
			</view>
		</view>
		<view class="add-btn-container uni-row">
			<button type="default" class="btn" @click="submitReject">提交</button>
		</view>
	</dialog-base>
</template>


<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		updateDayWorkItem,
		saveDayWorkItem
	} from "@/api/business/dayWorkItem.js"
	import {
		getDayworkCarrierList
	} from '@/api/business/dayworkCarrier.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime,
		toHHmmss
	} from '@/utils/common.js'
	import {
		getDictInfoByType
	} from '@/api/dict/dict.js'
	import {
		getSpecialDeptProcessList
	} from '@/api/business/deptProcess.js'
	import {
		debounce
	} from '@/utils/common.js'
	import {
		getDayWorkItemList,
		listItem,
		getIsFirstOrder,
	} from '@/api/business/dayWorkItem.js'

	const baseDialog = ref(null)
	const workInfo = ref({})
	const wasteInfo = ref([])
	const reasonList = ref([])
	const emit = defineEmits(['resflushItem'])
	const showCarrierList = ref(false) // 工序是否完成显示载具列表
	const bindList = ref([])
	const confirmCarrierList = ref([])
	const isFirstOrder = ref(true); //是否为首序
	const endFlag = ref(0)
	const specialDeptProcessList = ref([])
	const temp = ref(0) // 保存workInfo.value.qualifiedNum的值(还原用)
	const lotPreSumReject = ref(0)
	const lotPreSumQualifiedNum = ref(0)
	const remainingQualifiedNum = ref(0) // 当前的合格量还能最大添加多少(剩余数量)
	const dayworkItemInfo = ref(null)

	onLoad(() => {})

	function open(data, itemListData) {
		// console.log(store.isPreProcess)
		// console.log("777")
		resetPage();
		workInfo.value = {
			...data,
		};
		dayworkItemInfo.value = data
		// wasteInfo.value = data.rejectList == null ? [] : data.rejectList
		// wasteInfo.value = [{
		// 	reason: '2',
		// 	rejectNum: 2
		// }]
		// console.log(data)
		// console.log("数据data", workInfo);
		//查看是否为首序
		// console.log(store.dayworkInfo.id)
		// getIsFirst(data.dayworkId);
		workInfo.value.qualifiedNum = 0;
		init();
		baseDialog.value.open()
	}

	function close() {
		baseDialog.value.close()
	}

	function getIsFirst(dayworkId) {
		console.log("getIsFirst")
		getIsFirstOrder(dayworkId).then(res => {
			if (res.code == 200) {
				isFirstOrder.value = res.data.isFirstOrder;
				console.log(res)
			}
		}).catch(err => {
			console.log(err)
		})
	}

	function resetPage() {
		workInfo.value.qualifiedNum = null;
		workInfo.value = {};
		wasteInfo.value = [];
		reasonList.value = [];
		showCarrierList.value = false;
		bindList.value = [];
		confirmCarrierList.value = [];
		endFlag.value = 0;
		lotPreSumReject.value = 0;
		lotPreSumQualifiedNum.value = 0;
		remainingQualifiedNum.value = 0;
	}

	function init() {
		// console.log('init')
		getDictInfoByType("waste_causes").then(res => {
			// console.log('waste_causes')
			for (let i = 0; i < res.data.length; i++) {
				reasonList.value[i] = {
					value: res.data[i].dictValue,
					text: res.data[i].dictLabel
				}
			}
			wasteInfo.value = dayworkItemInfo.value.rejectList == null ? [] : dayworkItemInfo.value.rejectList.map(
				e => ({
					...e
				}))
		}).catch(err => {
			console.log('189 err')
		})
	}

	function handleInputQualifiedNum() {
		workInfo.value.qualifiedNum = workInfo.value.qualifiedNum.replace(/^-+|[^\d]/g, '');
	}

	function handleInputRejectNum(item) {
		item.rejectNum = item.rejectNum.replace(/^0+|^-|[^\d]/g, '');
		// 合格数随着投产数
		//let sumRejectNum = 0;
		// for (let i = 0; i < wasteInfo.value.length; i++) {
		// 	wasteInfo.value[i].rejectNum = wasteInfo.value[i].rejectNum.replace(/^0+|^-|[^\d]/g, '');
		// 	// sumRejectNum += Number(wasteInfo.value[i].rejectNum);
		// }
		// if (temp.value > 0) {
		// 	debounce(function() {
		// 		workInfo.value.qualifiedNum = temp.value;
		// 		workInfo.value.qualifiedNum -= sumRejectNum;
		// 	}, 500)
		// }
	}

	/*
		function handleScanCode() {
			// 引入原生插件
			const mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
			if (mpaasScanModule) {
			//调用插件的 mpaasScan 方法
			mpaasScanModule.mpaasScan({
					// 扫码识别类型，参数可多选，qrCode、barCode，
					// 如不设置，默认识别所有扫码类型，可能有些许影响识别效率
					scanType: ["qrCode", "barCode"],
					// 是否隐藏相册，默认false不隐藏
					hideAlbum: false,
				},
				(ret) => {
					let vehicleObj = JSON.parse(ret.resp_result);
					let hasInFlag = false
					for (let i = 0; i < bindList.value.length; i++) {
						if (vehicleObj.carrierCode == bindList.value[i].carrierCode) {
							hasInFlag = true
							if (!bindList.value[i].flag) {
								endFlag.value += 1;
							}
							bindList.value[i].flag = true;
						}
					}
					if(!hasInFlag) {
						uni.showToast({
							icon: 'none',
							title: '该批次没有绑定此箱',
							duration: 2000
						})
					}
					if (endFlag.value < bindList.value.length && hasInFlag) {
							 setTimeout(() => {
							    handleScanCode();
							  }, 700);
					}
				}
			);
			}
		}
	*/

	function handleAddWasteInfo() {
		if (wasteInfo.value.length > 0) {
			if (wasteInfo.value.some(item => item.rejectNum == '' || item.reason == '')) {
				uni.showToast({
					icon: 'none',
					title: '请填写废品量或废品原因再点击添加'
				})
			} else {
				wasteInfo.value.push({
					rejectNum: '',
					reason: ''
				})
			}
		} else {
			wasteInfo.value.push({
				rejectNum: 0,
				reason: ''
			})
		}

	}

	function handleDeleteWasteInfo(index) {
		wasteInfo.value.splice(index, 1);
	}

	function submitReject() {
		// 提交到父组件
		emit('resflushItem', wasteInfo.value);
		close()
	}

	function handleReasonChange(item) {
		// console.log(typeof item)
		// console.log(item)
		if (wasteInfo.value.filter(waste => waste.reason == item.reason).length > 1) {
			wasteInfo.value.splice(wasteInfo.value.indexOf(item), 1);
			uni.showToast({
				icon: 'none',
				title: '请勿重复选择废品原因',
				duration: 2000
			})
		}
	}

	defineExpose({
		open
	})
</script>

<style lang="scss">
	.dialog-body {
		.list-container {
			width: 100%;
			display: flex;
			align-items: flex-start;
			padding: 0 4rpx;

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

				.label {
					flex: 1;
					font-size: 32rpx;
				}

				.input {
					width: 40%;
					height: 66rpx;
					padding-left: 10rpx;
					font-size: 22rpx;
					border: 1rpx solid #e1e1e1;
					border-radius: 8rpx;
				}

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

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

			.confirmCarrier {
				.label {
					padding-top: 20rpx;
					font-size: 32rpx;
				}

				.vehicleList {
					justify-content: baseline;
					align-content: flex-start;
					flex-wrap: wrap;
					width: 100%;
					height: 120rpx;
					overflow: auto;

					.vehicleNo {
						padding: 0 10rpx;
						margin: 10rpx 20rpx 0 0;
						justify-content: space-between;
						align-items: center;
						width: auto;
						height: 60rpx;
						border: 2rpx solid rgba(213, 213, 213, 1);
						border-radius: 6rpx;
					}

					.blueBorder {
						background-color: #1684fc;
						color: #FFFFFF;
					}

				}
			}


		}

		.add-btn-container {
			margin-top: 32rpx;


			.btn {
				flex: 1;
				background-color: rgba(255, 85, 85, 1);
				color: #FFFFFF;
				margin-left: 5rpx;
				padding: 0;
			}

			.btnScan {
				flex: 1;
				color: #FFFFFF;
				margin-right: 5rpx;
				padding: 0;
			}
		}
	}
</style>