<template>
	<view class="page-wraper">
		<view class="" v-if='detailSource.subList.length==0'>
			<com-blank-view @goScan='goScan(true)'></com-blank-view>
		</view>
		<view v-else class="page-wraper">
			<view class="page-main">
				<comDeliverRequestCreator ref="deliverRequest" :dataContent="detailSource">
				</comDeliverRequestCreator>
				<button class="btn_add" @click="goScan(false)">+去添加</button>
			</view>

			<div class="btn_bottom">
				<view class="" style="display: flex;flex-direction: row;">
					<view class="">
						<button class="btn_commit" hover-class="btn_commit_after" @click="submit()">提交</button>
					</view>
				</view>
			</div>
			<comMessage ref="comMessage"></comMessage>
		</view>
	</view>
	<comDeliverRequestPopup ref="comDeliverRequestPopup" @confirm='requestConfirm'></comDeliverRequestPopup>
</template>

<script>
	import {
		deliverRequestSubmit,
		issueRecordSubmit
	} from '@/api/request2.js';

	import {
		goHome,
		updateTitle,
		getRemoveOption,
		getISODateTime
	} from '@/common/basic.js';

	import {
		getDataSource
	} from '@/pages/issue/js/issue.js';

	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import comDeliverRequestPopup from '@/pages/deliver/coms/comDeliverRequestPopup.vue'
	import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comDeliverRequestCreator from '@/pages/deliver/coms/comDeliverRequestCreator.vue'

	export default {
		name: 'issueRequestSubmit',
		components: {
			comBlankView,
			comDeliverRequestPopup,
			jobDetailPopup,
			comMessage,
			comDeliverRequestCreator
		},
		data() {
			return {
				subList: [], //接口返回的任务subList
				detailSource: {
					subList: []
				}, //绑定在页面上的数据源
				detailOptions: [],
				scanOptions: [],
				fromType: "",
				dataContent: {}

			}
		},
		mounted() {
			this.goScan(true)
		},
		onLoad(option) {
			this.fromType = option.fromType

		},
		methods: {
			goScan(editPosition) {
				this.$refs.comDeliverRequestPopup.openRequestPopup(editPosition);
			},

			//确定需求信息
			requestConfirm(action, item) {

				if (this.detailSource.subList.length == 0) {
					this.detailSource = {
						customerCode: item.customerCode,
						customerName: item.customerName,
						totalQty: 0,
						subList: []
					}
					var subItem = {
						itemCode: item.itemCode,
						itemName: item.itemName,
						qty: item.qty,
						uom: item.uom
					}
					this.detailSource.subList.push(subItem)
				} else {
					var result = this.detailSource.subList.filter(res => {
						if (res.itemCode == item.itemCode &&
							res.productionLineCode == item.productionLineCode &&
							res.workStationCode == item.workStationCode) {
							return res
						}
					})

					//没有添加数据
					if (result.length == 0) {
						var subItem = {
							productionLineCode: item.productionLineCode,
							productionLineName: item.productionLineName,
							workStationCode: item.workStationCode,
							workStationName: item.workStationName, //工位
							itemCode: item.itemCode,
							itemName: item.itemName,
							qty: item.qty,
							uom: item.uom
						}

						this.detailSource.subList.push(subItem)

					} else {
						//有了更新数据
						result[0].qty += item.qty

					}
				}
				this.caclcQty();
				if (this.$refs.deliverRequest != undefined) {
					this.$refs.deliverRequest.update()
				}
			},

			caclcQty() {
				var totalQty = 0;
				this.detailSource.subList.forEach(res => {
					totalQty += res.qty
				})
				this.detailSource.totalQty = totalQty;
			},


			setParams() {
				return this.detailSource
			},
			submit() {
				uni.showLoading({
					title: "提交中....",
					mask: true
				});

				var params = this.setRequestParams()
				console.log("提交参数", JSON.stringify(params));
				// deliverRequestSubmit(params).then(res => {
				// 	uni.hideLoading()
				// 	if (res.data) {
				// 		this.showCommitSuccessMessage("提交成功<br>生成成品发货申请" + res.data)
				// 	} else {
				// 		this.showErrorMessage("提交失败[" + res.msg + "]")
				// 	}
				// }).catch(error => {
				// 	uni.hideLoading()
				// 	this.showErrorMessage(error)
				// })


			},

			setRequestParams() {
				var subList = []
				this.detailSource.subList.forEach(detail => {
					detail.soNumber = detail.package.soNumber;
					detail.soLine = detail.package.soLine;
					detail.remark = ""
					// soNumber: 926926926,
					// soLine: 926,
					// itemCode: THILDD0A691AG,
					// remark: ,
					// qty: 2,
					// uom: EA
					subList.push(detail)
				})

				this.detailSource.subList = subList
				this.detailSource.deliverPlanNumber = "DP20231130-0001"
				this.detailSource.customerCode = this.detailSource.customerCode
				this.detailSource.customerDockCode = ""
				this.detailSource.carrierCode = ""
				this.detailSource.transferMode = ""
				this.detailSource.vehiclePlateNumber = ""
				this.detailSource.remark = ""
				this.detailSource.dueTime = ""
				this.detailSource.departmentCode = "研发部门"
				this.detailSource.status = 1
				this.detailSource.autoCommit = "FALSE"
				this.detailSource.autoAgree = "FALSE"
				this.detailSource.autoExecute = "FALSE"
				this.detailSource.directCreateRecord = "FALSE"

				return this.detailSource;
			},
			showCommitSuccessMessage(hint) {
				this.$refs.comMessage.showSuccessMessage(hint, res => {
					uni.navigateTo({
						url: './deliverRequest'
					})
				})
			},
			showErrorMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {}
				});
			},
		}
	}
</script>

<style>
</style>