<template>
	<view class='container'>
		<view class='content'>
			<view class='title uni-row'>
				<text>当前箱号</text>
			</view>
			<view class="vehicleList uni-row">
				<view class="vehicleNo uni-row" v-for="(item,index) in newBindList">
					<text>{{item.carrierCode}}</text>
					<text @click="handleDelVehicleNo(item,index)">×</text>
				</view>
			</view>
			<view v-if="discardVehicleList.length > 0" class="disuseList">
				<view class="uni-row" style="align-items: center;justify-content: space-evenly;">
					<view>箱号</view>
					<view>是否废弃</view>
					<view>废弃原因</view>
				</view>
				<view class="vehicleNo uni-row" v-for="(item,index) in discardVehicleList"
					style="align-items: center;justify-content: space-around;">
					<view class="uni-row discardVehicleNo"><text>{{item.carrierCode}}</text>
						<text @click="handleDelDiscardVehicleList(item,index)">×</text>
					</view>
					<view class="uni-row">
						<switch color="#ff0000" style="transform:scale(0.7)" @change="handleSwitchChange(item)" />
					</view>
					<view class="uni-row" v-if="item.checked">
						<uni-data-select v-model="item.reason" :localdata="abanonmentList" style="width: 240rpx;"
							:clear="false"></uni-data-select>
					</view>
					<!-- 占空位用 -->
					<view class="uni-row" v-if="!item.checked">
						<view style="width: 240rpx;"></view>
					</view>
				</view>
			</view>
		</view>
		<view class='bottom uni-row'>
			<button class='bottom-btn left-btn' @click="handleScanCode">扫码</button>
			<button class='bottom-btn right-btn' type=primary @click="debounce(handleSubmit,300)">提交</button>
		</view>
	</view>
</template>
<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app'
	import {
		getDictInfoByType
	} from '@/api/dict/dict.js'
	import {
		getDayworkCarrierList
	} from '@/api/business/dayworkCarrier.js'
	import {
		saveDayWork
	} from '@/api/business/dayWork.js'
	import {
		store
	} from '@/store/index.js'
	import {
		debounce
	} from '@/utils/common.js'
	import {
		addCarrierReject,checkCarrier
	} from '@/api/business/carrier.js'

	const bindList = ref([])
	const newBindList = ref([])
	const discardVehicleList = ref([])
	const abanonmentList = ref([])

	let dayWorkInfo = store.dayworkInfo;

	onLoad(() => {
		let reqParam = {
			dayworkId: dayWorkInfo.id,
			isChanged: 0
		}
		init(reqParam);
	})

	function init(data) {
		console.log(data)
		getDayworkCarrierList(data).then(res => {
			console.log(res)
			if (res.code == 200) {
				bindList.value = [...res.rows];
				newBindList.value = res.rows;
			}
		})
		getDictInfoByType('reason_for_abandonment').then((res) => {
			for (var i = 0; i < res.data.length; i++) {
				abanonmentList.value[i] = {
					text: res.data[i].dictLabel,
					value: res.data[i].dictValue
				}
			}
		})
	}

	function handleSwitchChange(item) {
		item.checked = !item.checked
	}

	// function handleSelectRejectReason(item) {
	// 	for (var i = 0; i < abanonmentList.value.length; i++) {
	// 		if(abanonmentList.value[i].value == item.reason ) {
	// 			item.reason = abanonmentList.value[i].text
	// 		}
	// 	}
	// }
	function handleDelVehicleNo(item, index) {
		if (bindList.value.includes(item)) {
			console.log(bindList.value)
			newBindList.value.splice(index, 1);
			let discardVehicleInfo = {
				carrierCode: item.carrierCode,
				checked: false, 
				reason: '',
				carrierId: item.carrierId,
				isAbandoned: 1,
				operationDate: new Date()
			}
			discardVehicleList.value.push(discardVehicleInfo)
		} else {
			newBindList.value.splice(index, 1);
		}
	}

	function handleDelDiscardVehicleList(item,index) {
		discardVehicleList.value.splice(index, 1)
		newBindList.value.push(item)
	}

	function handleScanCode() {
		uni.scanCode({
			scanType: ['qrCode'], // 条形码扫描
			onlyFromCamera: true, // 只允许相机扫码
			autoZoom: false,
			success: function(res) {
				let vehicleObj = JSON.parse(res.result);
				if (!vehicleObj.carrierId || vehicleObj.carrierId == "") {
					uni.showToast({
						icon: "error",
						title: "请扫载具码"
					})
					return;
				}
				for (let i = 0; i < newBindList.value.length; i++) {
					if (newBindList.value[i].carrierId === vehicleObj.carrierId) {
						uni.showToast({
							icon: "error",
							title: "载具已存在"
						})
						return;
					}
				}
				checkCarrier(vehicleObj.carrierId).then(response => {
					console.log(response)
						if(response.code == 200){
							newBindList.value.push(JSON.parse(res.result));
						}else{
							uni.showToast({
								icon: 'none',
								title: response.msg
							})
							return;
						}
				})
			}
		});
	}

	function handleSubmit() {
		let carrierRejectList = []
		for (var i = 0; i < discardVehicleList.value.length; i++) {
			if (discardVehicleList.value[i].checked && discardVehicleList.value[i].reason == "") {
				uni.showToast({
					icon: 'none',
					title: '第' + (i + 1) + '条未选择废弃原因'
				});
				return;
			}
			if (discardVehicleList.value[i].checked && discardVehicleList.value[i].reason !== "") {
				carrierRejectList.push(discardVehicleList.value[i])
			}
		}
		if (carrierRejectList.length > 0 && newBindList.value.length > 0) {
			addCarrierReject(carrierRejectList).then((response) => {
				if (response.code == 200) {
					uni.showToast({
						icon: 'success',
						title: '操作成功',
					});
				} else {
					uni.showToast({
						icon: 'error',
						title: '添加失败',
					});
				}
			})
		}
		dayWorkInfo.dayworkCarriers = newBindList.value;
		saveDayWork(dayWorkInfo).then(res => {
			if (res.code === 200) {
				uni.showToast({
					icon: 'success',
					title: '添加成功'
				});
				// uni.$emit('batchReporting-addBatch')
				uni.$emit('dayworkItemUpdate');
				uni.navigateBack({
					url: '/pages/batchReporting/index'
				})
			} else {
				uni.showToast({
					icon: 'error',
					title: res.msg
				});
			}
		})
	}
</script>

<style lang="scss">
	.container {
		height: 100%;
		background-color: #f5f5f5;
	}

	.bottom {
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100rpx;
		padding: 16rpx 24rpx;
		align-items: center;
		background-color: #FFFFFF;
		justify-content: space-between;

		.bottom-btn {
			flex: 1;
			font-size: 28rpx;
			color: #FFFFFF;

			&.left-btn {
				background-color: rgba(0, 226, 166, 1);
			}

			&.right-btn {
				margin-left: 24rpx;
			}
		}
	}

	.content {
		width: 90%;
		height: calc(100% - 100rpx - 100rpx - 80rpx);
		background-color: rgba(255, 255, 255, 1);
		margin: 50rpx auto;
		padding-bottom: 50rpx;
		border-radius: 12rpx;

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

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

		.disuseList {
			border-top: 2rpx solid #e1e1e1;
			height: calc(100% - 480rpx - 20rpx);
			overflow: auto;
			width: 90%;
			margin: 0 auto;
			padding-top: 20rpx;
		}
	}

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

	.title {
		margin: 30rpx auto;
		width: 30%;
		font-size: 36rpx;
		font-weight: bold;
	}

	.btn {
		position: fixed;
		top: 940rpx;
		left: 0;
		right: 0;
	}

	.segment {
		width: 280rpx;
		height: 1rpx;
		border: 1px solid rgba(187, 187, 187, 1);
	}
</style>