<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>

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

					<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'></instanceBom>


					<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 {
		workorderList,
		getByCode,
		scanLedger
	} from '@/api/pda/workOrder.js'

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



	export default {
		components: {
			workOrderBom,
			deviceBom,
			modelBom,
			instanceBom
		},
		data() {
			return {
				title: '',
				idsList: [],
				List: [],


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


			this.getList()

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

			uni.$on("setSelectList", (selectList, id) => {
				console.log(selectList)
				this.List.forEach(m => {
					if (m.workOrderId == id) {
						this.$set(m, 'instanceList', selectList)
					}
				})




			});

		},

		methods: {
			scrolltolower() {},

			save() {
				let _arr = []
				_arr = this.List.map(m => {

					return {
						...m
					}

				})


				console.log(_arr)

				batchSave(_arr).then(res => {

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

				})
			},

			getList() {
				workorderList(this.idsList).then(res => {
					this.List = res.map(m => {
						m.workOrderId = m.id
						m.equipmentList = [] // 设备
						m.modelList = [] // 模具
						m.instanceList = [] // 投料
						delete m.id
						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)

				// CX-EQ-YLSJL-008	  设备
				// M001 M002 模具 
				//  S310000552731 物料
				// this.scanItData('CX-EQ-YLSJL-008', id)
				// return false

				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 => {

					let _arr = []
					if (res.length == 1 && res[0].rootCategoryLevelId == 4) { // 设备
						_arr = this.List
						_arr.forEach((e, index) => {
							if (e.workOrderId == id) {
								 console.log(res.fixCode)
								_arr[index].equipmentList = 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()

					}

				})

			},

			// 全部扫一扫
			handlScanCode() {


				// CX-EQ-YLSJL-008	  设备
				// M001 M002 模具 
				//  S310000552731 物料
				// this.scanItAllData('M001')
				// return false

				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 = 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, list) {
				const storageKey = Date.now() + "";
				uni.setStorageSync(storageKey, list || []);
				uni.navigateTo({
					url: `/pages/pda/workOrder/search/index?id=${id}&storageKey=${storageKey}&isType=feed`
				})
			},

		},

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

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