<template>
	<view class="">
		<uni-popup ref="popup" :maskClick="false">
			<view class="uni-flex uni-column pop_customer">
				<view class="" style="padding:10rpx">
					<view class="uni-flex u-col-center uni-row space-between" style="padding: 10rpx 10rpx 20rpx 10rpx">
						<view class="" style="font-size: 35rpx;">
							{{title}}
						</view>
						<image style="width: 35rpx;height: 35rpx;" src="/static/icons/icons_close.svg"
							@click="closeRequestPopup()"></image>
					</view>
					<u-line />
					<view class="uni-flex uni-column" style="background-color: white; ">
						<view class="uni-flex uni-column  ">
							<view class="uni-flex uni-row padding title u-col-center">
								<text>位置 : </text>
								<view class="uni-flex u-col-center uni-row" @click="showSelect">
									<!-- <input class="uni-input" v-model="toLocationCode" placeholder="请输入需求库位"
										:disabled="locationDisable" :focus="locationFocus" @confirm="locationConfirm"
										@blur='locationFocus = false' /> -->
									<view class="">
										{{positionInfo}}
									</view>
									<u-select v-model="show" mode="mutil-column-auto" :list="list"
										@confirm="confirmSelect"></u-select>

								</view>

							</view>
							<u-line />

							<view class="uni-flex uni-row padding title u-col-center">
								<text>零件 : </text>
								<view class="uni-flex u-col-center uni-row">
									<input class="" v-model="itemCode" placeholder="请输入需求零件"
										:focus="itemCodeFocus" @blur='itemCodeFocus = false'
										@confirm="itemCodeConfirm" />
								</view>
							</view>
							<u-line />

							<view class="uni-flex uni-row  padding title u-col-center">
								<text>数量 : </text>
								<view class="uni-flex uni-row uni-center" style="align-items: center;">
									<uni-number-box :value="counQty" @change="change" style='margin-left: 10rpx;'
										:focus="numberFocus" @blur='numberFocus = false'>
									</uni-number-box>
									<uom :uom="uom"></uom>

								</view>
							</view>
							<u-line />
						</view>
					</view>
				</view>
				<view class="uni-flex uni-row hide_border">
					<button class="btn_edit_big_cancle" hover-class="btn_edit_big_after" @click="cancel()">取消</button>
					<button class="btn_edit_big_confirm" hover-class="btn_edit_big_after" @click="confirm()">确认</button>
				</view>
			</view>
		</uni-popup>
		<comMessage ref="comMessage"></comMessage>
	</view>
</template>

<script>
	import {
		getBasicLocationByCode,
		getBasicItemByCode
	} from '@/api/request2.js';
	import {
		getLocationTypeName,
		getListLocationTypeDesc,
		checkDirectoryItemExist
	} from '@/common/directory.js';
	import uom from '@/mycomponents/qty/uom.vue'
	import balanceStatus from '@/mycomponents/status/balanceStatus.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	export default {
		components: {
			uom,
			balanceStatus,
			comMessage
		},
		data() {
			return {
				// toLocationCode: 'SFG',
				// itemCode: 'CE115F11161AG',
				toLocationCode: '',
				itemCode: '',
				qty: 0,
				locationFocus: true,
				itemCodeFocus: false,
				locationDisable: false,
				requestInfo: null,
				locationTypeList: [],
				isCheckLocation: false,
				isCheckItemCode: false,
				counQty: 0,
				numberFocus: false,
				uom: "",
				positionInfo: "请选择位置",
				show: false,
				list: [{
						value: 1,
						label: '车间1',
						children: [{
								value: 2,
								label: '生产线1',
								children: [{
										value: 3,
										label: '工位1'
									},
									{
										value: 4,
										label: '工位2'
									}
								]
							},
							{
								value: 5,
								label: '生产线2',
								children: [{
										value: 6,
										label: '工位1'
									},
									{
										value: 7,
										label: '工位2'
									}
								]
							}
						]
					},
					{
						value: 8,
						label: '车间2',
						children: [{
								value: 9,
								label: '生产线1',
								children: [{
										value: 10,
										label: '工位1'
									},
									{
										value: 10,
										label: '工位2'
									}
								]
							}, {
								value: 9,
								label: '生产线2',
								children: [{
										value: 10,
										label: '工位1'
									},
									{
										value: 10,
										label: '工位2'
									}
							]
							},


						]
					}
				]
			}
		},
		props: {
			title: {
				type: String,
				default: '需求信息'
			},
		},
		methods: {
			openRequestPopup(requestInfo) {
				if (requestInfo != undefined) {
					this.requestInfo = requestInfo;
					this.toLocationCode = requestInfo.toLocationCode;
					// this.itemCode = requestInfo.Items[0].itemCode;
					// this.qty = requestInfo.Items[0].qty;
					this.itemCode = '';
					this.qty = 0;
					this.locationDisable = true;
					this.itemCodeGetFocus();
				} else {
					this.toLocationCode = ''
					this.itemCode = ''
					this.qty = 0
					this.locationFocus = true
					this.itemCodeFocus = false
					this.locationDisable = false
					this.requestInfo = null
					this.locationTypeList = []
					this.isCheckLocation = false
					this.isCheckItemCode = false
					this.counQty = 0
					this.uom = ""
				}
				this.$refs.popup.open('bottom')
			},
			closeRequestPopup() {
				this.$refs.popup.close()
			},
			locationConfirm() {
				//查询库位信息
				this.checkLocatioCode();

			},
			itemCodeGetFocus() {
				this.itemCodeFocus = true;
			},
			itemCodeLoseFocus() {
				this.itemCodeFocus = false;
			},
			locationCodeGetFocus() {
				this.locationFocus = true;
			},
			locationCodeLoseFocus() {
				this.locationFocus = false;
			},

			confirm() {
				if (this.requestInfo != null) {
					let item = this.requestInfo.Items.find(r => r.itemCode == this.itemCode)
					if (item != undefined) {
						if (this.requestInfo.qty == item.qty) {
							this.$refs.comMessage.showMessage('零件【' + this.itemCode + '】的需求信息已存在, 请重新输入零件及数量',
								res => {
									this.itemCode = '';
									this.qty = 0;
									this.uom = ""
									this.itemCodeGetFocus();
								});
						} else {
							this.$refs.comMessage.showQuestionMessage('已经有零件【' + this.itemCode + '】的需求信息, 是否要修改零件的需求数量?',
								res => {
									if (res) {
										this.callback('update');
									}
								});
						}
					}
				} else {
					// if (this.toLocationCode == "" || !this.isCheckLocation) {
					// 	this.showErrorMessage("请输入库位", "location")
					// 	return
					// }
					if (this.itemCode == "" || !this.isCheckItemCode) {
						this.showErrorMessage("请输入零件", "itemCode")
						return
					}
					if (this.qty == 0) {
						this.showErrorMessage("数量必须大于0")
						return
					}

					this.callback('add');

				}
			},

			checkLocatioCode() {
				getBasicLocationByCode(this.toLocationCode).then(res => {
					if (res.data.total > 0) {
						let result = res.data.list[0];
						var type = result.Type;
						var available = result.available;
						if (available == 1) {
							if (checkDirectoryItemExist(this.locationTypeList, type)) {
								this.toLocationCode = result.Code;
								this.isCheckLocation = true
								this.itemCodeGetFocus();
							} else {
								var hint = getListLocationTypeDesc(this.locationTypeList);
								this.showErrorMessage("扫描库位[" + this.toLocationCode + "]是[" +
									getLocationTypeName(type) + "],需要的库位类型是[" + hint + "]", "location")
							}
						} else {
							this.showErrorMessage("扫描库位[" + this.toLocationCode + "]不可用", "location")
						}
					} else {
						this.showErrorMessage('未查询到库位[' + this.toLocationCode + ']', "location")

					}
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading();
					this.showErrorMessage(error, "location")
				})
			},

			checkItemCode() {
				//校验零件
				getBasicItemByCode(this.itemCode).then(res => {
					uni.hideLoading();
					if(res.data==null){
						this.showErrorMessage('未查找到零件【' + this.itemCode + '】', "itemCode");
					}else {
						if(res.data.list.length>0){
							this.itemCode = res.data.list[0].code;
							this.isCheckItemCode = true;
							this.numberFocus = true
							this.uom = res.data.list[0].uom
						}else {
							this.showErrorMessage('未查找到零件【' + this.itemCode + '】', "itemCode");
						}
					}
					

				}).catch(error => {
					uni.hideLoading();
					this.showErrorMessage(error, "itemCode");
				})
			},


			callback(action) {
				let item = {
					toLocationCode: this.toLocationCode,
					itemCode: this.itemCode,
					qty: this.qty
				};
				this.closeRequestPopup();
				this.$emit("confirm", action, item);
			},

			cancel() {
				this.closeRequestPopup();
			},
			showErrorMessage(message, type) {
				setTimeout(r => {
					this.$refs.comMessage.showErrorMessage(message, res => {
						if (type == "location") {
							this.locationCodeGetFocus();
						} else if (type == "itemCode") {
							this.itemCodeGetFocus();
						} else {
							this.numberFocus = true;
						}
					})
					if (type == "location") {
						this.toLocationCode = ""
						this.isCheckLocation = false
					} else if (type == "itemCode") {
						this.itemCode = ""
						this.isCheckItemCode = false;
					}
				})
			},
			itemCodeConfirm() {
				this.checkItemCode();
			},
			change(value) {
				this.qty = value;
			},
			cancel(e) {
				console.log("取消", e)
			},
			showSelect() {
				this.show = true
			},
			confirmSelect(e) {
				this.positionInfo = e[0].label + "-" + e[1].label + "-" + e[2].label
				console.log(e);
			}


		}
	}
</script>

<style lang="scss">
	.title {
		font-size: 30rpx;
	}
</style>
