<template>
	<view class="content-box">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" :title="title" background-color="#F7F9FA"
			color="#000" @clickLeft="back">
		</uni-nav-bar>

		<view class="list_box">
			<u-list @scrolltolower="scrolltolower">
				<view class="card_box" v-for="(item, index) in List" :key="index">
					<workOrderBom :item="item" v-if="item" @handleScan="handleWordScan"></workOrderBom>

					<deviceBom v-if="item.equipmentList.length != 0" :workOrderId="item.workOrderId"
						:list="item.equipmentList" @scanIt="scanIt"></deviceBom>

					<turnoverBom v-if="item.turnover.length != 0" :list="item.turnover" :wordItem="item" pattern="job">
					</turnoverBom>

					<view class="operate_box rx-sc">
						<u-button size="small" class="u-reset-button" type="success"
							@click="handAdd(item.workOrderId)">手动添加</u-button>
						<u-button size="small" class="u-reset-button" type="success"
							@click="scanIt(item.workOrderId)">扫一扫</u-button>

						<u-button size="small" class="u-reset-button" type="success"
							@click="handZZCQuality">周转车质检</u-button>
						<u-button size="small" class="u-reset-button" type="success" @click="handQuality">质检</u-button>
					</view>



					<diagramLast v-if="item && Object.prototype.hasOwnProperty.call(item, 'lastObj') &&
						item.workOrderType !=2" :item="item.lastObj"></diagramLast>

					<inspectionBom
						v-if='Object.prototype.hasOwnProperty.call(item, "semiProductList") && item.semiProductList'
						:semiProductList='item.semiProductList'></inspectionBom>
				</view>
			</u-list>
		</view>


		<SearchPopup mode="center" v-if="qualityShow">
			<template v-slot:list>
				<view class="popup_list">

					<u-form labelPosition="left" :model="qualityForm" labelWidth="180" labelAlign="left" class="">
						<u-form-item label="质检取样数:" borderBottom prop="num">
							<input class="uni-input" v-model="qualityForm.num" placeholder="请输入质检取样数"></input>
						</u-form-item>

					</u-form>
				</view>
			</template>

			<template v-slot:operate>
				<view class="operate_box rx-bc">
					<u-button size="small" class="u-reset-button" @click="cancel">
						取消
					</u-button>


					<u-button type="success" size="small" class="u-reset-button" @click="qualityOk">
						确定
					</u-button>

				</view>
			</template>

		</SearchPopup>





		<view class="bottom-wrapper">
			<view class="btn_box" @click="save">确认</view>
		</view>


	</view>
</template>

<script>
	import {
		workorderList,
		scanLedger,
		qualityQy
	} from "@/api/pda/workOrder.js";
	import {
		batchSave
	} from "@/api/pda/feeding.js";

	import workOrderBom from "../../feeding/components/workOrderBom.vue";
	import inspectionBom from "../components/inspectionBom.vue";
	import deviceBom from "../../feeding/components/deviceBom.vue";
	import diagramLast from "../components/diagramLast.vue";

	import turnoverBom from "../../sample/components/turnoverBom.vue";

	import SearchPopup from '../../components/searchPopup.vue'


	export default {
		components: {
			workOrderBom,
			inspectionBom,
			deviceBom,
			diagramLast,
			turnoverBom,
			SearchPopup
		},
		data() {
			return {
				title: null,
				id: null,
				taskId: null,

				List: [],
				qualityShow: false,

				qualityForm: {
					num: null,
				}

			};
		},

		onLoad(options) {
			this.title = options.taskName || '质检'
			this.id = options.workOrderId;
			this.taskId = options.taskId;
			this.getList();


		},

		onShow() {
			uni.$off("setSelectList");

			uni.$on("setSelectList", (selectList, id) => {
				this.List.forEach((m) => {
					if (m.workOrderId == id) {
						let equipmentList = []; // 生产设备
						let turnover = [];

						selectList.forEach((f) => {
							if (f.rootCategoryLevelId == 4) {
								equipmentList = equipmentList.concat(f);
							}

							if (f.rootCategoryLevelId == 7) {
								turnover = turnover.concat(f);
							}
						});

						this.$set(m, "equipmentList", equipmentList);
						this.$set(m, "turnover", turnover);
					}
				});
			});
		},

		methods: {
			getList() {
				workorderList({
					ids: [this.id],
					taskId: this.taskId,
				}).then((res) => {
					this.List = res.map((m) => {
						m.workOrderId = m.id;

						m.equipmentList = []; // 设备
						m.turnover = []; // 周转车


						m.lastObj = {
							formedNum: m.formedNumLast,
							taskNameLast: m.taskNameLast,
							unit: m.unit,
						};
						m.feedType = 3;

						delete m.id;

						if (this.taskId) {
							m.taskId = this.taskId;
						}

						return {
							...m,
						};
					});
				});
			},




			scrolltolower() {},
			handleWordScan() {
				uni.showToast({
					icon: "none",
					title: "不支持换工单",
				});
			},

			scanIt(id) {
				uni.scanCode({
					success: (res) => {
						this.scanItData(res.result, id);
					},
				});
			},

			scanItData(result, id) {
				scanLedger(result).then((res) => {
					let _arr = [];
					if (res.length == 1 && res[0].rootCategoryLevelId == 4) {
						// 设备
						_arr = this.List;
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								_arr[index].equipmentList = res;
							}
						});
						this.List = _arr;
						this.$forceUpdate();
					}

					if (res.length == 1 && res[0].rootCategoryLevelId == 7) {
						// 周转车
						_arr = this.List;
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								_arr[index].turnover = res;
							}
						});
						this.List = _arr;
						this.$forceUpdate();
					}
				});
			},
			handZZCQuality() {

				if (this.List[0].turnover.length == 0) {
					uni.showToast({
						icon: "none",
						title: "请先添加周转车取样",
					});
					return false;
				}

				let _count = 0;
				this.List[0].turnover &&
					this.List[0].turnover.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") && o.sampleNum >= 1) {
									_count = _count + Number(o.sampleNum);

								}
							});
						}
					});

				if (_count < 1) {
					uni.showToast({
						icon: "none",
						title: "请先取样",
					});
					return false;
				}

				let param = {
					taskId: this.taskId,
					workOrderId: this.id,
					quantity: _count
				}

				qualityQy(param).then(res => {
					this.$set(this.List[0], 'semiProductList', res)
					this.$forceUpdate()
				})





			},

			handQuality() {
				if (Object.prototype.hasOwnProperty.call(this.List[0], 'lastObj') && this.List[0].lastObj.formedNum >
					0) {} else {
					uni.showToast({
						icon: "none",
						title: "暂无质检合格数",
					});
					return false
				}
				this.qualityShow = true
			},

			cancel() {
				this.qualityShow = false
			},

			qualityOk() {
		

				if (!this.qualityForm.num && this.qualityForm.num < 1) {
					uni.showToast({
						icon: "none",
						title: "请先输入质检取样数",
					});
					return false
				}
				let param = {
					taskId: this.taskId,
					workOrderId: this.id,
					quantity: this.qualityForm.num
				}

				qualityQy(param).then(res => {
					this.$set(this.List[0], 'semiProductList', res)
					this.qualityShow = false
					this.$forceUpdate()
				})
			},

			save() {
				batchSave(this.List).then((res) => {
					uni.navigateBack();
				});
			},

			handAdd(id) {
				console.log(id);

				const storageKey = Date.now() + "";
				uni.setStorageSync(storageKey, this.List || []);
				uni.navigateTo({
					url: `/pages/pda/workOrder/search/index?id=${id}&storageKey=${storageKey}&isType=zdy&taskId=${this.taskId}&classIds=[4,7]`,
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.content-box {
		height: 100vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
	}

	.list_box {
		flex: 1;
		overflow: hidden;
		padding: 4rpx 0;

		.u-list {
			height: 100% !important;
		}

		.card_box {
			padding: 16rpx 24rpx;
		}
	}

	.bottom-wrapper {
		.btn_box {
			width: 750rpx;
			height: 88rpx;
			line-height: 88rpx;
			background: $theme-color;
			text-align: center;
			font-size: 36rpx;
			font-style: normal;
			font-weight: 400;
			color: #fff;
		}
	}

	.operate_box {
		padding: 10rpx 20rpx;

		/deep/ .u-button {
			width: 160rpx;
		}
	}


	.popup_list {
		width: 78vw;
		min-height: 120rpx;
		padding: 0 32rpx;

	}
</style>