<template>
	<view>
		<view class="title_box rx-bc">
			<view class="name">取样</view>
		</view>


		<view class="material ">

			<view class="content_table">
				<view class="item">
					<view class="lable rx-cc">工序名称</view>
					<view class="content">
						{{item.diagramLast.taskTypeName}}
					</view>
				</view>




				<view class="item rx-sc">
					<view class="rx ww55 ">
						<view class="lable  rx-cc">数量</view>
						<view class="content rx-sc">
							<view>{{item.formedNum}}</view>
							<view class="unit">{{item.unit}}</view>
						</view>
					</view>

					<view class="rx ww45">
						<view class="lable rx-cc ww80">重量</view>
						<view class="content content_num">
							<view>{{item.formedWeight}}</view>
							<view class="unit">{{item.weightUnit}}</view>

						</view>
					</view>

				</view>

				<view class="item">
					<view class="lable rx-cc">取样数量</view>
					<view class="content content_num  rx-sc">

						<view style='width: 300rpx;'>{{ item.sampleNum }}</view>
						<view class="unit">{{item.unit}}</view>
						<view class="penalize" v-if='!isDetails' @click="penalize">处置

						</view>

					</view>
				</view>











			</view>

			<view class="content_table2" v-if="item.sampleList.length > 0">
				<view class="head row rx-sc">
					<view class="item ww15">货位</view>
					<view class="item ww30">类型</view>
					<view class="item ww45">处置</view>
					<view class="item ww15">操作</view>
				</view>

				<view class="table">

					<view v-for="(it, index) in item.sampleList" :key='index'>
						<view class="tr row rx-sc ">
							<view class="item ww15 content_num">
								{{ it.code }}
							</view>

							<view class="item ww30 content_num">
								<zxz-uni-data-select v-model="it.selectType" :localdata="rangeList"
									:disabled="isDetails" @change="hendleReuseNum"></zxz-uni-data-select>
							</view>

							<view class="item ww45  content_num">
								<zxz-uni-data-select :localdata="warehouseList" :disabled="isDetails"
									v-if='it.selectType == 2' v-model="it.warehouseId" dataValue='id' dataKey="name"
									filterable format='{name}'></zxz-uni-data-select>


							</view>
							<view class="item ww15">
								<view class="left rx-ss" @click="getDelete(index)">
									<uni-icons custom-prefix="iconfont" v-if="!isDetails" type="icon-shanchu" size="20"
										color="#fa3534"></uni-icons>
								</view>
							</view>
						</view>


						<view class="tr row  rx-sc wrap" style="margin-bottom: 30rpx;">

							<view class="item ww25 content_num cx-cc" v-for="(p, i) in it.paramList" :key="index + i">
								<view class="name">{{p.name}}</view>
								<input v-model="p.value" style="width: 168rpx ;"></input>
							</view>

						</view>

					</view>




				</view>


			</view>


		</view>



		<view class="title_box rx-bc" v-if="isDetails">
			<view class="name">报工</view>
		</view>


		<view class="material " v-if="isDetails">

			<view class="content_table">
				<view class="item">
					<view class="lable rx-cc">合格数量</view>
					<view class="content  content_num">
						<input class="uni-input" v-model="workReportInfo.formedNum" type='digit'></input>
					</view>
				</view>

			</view>

		</view>


		<u-popup :show="show" mode='center' v-if='show' :closeOnClickOverlay='false'>
			<view class="popup_box">
				<uni-data-select v-model="selectType" :localdata="rangeList"></uni-data-select>

				<view class="operate_box rx-sc">
					<u-button size="small" class="u-reset-button" @click="cancel">取消</u-button>
					<u-button size="small" class="u-reset-button" type="success" @click="save">确定</u-button>
				</view>
			</view>


		</u-popup>





	</view>
</template>

<script>
	import {
		getWarehouseList,
	} from '@/api/pda/workOrder.js'


	export default {
		components: {

		},
		props: {
			item: {
				type: Object,
				default: () => {}
			},

			workReportInfo: {
				type: Object,
				default: () => {}
			},

			isDetails: {
				type: Boolean,
				default: false
			},
			turnoverList: {
				type: Array,
				default: () => []
			},

			paramList: {
				type: Array,
				default: () => []
			}


		},


		watch: {

			turnoverList: {
				immediate: true,
				deep: true,
				handler(newVal) {

					this.turnoverCount = 0


					newVal.forEach(f => {
						if (f.extInfo && f.extInfo.positionList && f.extInfo.positionList.length > 0) {
							f.extInfo.positionList.forEach(o => {
								if (Object.prototype.hasOwnProperty.call(o, 'sampleNum')) {
									this.turnoverCount = this.turnoverCount + Number(o.sampleNum)
									this.$set(this.item, 'sampleNum', this.turnoverCount)
								}

							})
						}

						this.mergedArray = []
						if (f.extInfo && f.extInfo.positionList && f.extInfo.positionList.length > 0) {
							f.extInfo.positionList.forEach(obj => {
								if (Object.prototype.hasOwnProperty.call(obj, 'sampleNum') && obj
									.sampleNum > 1) {
									for (let i = 0; i < obj.sampleNum; i++) {
										// 创建对象的副本，以避免引用相同的对象
										const objCopy = {
											...obj,
											paramList: JSON.parse(JSON.stringify(this.paramList))
										};
										// 可以选择删除 num 属性，如果不需要在最终结果中保留它
										delete objCopy.sampleNum;
										this.mergedArray.push(objCopy);
									}
								} else if (obj.sampleNum == 1) {
									// 如果 sampleNum 等于 1，直接推入数组（可以选择删除 sampleNum 属性）
									const objCopy = {
										...obj,
										paramList: JSON.parse(JSON.stringify(this.paramList))
									};

									this.mergedArray.push(objCopy);
								}
								// 如果 num 小于 1，则不执行任何操作（根据需求调整）
							});
						}

						console.log(this.mergedArray)

					})


				}
			}
		},

		data() {
			return {
				warehouseList: [],

				show: false,
				selectType: null,
				rangeList: [{
						value: 1,
						text: "回用"
					},
					{
						value: 2,
						text: "入库"
					},
					{
						value: 3,
						text: "损耗"
					}
				],

				turnoverCount: 0,
				mergedArray: []
			}
		},
		created() {
			this.getWarehouseListFn()
		},
		methods: {
			getWarehouseListFn() {
				getWarehouseList().then(res => {
					this.warehouseList = res
				})
			},
			cancel() {
				this.selectType = null
				this.show = false

			},

			penalize() {


				if (Number(this.item.sampleNum <= 0)) {
					uni.showToast({
						icon: 'none',
						title: '取样数量要大于0, 请先选择周转车'
					})
					return false
				}
				this.item.sampleList = JSON.parse(JSON.stringify(this.mergedArray))
				this.$forceUpdate()


			},


			getDelete(idx) {
				this.item.sampleList.splice(idx, 1)
			},

			hendleReuseNum() {
				let count = 0
				if (this.item.sampleList.length > 0) {
					this.item.sampleList.forEach(f => {
						if (Object.prototype.hasOwnProperty.call(f, 'selectType') && f.selectType == 1) {
							count = count + 1
						}

					})
				}
				if (count > 0) {
					this.$set(this.item, 'reuseNum', count)
					this.$emit('showReuseTurnover', true)
					this.$forceUpdate()

				}

			},



		}
	}
</script>

<style lang="scss" scoped>
	.title_box {
		margin-top: 28rpx;

		.name {
			font-size: 28rpx;
			font-style: normal;
			font-weight: 400;
			color: $theme-color;
			padding-left: 20rpx;

			position: relative;

			&:before {
				position: absolute;
				content: '';
				left: 0rpx;
				top: 0rpx;
				bottom: 0rpx;
				width: 4rpx;
				height: 28rpx;
				background: $theme-color;
				margin: auto;
			}


		}

		.btn_box {
			padding: 0 18rpx;
			height: 60rpx;
			background: $theme-color;
			font-size: 26rpx;
			font-style: normal;
			font-weight: 400;
			font-size: 24rpx;
			color: #fff;
			border-radius: 4rpx;

			.scan {
				width: 34rpx;
				height: 34rpx;
				margin-right: 12rpx;

			}

		}

	}


	.material {
		margin-top: 10rpx;



		.content_table {
			width: 100%;
			border: 2rpx solid $border-color;

			.item {
				display: flex;
				border-bottom: 2rpx solid $border-color;


				.lable {
					width: 156rpx;
					text-align: center;
					background-color: #F7F9FA;
					font-size: 26rpx;
					border-right: 2rpx solid $border-color;
					flex-shrink: 0;
				}

				.lable150 {
					width: 156rpx !important;
					font-size: 24rpx;
				}

				.ww80 {
					width: 80rpx;
				}

				.content {
					width: 518rpx;
					min-height: 64rpx;
					font-size: 28rpx;
					line-height: 28rpx;
					font-style: normal;
					font-weight: 400;
					padding: 18rpx 8rpx;
					box-sizing: border-box;
					word-wrap: break-word;
					flex-grow: 1 !important;


					.unit {
						padding: 0 4rpx;
						font-size: 24rpx;
						color: #404446;
					}

					.penalize {
						width: 200rpx;
						line-height: 60rpx;
						background: $theme-color;
						font-size: 24rpx;
						text-align: center;
						color: #fff;
					}

				}


				.pd4 {
					padding: 4rpx 8rpx;
				}



				&:last-child {
					border-bottom: none;
				}
			}

			.ww55 {
				width: 55%;
			}

			.ww45 {
				width: 45%;
			}
		}


		.content_table2 {
			width: 100%;
			margin-top: 8rpx;

			.wrap {
				flex-wrap: wrap;
			}

			.row {
				width: 100%;


				.item {
					padding: 8rpx 0;
					color: #404446;
					font-size: 28rpx;
					padding-left: 12rpx;

				}

				.left {
					width: 40rpx;
				}

				.ww25 {
					width: 25%;
				}

				.ww30 {
					width: 30%;
				}

				.ww20 {
					width: 20%;
				}

				.ww35 {
					width: 35%;
				}

				.ww45 {
					width: 45%;
				}


				.ww55 {
					width: 55%;
				}

				.ww15 {
					width: 15%;
				}

				.turnover_btn {
					color: $theme-color;
					font-size: 24rpx;
					padding-left: 12rpx;
				}
			}

			.head {
				height: 64rpx;
				background: #F7F9FA;
				border-top: 2rpx solid #E3E5E5;
				border-left: 2rpx solid #E3E5E5;

				.item {
					height: 64rpx;
					line-height: 64rpx;
					border-right: 2rpx solid #E3E5E5;
					box-sizing: border-box;
				}
			}





			.tr {
				border-top: 2rpx solid #E3E5E5;
				border-left: 2rpx solid #E3E5E5;


				.item {
					font-size: 24rpx;
					min-height: 78rpx;
					display: flex;
					align-items: center;
					border-right: 2rpx solid #E3E5E5;
					box-sizing: border-box;
					white-space: normal;
					word-break: break-all;

				}

				&:last-child {
					border-bottom: 2rpx solid #E3E5E5;


				}
			}
		}
	}


	.content_num {
		display: flex;
		align-items: center;
		padding: 0 4rpx;
		box-sizing: border-box;

		/deep/ .uni-input-input {
			border: 2rpx solid #F0F8F2;
			min-width: 100rpx;
			background: #F0F8F2;
			color: $theme-color;
			box-sizing: border-box;
		}


	}
</style>