<template>
	<view class="qr-container">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="扫码列表" background-color="#157A2C" color="#fff"
			@clickLeft="back" right-icon="scan" @clickRight="HandlScanCode" height="88rpx">
		</uni-nav-bar>

		<view class="main">
			<view class="wrapper">
				<u-list class="listContent">
					<checkbox-group v-for="(item, index) in listData" :key="index"
						@change.stop="e => selectVal(e, item, index)">
						<label>
							<view class="listBox">
								<view class="listBox-sel">
									<checkbox :value="item.code" color="#fff" :disabled="item.disabled"
										:checked="item.checked" />
								</view>
								<view class="listBox-con">
									<view style="display: flex;">
										<view class="listBox-top" style="justify-content: left;">
											<view class="round">{{Number(index)+1}}</view>
											<view class="listBox-name">
												{{ item.categoryName }}
											</view>
										</view>
										<view class="listBox-top">
											<view class="listBox-code">
												{{ item.packageNo }}
											</view>
										</view>
									</view>
									<view class="listBox-bottom">
										<view>批次号：{{ item.batchNo }}</view>
										<view>规格：{{ item.specification }}</view>
										<view>
											单件数量：{{ item.measureQuantity }}{{ item.measureUnit }}/{{ item.packingQuantity }}{{ item.packingUnit }}
										</view>
										<view>重量：{{ item.weight }}({{ item.weightUnit }})</view>

										<view>生产日期：{{ item.productionDate?item.productionDate.slice(0,10):'' }}</view>
										<view>供应商：{{ item.supplierName }}</view>
										<view v-if="dimension == 3">发货条码：{{ item.barcodes }}</view>
										<view v-if="dimension == 3">物料代号：{{ item.materielDesignation }}</view>
										<view v-if="dimension == 3">客户代号：{{ item.clientCode }}</view>
										<view v-if="dimension == 3">刻码：{{ item.engrave }}</view>
										<view v-if="dimension == 3">仓库：{{ item.warehouseName }}</view>
									</view>
								</view>
							</view>
						</label>
					</checkbox-group>
					<u-empty class="noDate" style="margin-top: 20vh" v-if="!listData.length"></u-empty>
				</u-list>
			</view>

			<view class="footer">
				<view class="bottom">
					<checkbox v-if="!seletedAll" color="#fff" :checked="seletedAll" @tap="_seletedAll">全选</checkbox>
					<checkbox class="select-all" color="#fff" v-else :checked="seletedAll" @tap="_seletedAll">取消全选
					</checkbox>
				</view>
				<u-button type="success" size="small" class="u-reset-button" :disabled="!checkListLen" @click="jumpAdd">
					<view class="selBtn">选择( {{ checkListLen }} )</view>
				</u-button>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
	import {
		getTreeByIds,
		getPackingList,
		getWarehouseList,
		getProductList,
		getBatchList,
		getHierarchyList,
		getHierarchyFifo
	} from '@/api/warehouseManagement'

	import {
		uniqueByProperty
	} from '@/utils/utils.js'


	export default {
		data() {
			return {
				dimension: 3,
				listData: [],
				seletedAll: false, //全选状态
				ids: [],
				assetType: []
			}
		},
		computed: {
			checkListLen() {
				console.log(
					'this.listData.filter(el => el.checked)---',
					this.listData.filter(el => el.checked)
				)
				return this.listData.filter(el => el?.checked).length
			},
		},
		methods: {
			HandlScanCode() {
				let _this = this
				let obj = {}
				
				// let kk = {
				// 	"inoutType": "in",
				// 	"inId": "1956166290880073730",
				// 	"packageNo": "55408270001",
				// 	"assetType": "1,9",
				// 	'batchNo': '827'
				// }

				// _this.getCodeProduct(kk)


				uni.scanCode({
					success: (res) => {
						res.result.split('&').forEach(
							(item) =>
							(obj[item.split('=')[0]] = decodeURIComponent(
								item.split('=')[1]
							))
						);
						console.log(obj);
						if (obj.inoutType === 'in') {
							_this.getCodeProduct(obj)
						}
					},
					complete: (res) => {}
				})

			},

			async getCodeProduct({
				packageNo,
				batchNo,
				assetType
			}) {

				let params = {
					pageNum: 1,
					size: -1
				}

				if (!packageNo) {
					params.batchNo = batchNo
				}
				params.packageNo = packageNo


				console.log(params);

				const res = await getPackingList(params)

				// 已经出库了，查不到数据
				if (!res.list.length) {
					return this.$refs.uToast.show({
						message: "该单已失效",
						duration: 1000
					})
				}

				let data = res.list
				if (!this.ids.length) {
					this.assetType = assetType.split(',')
					this.ids = data.map(item => item.id)
					this.listData = res.list.map(item => ({
						...item,
						checked: true
					}))
					return
				}

				// 已经有数据的时候，新扫描的数据的ids
				this.assetType = [...new Set(this.assetType.concat(assetType.split(',')))]
				let ids = data.map(item => item.id)
				let addIds = ids.filter(item => !this.ids.includes(item));
				this.ids = this.ids.concat(addIds)
				this.listData = this.listData.concat(data.filter(item => addIds.includes(item.id)).map(k => ({
					...k,
					checked: true
				})))

			},


			selectVal(e, val, index) {
				this.$set(this.listData[index], 'checked', !this.listData[index].checked)
				// this.listData[index].checked = !this.listData[index].checked
				this.seletedAll = !this.listData.some(item => !item.checked)
			},

			//全选按钮
			_seletedAll() {
				if (!this.seletedAll) {
					this.seletedAll = true
					this.listData.map(item => {
						this.$set(item, 'checked', true)
					})
				} else {
					this.seletedAll = false
					//this.checkListLen = 0;
					this.listData.map(item => {
						this.$set(item, 'checked', false)
					})
				}
			},
			//跳转回添加页面
			async jumpAdd() {
				let selectionList = this.listData.filter(item => item.checked)
				if (this.dimension == 1) {
					let boolen = selectionList.every(item => item.outboundNum > 0)
					if (!boolen) {
						uni.showToast({
							icon: 'none',
							title: '请输入出库数量',
							duration: 2000
						})
						return
					}
				}
				let data = null
				if (this.dimension != 1) {
					data = await getHierarchyList({
						ids: selectionList.map(item => item.id).join(','),
						type: this.dimension
					})
				} else {
					data = await getHierarchyFifo({
						type: this.dimension,
						builders: selectionList.map(item => {
							return {
								categoryId: item.categoryId,
								num: item.outboundNum
							}
						})
					})
				}
				console.log('data-------------------', data)
				uni.$emit('setSelectList', data)
				uni.$emit('setCategory', this.assetType)
				uni.navigateBack()
			},
		}


	}
</script>


<style lang="scss" scoped>
	.qr-container {
		height: 100vh;

		.main {
			position: relative;
			height: calc(100% - 88rpx - 100rpx);
			display: flex;
			flex-direction: column;
		}


		.wrapper {
			flex: 1;
			overflow: hidden;
			padding-bottom: 60rpx;
		}

		.listContent {
			height: 100% !important;

			.listBox {
				display: flex;
				// height: 180rpx;
				padding: 20rpx 0;
				border-bottom: 2rpx solid #e5e5e5;

				.listBox-sel {
					height: 90rpx;
					width: 80rpx;
					// line-height: 90rpx;
					text-align: center;

					checkbox {
						transform: scale(1.2);
					}
				}

				.listBox-con {
					width: 100%;
					// display: flex;
					// flex-wrap: wrap;
					// justify-content: space-between;
					align-items: center;
					padding: 0 18rpx 0 0;

					.round {
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						border-radius: 50%;
						background: $theme-color;
						color: #fff;
						text-align: center;
						font-size: 20rpx;
					}

					.listBox-top {
						width: 100%;
						display: flex;
						justify-content: space-between;
						padding-bottom: 10rpx;

						.listBox-name,
						.listBox-code {
							display: inline-block;
							font-size: $uni-font-size-sm;
							font-weight: bold;
						}
					}

					.listBox-bottom {
						width: 100%;
						display: flex;
						justify-content: space-between;
						font-size: $uni-font-size-sm;
						flex-wrap: wrap;

						>view {
							width: 50%;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}

						.input_view {
							display: flex;
							align-items: center;
							justify-content: center;

							.u-input {
								height: 36rpx;
								padding: 0 !important;
								margin-right: 10rpx;
								border: 1px solid #ddd;
							}
						}

						.w100 {
							width: 100%;
						}
					}
				}
			}

			.noDate {
				height: 100%;
			}
		}

		//底部按钮
		.footer {
			width: 100%;
			height: 100rpx;
			position: fixed;
			display: flex;
			justify-content: space-between;
			align-items: center;
			bottom: 0;
			border-top: 1rpx solid #eeecec;
			background-color: #ffffff;
			z-index: 999;

			.bottom {
				margin-left: 10rpx;
			}

			.u-reset-button {
				position: absolute;
				right: 10rpx;
				top: 20rpx;
				width: 150rpx;
			}
		}

		.search_list {
			min-height: 100rpx;

			/deep/ .baseForm {
				padding: 0 20rpx;
			}
		}

		.operate_box {
			padding: 10rpx 32rpx;

			/deep/ .u-button {
				width: 40%;
			}
		}
	}
</style>