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

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


					<paramBom v-if='item.paramDetailList.length != 0' :list='item.paramDetailList'></paramBom>


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


					<productsBom v-if="item.product != null && item.product != '{}'" :productsObj="item.product">
					</productsBom>

					<modelBom v-if='item.modelList.length != 0' :workOrderId='item.workOrderId' :list='item.modelList'
						@scanIt='scanIt'>
					</modelBom>

					<instanceBom v-if='item.instanceList.length != 0' :workOrderId='item.workOrderId'
						:list='item.instanceList' :equipmentList="item.equipmentList"
						:currentTaskDiagram="item.currentTaskDiagram"></instanceBom>


					<semiProductBom v-if='item.semiProductList.length != 0' :workOrderId='item.workOrderId'
						:list='item.semiProductList' :equipmentList="item.equipmentList"
						:currentTaskDiagram="item.currentTaskDiagram"></semiProductBom>



					<turnoverBom v-if='item.turnover.length != 0' :list='item.turnover' :wordItem='item' pattern='feed'
						@handleScan='handleScan' @handleDel='handleDel'></turnoverBom>

					<aridRegion v-if='item.aridRegionList.length != 0' :list='item.aridRegionList'
						@handleScan='handleScan'></aridRegion>


					<palletBom v-if='item.palletList.length != 0' :palletList='item.palletList'></palletBom>

					<revolvingDiskBom v-if="item.revolvingDiskList.length != 0"
						:revolvingDiskList="item.revolvingDiskList"></revolvingDiskBom>

					<packingBom v-if='item.packingList.length != 0' :list='item.packingList'></packingBom>




					<view class='flex_btn' @click="openDetails(item.workOrderId)">出库单</view>



					<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>
					</view>
				</view>
			</u-list>

		</view>

		<view class="bottom-wrapper">
			<view class="btn_box" @click="save">一键报工</view>
		</view>



	</view>
</template>

<script>
	import workOrderBom from './components/workOrderBom.vue'
	import deviceBom from './components/deviceBom.vue'
	import modelBom from './components/modelBom.vue'
	import instanceBom from './components/instanceBom.vue'
	import semiProductBom from './components/semiProductBom'
	import aridRegion from './components/aridRegion'
	import paramBom from './components/paramBom.vue'
	import turnoverBom from '../jobBooking/components/turnoverBom.vue'
	import packingBom from './components/packingBom.vue'
	import productsBom from './components/productsBom.vue'
	import palletBom from './components/palletBom'
	import revolvingDiskBom from './components/revolvingDiskBom'


	import {
		workorderList,
		getByCode,
		scanLedger

	} from '@/api/pda/workOrder.js'

	import {
		batchSave
	} from '@/api/pda/feeding.js'



	export default {
		components: {
			workOrderBom,
			deviceBom,
			modelBom,
			instanceBom,
			semiProductBom,
			aridRegion,
			paramBom,
			turnoverBom,
			packingBom,
			productsBom,
			palletBom,
			revolvingDiskBom,

		},
		data() {
			return {
				title: '',
				idsList: [],
				List: [],
				taskId: null,

				clientEnvironmentId: null,




			}
		},
		onLoad(options) {
			this.title = options.taskName ? options.taskName + '-投料' : '投料'
			let queryArray = decodeURIComponent(options.arr);
			this.idsList = JSON.parse(queryArray);

			this.taskId = options.taskId
			this.taskName = options.taskName


			this.getList()

			this.clientEnvironmentId = uni.getStorageSync("userInfo") && uni.getStorageSync("userInfo").clientEnvironmentId


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

			uni.$on("setSelectList", (selectList, id) => {

				this.List.forEach(m => {
					if (m.workOrderId == id) {
						let modelList = [] // 模具
						let instanceList = [] // 投料
						let aridRegionList = [] // 干燥区
						let equipmentList = [] // 生产设备
						let palletList = [] // 舟皿
						let revolvingDiskList = [] // 周转盘


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

							}
							if (f.rootCategoryLevelId == 5) {
								modelList = modelList.concat(f)

							} else if (f.rootCategoryLevelId == 1) {
								instanceList = instanceList.concat(f)

							} else if (f.rootCategoryLevelId == 11) {
								aridRegionList = aridRegionList.concat(f)
							} else if (f.rootCategoryLevelId == 8) {
								palletList = palletList.concat(f)
							} else if (f.rootCategoryLevelId == 26) {
								revolvingDiskList = revolvingDiskList.concat(f)
							}



						})


						this.$set(m, 'modelList', modelList)
						this.$set(m, 'instanceList', instanceList)
						this.$set(m, 'aridRegionList', aridRegionList)
						this.$set(m, 'equipmentList', equipmentList)
						this.$set(m, 'palletList', palletList)
						this.$set(m, 'revolvingDiskList', revolvingDiskList)




					}
				})







			});

		},

		methods: {
			scrolltolower() {},

			save() {


				if (this.clientEnvironmentId == 3) {
					this.List.forEach(e => {
						if (e.equipmentList.length == 1) {
							e.instanceList.forEach(o => {
								if (o.rootCategoryLevelId == 1 && o.isConsumable == 0) {
									o['deviceName'] = e.equipmentList[0].name
									o['deviceId'] = e.equipmentList[0].id
								}
							})
						}
					})

				}

				this.List.forEach(f => {
					f.instanceList.forEach(e => {
						if (Object.prototype.hasOwnProperty.call(e, 'type')) {
							e.extInfo['type'] = e.type
						}
						e.extInfo['isConsumable'] = e.isConsumable
					})
				})





				batchSave(this.List).then(res => {

					uni.navigateTo({
						url: `/pages/pda/feeding/index/index?feedStatus=1`,
					});

				})
			},

			getList() {
				workorderList({
					ids: this.idsList,
					taskId: this.taskId
				}).then(res => {
					this.List = res.map(m => {

						m.workOrderId = m.id

						m.instanceList = [] // 物料
						m.equipmentList = [] // 设备
						m.modelList = [] // 模具
						m.aridRegionList = [] // 干燥区
						m.packingList = [] // 包装
						m.palletList = [] // 舟皿
						m.revolvingDiskList = [] // 周转盘
						m.semiProductList = [] //半成品

						if (m.pickOutInList.length > 0) {
							m.pickOutInList.forEach(f => {
								if (f.rootCategoryLevelId == 1) {
									m.instanceList.push(f)
								} else if (f.rootCategoryLevelId == 4) {
									m.modelList.push(f)
								} else if (f.rootCategoryLevelId == 5) {
									m.modelList.push(f)
								} else if (f.rootCategoryLevelId == 11) {
									m.aridRegionList.push(f)
								} else if (f.rootCategoryLevelId == 13) {
									m.packingList.push(f)
								} else if (f.rootCategoryLevelId == 8) {
									m.palletList.push(f)
								} else if (f.rootCategoryLevelId == 26) {
									m.revolvingDiskList.push(f)
								} else if (f.rootCategoryLevelId == 23) {
									m.semiProductList.push(f)
								}


							})
						}



						// 处理字段
						m.quality == '' ? m.quality = {} : ''


						if (!Object.prototype.hasOwnProperty.call(m, 'product')) {
							m.product = '{}'
						}

						console.log(m)




						delete m.id
						if (this.taskId) {
							m.taskId = this.taskId
							m.taskName = this.taskName
						}
						return {
							...m
						}
					})
				})
			},






			handleScan(id, type) {
				console.log(id)
				console.log(type)

				// this.scanData('SCJHGD20240117002', type, id)
				// return false

				let _this = this
				uni.scanCode({
					success: function(res) {
						_this.scanData(res.result, type, id)
					}
				})


			},

			scanData(result, type, id) {
				if (type == 'wordOrder') {
					let isFals = this.List.some(m => m.code == result)
					if (isFals) {
						uni.showToast({
							title: '工单已存在',
							icon: 'none'
						})
						return false
					}

					getByCode(result).then(res => {
						let _arr = this.List
						_arr.forEach((e, index) => {
							if (e.workOrderId == id && res) {
								_arr[index] = res
							}
						})

						this.List = _arr
						this.$forceUpdate()



					})
				}

			},
			scanIt(id) {
				console.log(id)

				let _this = this
				uni.scanCode({
					success: function(res) {
						_this.scanItData(res.result, id)
						console.log(res.result, id)
					}
				})
			},

			scanItData(result, id) {
				scanLedger(result).then(res => {
					console.log(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 = _arr[index].equipmentList.concat(res)
							}
						})
						this.List = _arr
						this.$forceUpdate()

					} else if (res.length >= 1 && res[0].rootCategoryLevelId == 5) { // 模具

						_arr = this.List
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								_arr[index].modelList = res
							}
						})
						this.List = _arr
						this.$forceUpdate()

					} else if (res.length >= 1 && ['1'].includes(res[0].rootCategoryLevelId)) { // 物料

						_arr = this.List
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								_arr[index].instanceList = _arr[index].instanceList.concat(res)
							}
						})
						this.List = _arr

						this.$forceUpdate()

					} else if (res.length >= 1 && res[0].rootCategoryLevelId == 11) { // 干燥区
						_arr = this.List
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								let isFals = _arr[index].aridRegionList.some(m => m.code == res[0]
									.aridRegionList[0].code)
								if (isFals) {
									uni.showToast({
										title: '干燥区已存在',
										icon: 'none'
									})
									return false
								}
								let _obj = res[0].aridRegionList[0]
								if (_obj.status == 0) {
									_obj['name'] = res[0].name
									_obj['region'] = res[0].extInfo.region
									_arr[index].aridRegionList = _arr[index].aridRegionList.concat(res[0]
										.aridRegionList)
								} else {
									uni.showToast({
										title: '干燥区已占用',
										icon: 'none'
									})
								}

							}
						})
						this.List = _arr

						this.$forceUpdate()
					} else if (res.length >= 1 && res[0].rootCategoryLevelId == 7) { // 周转车
						let isFals = _arr[index].turnover.some(m => m.code == res[0].code)
						if (isFals) {
							uni.showToast({
								title: '周转车已存在',
								icon: 'none'
							})
							return false
						}
					}


				})

			},

			// 全部扫一扫
			handlScanCode() {


				let _this = this
				uni.scanCode({
					success: function(res) {
						_this.scanItAllData(res.result)

					}
				})
			},


			scanItAllData(result) {
				scanLedger(result).then(res => {

					let _arr = []
					if (res.length == 1 && res[0].rootCategoryLevelId == 4) { // 设备
						_arr = this.List
						_arr.forEach((e, index) => {
							res['extInfo'].fixCode = res.fixCode
							e.equipmentList = e.equipmentList.concat(res)
						})
						this.List = _arr
						this.$forceUpdate()

					} else if (res.length >= 1 && res[0].rootCategoryLevelId == 5) { // 模具

						_arr = this.List
						_arr.forEach((e, index) => {
							e.modelList = res
						})
						this.List = _arr
						this.$forceUpdate()

					} else if (res.length >= 1 && ['1'].includes(res[0].rootCategoryLevelId)) {

						_arr = this.List
						_arr.forEach((e, index) => {
							e.instanceList = e.instanceList.concat(res)
						})
						this.List = _arr

						this.$forceUpdate()

					}
				})

			},

			handAdd(id) {
				const storageKey = Date.now() + "";
				uni.setStorageSync(storageKey, this.List || []);
				uni.navigateTo({
					url: `/pages/pda/workOrder/search/index?id=${id}&storageKey=${storageKey}&isType=feed&taskId=${this.taskId}`
				})
			},




			openDetails(id) {
				const storageKey = Date.now() + "";
				uni.setStorageSync(storageKey, this.List || []);
				let url = `/pages/pda/feeding/single?id=${id}&taskId=${this.taskId}&storageKey=${storageKey}`
				uni.navigateTo({
					url
				})
			},

		},

	}
</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;
		}



	}

	.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 80rpx;

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




	.flex_btn {
		position: fixed;

		right: 0;
		bottom: 160rpx;
		width: 140rpx;
		height: 66rpx;
		line-height: 66rpx;
		border-radius: 22rpx 0 0 22rpx;
		background: $theme-color;
		text-align: center;
		font-size: 22rpx;
		font-style: normal;
		font-weight: 400;
		color: #fff;
	}
</style>