<template>
	<view>

		<uni-collapse>

			<uni-forms :modelValue="formData" ref="form" :rules="rules">

				<uni-collapse-item title="工单入库单" :open="true">
					<uni-forms-item label="生产工单" :labelWidth='90' name="pwoCode">
						<uni-easyinput suffixIcon="scan" v-model="formData.pwoCode" @iconClick="scanBar" type="text"
							@confirm="scanBarCode"></uni-easyinput>
					</uni-forms-item>

				</uni-collapse-item>
				<!-- <u-divider text="工单入库单明细"></u-divider> -->
				<!-- <uni-collapse-item title="工单入库单明细" :open="true"> -->
				<view :key="item.id" v-for="(item, index) in materialStockList" style="over-flowe">
					<uni-badge :text="index+1" type="primary"></uni-badge>
					<uni-forms-item label="物料编码" :labelWidth='90'
						:name="'mesPwoJobCvDetailList.'+ index +'.	materialCode'">
						<uni-easyinput type="text" disabled v-model="item.materialCode"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="物料名称" :labelWidth='90'
						:name="'mesPwoJobCvDetailList.'+ index +'.materialName'">
						<uni-easyinput type="text" disabled v-model="item.materialName"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="类型" :labelWidth="90">
						<uni-tag :text="typetag(item.type)" :type="colortag(item.type)"
							@click="showConfirm(index)"></uni-tag>
					</uni-forms-item>
					<uni-forms-item label="数量" :labelWidth='90' :rules="rules.number"
						:name="'mesPwoJobCvDetailList.'+ index +'.number'">
						<u-number-box inputWidth="120" button-size="36" v-model="item.number" min="0"></u-number-box>
					</uni-forms-item>
					<uni-forms-item label="仓库" :labelWidth="90">
						<!-- 							<view class="item" catchtap="hidekeyborad">
								<picker @change="val => {typeConfirm(val,item);}" :value="tyindex" :range="range"
									range-key="label">
									<uni-easyinput type="text" v-model="item.whName" @focus="focus" />
								</picker>
							</view> -->
						<uni-data-select v-model="item.whCode" :localdata="range"></uni-data-select>

					</uni-forms-item>

				</view>
				<!-- </uni-collapse-item> -->

			</uni-forms>

		</uni-collapse>
		<u-button type="primary" @click="submit">入库</u-button>
	</view>
</template>

<script>
	import {
		getPwo,
		listPwo,
		handleIn
	} from "@/api/mes/pwoDraw.js";
	import {
		listWarehouse
	} from "@/api/wms/pdcIn.js";
	import {
		listStock
	} from "@/api/mes/pwoIn.js";
	export default {
		onLoad: function(option) {
			console.log(option);
			if (option) {
				this.formData.pwoCode = option.pwoCode;
				this.materialStockList = JSON.parse(decodeURIComponent(option.materialStockList));
				this.status = 10;
				this.isSearchRoute = true;
				this.scanBarCode()
			}

		},
		mounted() {
			listWarehouse().then(async res => {
				// this.range = [res.rows]
				console.log(this.range)
				// res.rows.map(item => {
				// 	item.label = item.warehouseCode + ':' + item.warehouseName;
				// 	return item
				// })
				this.range = res.rows.map(item => {
					return {
						text: item.warehouseCode + ':' + item.warehouseName,
						value: item.warehouseCode,
						diasble: false
					}
				});
			})
		},
		data() {
			return {
				whName: null,
				flag: false,
				isSearchRoute: false,
				selectedWarehouseName: [],
				materialStockList: [],
				formData: {
					pwoCode: null,
				},
				tyindex: null,
				value: 0,
				range: [],
				status: null,
				rules: {
					pwoCode: {
						rules: [{
							required: true,
							errorMessage: '请输入生产工单'
						}]
					},
				}
			}
		},
		methods: {
			focus() {
				console.log('key')
				if (this.flag) {
					return
				} else {
					console.log('key')
					// plus.key.hideSoftKeybord()
					uni.hideKeyboard()
				}
			},
			typetag(type) {
				switch (Number(type)) {
					case 1:
						return '合格品';
						break;
					case 2:
						return '不良品';
						break;
					case 3:
						return '报废品';
						break;
					case 4:
						return '待退料';
						break;
					default:
						return '无';
						break;
				}
			},
			colortag(type) {
				switch (Number(type)) {
					case 1:
						return 'success';
						break;
					case 2:
						return 'warning';
						break;
					case 3:
						return 'error';
						break;
					case 4:
						return 'primary';
						break;
					default:
						return '无';
						break;
				}
			},
			change(e) {
				console.log("e:", e);
			},
			scanBarCode() {
				console.log(this.formData.pwoCode);
				const _this = this;
				console.log(_this.formData.pwoCode);
				if (_this.formData.pwoCode) {
					let obj = {
						pwoCode: _this.formData.pwoCode
					}
					console.log(1);
					listPwo(obj).then(async res => {
						console.log(res.rows[0].id);
						_this.pwoId = res.rows[0].id;
						_this.status = res.rows[0].status;
					})
				}
				/** 获取物料信息 */

				// 清空物料库存信息
				this.materialStockList = [];
				console.log(this.formData.pwoCode);
				listStock({
					pwoBelongCode: this.formData.pwoCode
				}).then((resp) => {
					console.log(3);
					this.materialStockList = this.materialStockList.concat(resp.rows);
				});

			},
			typeConfirm(e, item) {
				this.tyindex = null;
				console.log(item)
				item.whName = this.range[e.detail.value].warehouseName;
				item.whCode = this.range[e.detail.value].warehouseCode;
				this.tyindex = e.detail.value
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.pwoCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			submit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定入库该生产工单吗？',
						success: function(res) {
							if (res.confirm) {
								if (_this.status != 10) {
									console.log(_this.status);
									_this.$modal.msg("该工单不是‘待入库’状态,请重新输入！")
								} else {
									// _this.formData.warehouseCode = _this.value;
									let stockWhList = _this.materialStockList.map((obj) => {
										return {
											id: obj.id,
											whCode: obj.whCode,
										};
									});
									_this.$modal.loading('提交中')
									handleIn(_this.pwoId, '1', stockWhList).then(res => {
										_this.$modal.closeLoading();
										_this.$modal.msgSuccess("入库成功！");

										if (_this.isSearchRoute = true) {
											setTimeout(() => {
												_this.$tab.navigateBack();
											}, 500);
										} else {
											setTimeout(() => {
												_this.$tab.switchTab(
													"/pages/work/index");
											}, 500);
										}

									});
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
	upLoadSelect /deep/ .uni-select__selector-scroll {
		overflow: hidden;
		height: 1500px; //调整为合适的高度即可
	}
</style>