<template>
	<view class="content-box">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" 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' v-if='objData' pType="job" @handleScan='handleScan'></workOrderBom>

					<jobBom v-if="clientEnvironmentId == 2" :item='objData' ref='jobRef'
						:notFormed='objData.notFormedList' pType="job" @penalize='penalize' @modeNum='modeNum'></jobBom>

					<view class="select_box">
						<zxz-uni-data-select :localdata="warehouseList" placeholder="请选择入库仓库" v-model="warehouseId"
							dataValue='id' dataKey="name" filterable format='{name}'></zxz-uni-data-select>
					</view>

					<view class="select_box" v-if="clientEnvironmentId !== 3">
						<zxz-uni-data-select :localdata="WarehouseSpecTypeList" placeholder="请选择入库规格"
							v-model="inWarehouseSpecType" dataValue='value' dataKey="text" filterable format='{text}'
							disabled></zxz-uni-data-select>
					</view>




					<!-- 打包信息 -->
					<view v-if="clientEnvironmentId == 3">

						
						<view class="list_box" v-for="(it, index) in tgList" :key="index">
							<view class="time">打包时间： {{ it.createTime }}</view>
							<packingTgBom :list='it.extInfo.pickOutInList' :isWarehousing='true' :item='it'
								:objData="objData"></packingTgBom>
						</view>
					</view>
					<view v-else>

						<view v-if="clientEnvironmentId != 3 && objData.singleReport !== 1">
							
							<view class="list_box" v-for="(item, index) in List" :key="index">
								<view class="time" style="margin-bottom: 10rpx;">打包时间： {{ item.createTime }}</view>
								<packingBom :objData="item.extInfo" :measuringUnit='item.measuringUnit'></packingBom>
							</view>
						</view>

						<view v-else>
							
							<view class="list_box" v-for="(it, index) in List" :key="index">
								<view class="time">打包时间： {{ it.createTime }}</view>
								<packingQtBom :list='it.extInfo.pickOutInList' :isWarehousing='true' :item='it'
									:objData="objData"></packingQtBom>
							</view>
						</view>
					</view>
				</view>
			</u-list>
		</view>



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

	</view>
</template>

<script>
import {
	getByIdReport,
	jobSave
} from '@/api/pda/jobBooking.js'

import {
	getWarehouseList,
	listPDAToWarehouse,
	listPDAToWarehouseTG,

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

import jobBom from './components/jobBom.vue'


import packingBom from './components/packingBom.vue'
import workOrderBom from '../feeding/components/workOrderBom.vue'
import packingTgBom from '../jobBooking/components/packingTgBom.vue'
import packingQtBom from '../jobBooking/components/packingQtBom.vue'

export default {
	components: {
		workOrderBom,
		packingBom,
		packingTgBom,
		packingQtBom,
		jobBom
	},
	data() {
		return {
			id: null,
			taskId: null,

			objData: {
				workReportInfo: {
					formedNum: ''
				}
			},
			warehouseList: [],
			warehouseId: null,
			delta: -1,

			List: [],

			tgList: [],
			inWarehouseSpecType: '2',

			WarehouseSpecTypeList: [
				{
					text: '按计量入库',
					value: '1'
				}, {
					text: '最小包装入库',
					value: '2'
				},
				{
					text: '内包装入库',
					value: '3'
				},
				{
					text: '外包装入库',
					value: '4'
				}
			],

			clientEnvironmentId: uni.getStorageSync("userInfo") && uni.getStorageSync("userInfo")
				.clientEnvironmentId, // *1 主环境-601环境   2 soll-索尔环境    3 tg-碳谷环境

			inWarehouseType: null,

		}
	},

	onLoad(options) {
		this.id = options.workOrderId
		this.taskId = options.taskId
		this.workReportId = options.workReportId
		this.delta = options.delta

		this.getList()

		this.getWarehouseListFn()

		if (this.clientEnvironmentId == 3) {
			this.getTgWarehouse()
		} else {
			this.getWarehouse()
		}

	},

	methods: {
		getList() {
			getByIdReport(this.id, this.taskId).then(res => {
				this.objData = res


				if (res.workOrderType == 1) {
					this.inWarehouseType = 1
				} else if (res.workOrderType == 2) {
					this.inWarehouseType = 8 //  "1入库类型1生产入库2半成品入库"
				}
				// workReportInfo.formedNum
				this.objData.notFormedList = [{
					notFormedNum: null,
					notFormedWeight: null,
					weightUnit: res.weightUnit,
					unit: res.unit,
					warehouseId: null, // 处置 仓库id
				}]


				this.objData.workReportInfo = {
					formingNum: null,
					formingWeight: null,
					formedNum: this.objData.product[0] && this.objData.product[0].feedQuantity || '',
					formedWeight: null,
					taskId: this.taskId

				}

				this.$set(this, 'objData', this.objData)
				console.log(this.objData, '222222222222222222222');
			})
		},

		getWarehouseListFn() {
			getWarehouseList().then(res => {
				this.warehouseList = res
			})
		},

		getWarehouse() {
			let param = {
				workOrderId: this.id,
				taskId: this.taskId,
				workReportId: this.workReportId

			}
			listPDAToWarehouse(param).then(res => {
				this.List = res
			})
		},


		getTgWarehouse() {
			let param = {
				workOrderId: this.id,
				taskId: this.taskId,
				workReportId: this.workReportId

			}
			listPDAToWarehouseTG(param).then(res => {
				this.tgList = res
			})
		},


		handleScan() {

		},
		scrolltolower() { },

		save() {

			if (!this.warehouseId) {
				uni.showToast({
					icon: 'none',
					title: '请先选择入库仓库'
				})
				return false
			}



			// if (this.clientEnvironmentId == 21) {
			// 	// /pda/mes/workreport/productReceiveCreate/{workId}
			// 	productReceiveCreate(this.objData.workOrderId).then(v=>{
			// 		uni.navigateBack();
			// 	})

			// } else {

			let categoryId = '', rootCategoryLevelId = '';

			if (this.objData.product.length) {
				categoryId = this.objData.product[0].categoryId;
				rootCategoryLevelId = this.objData.product[0].rootCategoryLevelId;
			}


			let storageInfo = {
				inWarehouseSpecType: this.clientEnvironmentId !== 3 || this.clientEnvironmentId !== 2 ? Number(this.inWarehouseSpecType) : '',
				warehouseId: this.warehouseId,
				inWarehouseType: this.inWarehouseType,
				toWarehouseList: this.clientEnvironmentId == 3 ? this.tgList : this.List,
			}


			if (this.clientEnvironmentId == 2) {
				let num = '';
				if (this.objData.workReportInfo && this.objData.workReportInfo?.formedNum) {
					num = this.objData.workReportInfo.formedNum;
				}

				storageInfo.packingReportList = [{
					code: this.objData.productCode,
					categoryId: categoryId,
					workOrderId: this.objData.workOrderId,
					rootCategoryLevelId: rootCategoryLevelId,
					totalQuantity: num,
					quantity: num,
					unit: this.objData.unit,
					packingNum: 1,
					taskId: -1
				}]
			}

			this.objData['storageInfo'] = storageInfo

			jobSave(this.objData).then(res => {
				uni.showToast({
					title: '入库申请成功',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack();
				}, 500)


			})

		},


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




.select_box {
	margin-top: 20rpx;
}

.list_box {
	margin-top: 40rpx;

	.time {
		font-size: 28rpx;
	}
}
</style>