<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">
					<workOrderBom :item='objData' @handleScan='handleWordScan'></workOrderBom>




					<qualityTurnoverBom v-if='objData.turnover && objData.turnover.length != 0' :list='objData.turnover'
						:wordItem='objData' :newTurnover='objData.newTurnover' @refreshList='getList'>
					</qualityTurnoverBom>




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

				</view>
			</u-list>

		</view>




	</view>
</template>

<script>
	import workOrderBom from '../../feeding/components/workOrderBom.vue'
	import qualityTurnoverBom from '../components/qualityTurnoverBom.vue'

	import {
		getVehicle,
		scanLedger
	} from '@/api/pda/workOrder.js'





	export default {
		components: {
			workOrderBom,
			qualityTurnoverBom
		},
		data() {
			return {
				title: '',
				workOrderId: null,
				objData: {},
				taskId: null


			}
		},

		onShow() {
			uni.$off("setSelectList");
			uni.$on("setSelectList", (selectList, id) => {
				let turnover = []
				selectList.forEach(f => {
					if (f.rootCategoryLevelId == 7) { // 周转车

						f.extInfo.positionList.length && f.extInfo.positionList.forEach(o => {
							o['check'] = false
							o['isend'] = false
						})

						turnover = turnover.concat(f)
					}
				})
				this.$set(this.objData, 'turnover', turnover)
				this.$forceUpdate()



			});
		},

		onLoad(options) {
			this.title = options.taskName ? options.taskName + '-更换周转车' : '更换周转车'
			this.workOrderId = options.workOrderId;
			this.taskId = options.taskId
			this.taskName = options.taskName

			this.getList()

		},


		methods: {
			scrolltolower() {},



			getList() {
				getVehicle({
					workOrderId: this.workOrderId,
					taskId: this.taskId
				}).then(res => {


					if (!res.hasOwnProperty('turnover')) {
						res['turnover'] = []
					}
					this.objData = res

				})
			},






			handleWordScan(id, type) {
				uni.showToast({
					icon: 'none',
					title: '不支持更换工单'
				})


			},


			scanIt(id) {
				console.log(id)
				let _this = this
				// 	_this.scanItData('w0300000002579001', id)
				// return false
				uni.scanCode({
					success: function(res) {
						_this.scanItData(res.result, id)
					}
				})
			},


			scanItData(result, id) {
				scanLedger(result).then(res => {
					let _arr = []
					if (res.length == 1 && res[0].rootCategoryLevelId == 7) { // 设备
						this.objData.turnover = res
						this.$forceUpdate()

					}

				})

			},



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







		},

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



	}


	.operate_box {
		padding: 10rpx 160rpx;

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