<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-collapse-item title="销售出库单" :open="true">
					<uni-forms-item label="销售出库任务单" :labelWidth='90' name="saleOutTaskCode">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarTaskCode"
							v-model="formData.saleOutTaskCode" />
					</uni-forms-item>
					<uni-forms-item label="仓库编码" :labelWidth='90' name="warehouseCode">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarCk"
							v-model="formData.warehouseCode" />
					</uni-forms-item>
					<uni-forms-item label="客户编码" :labelWidth='90' name="customerCode">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarKh"
							v-model="formData.customerCode" />
					</uni-forms-item>
				</uni-collapse-item>
				<uni-collapse-item title="销售出库单明细" :open="true">
					<uni-swipe-action>
						<uni-swipe-action-item :rightOptions="rightOptions"
							v-for="(item, index) in formData.wmsSaleOutDetailList"
							@click="(data) => clickDetail(index,data)" :key="index">
							<uni-badge :text="index+1" type="primary"></uni-badge>
							<uni-forms-item label="物料编码" :name="'wmsSaleOutDetailList.'+ index +'.materialCode'">
								<uni-easyinput type="text" disabled v-model="item.materialCode"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料名称" :name="'wmsSaleOutDetailList.'+ index +'.materialName'">
								<uni-easyinput type="text" disabled v-model="item.materialName"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料批号" :labelWidth='90'
								name="'wmsSaleOutDetailList.'+ index +'.materialBatchNo'">
								<uni-easyinput disabled type="text" v-model="item.materialBatchNo" />
							</uni-forms-item>
							<uni-forms-item label="物料箱号" :labelWidth='90'
								name="'wmsSaleOutDetailList.'+ index +'.materialLotNo'">
								<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarXh"
									v-model="item.materialLotNo" />
							</uni-forms-item>
							<!-- 							<uni-forms-item label="库位条码" :labelWidth='90'
								name="'wmsSaleOutDetailList.'+ index +'.storageLocationBarcode'">
								<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarKq"
									v-model="item.storageLocationBarcode" />
							</uni-forms-item> -->
							<uni-forms-item label="库位条码" :labelWidth='90'
								name="'wmsSaleOutDetailList.'+ index +'.storageLocationBarcode'">
								<uni-easyinput suffixIcon="scan" @iconClick="scanBarstorageLocationBarcode(index)"
									@change="splitStlBarcode(index)" type="text" v-model="item.storageLocationBarcode"
									ref="sbcode" />
							</uni-forms-item>
							<uni-forms-item label="出库数量" :labelWidth='90'
								name="'wmsSaleOutDetailList.'+ 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='120'>
								<uni-data-checkbox v-model="radio" :localdata="chIn"></uni-data-checkbox>
							</uni-forms-item>
							<view v-show="radio==1">
								<uni-forms-item label="防串货编码" :labelWidth='90' name="code">
									<uni-easyinput suffixIcon="scan" @iconClick="scanBarCode" type="text"
										v-model="code" />
								</uni-forms-item>
								<uni-forms-item label="编码类型" :labelWidth='120'>
									<uni-data-checkbox v-model="radio1" :localdata="type"></uni-data-checkbox>
								</uni-forms-item>
								<view style="text-align: center;">
									<button type="warning" size="mini" @click="codeIn(index)">录入</button>
								</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		getTask,
		listPick,
		addOut,
		listTask,
		getOutBillBySourceCode
	} from "@/api/wms/sale.js";
	import {
		listMaterial
	} from "@/api/wms/request.js";
	import {
		listCustomer,
		addAntiChannelRecord,
		searchByCode
	} from "@/api/srm/antiCrossCargo.js";
	import {
		conforms
	} from "lodash";
	export default {
		mounted() {

		},
		data() {
			return {
				code: null,
				radio: 0,
				radio1: 1,
				chIn: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 0
					}
				],
				type: [{
						text: '批号',
						value: 1
					},
					{
						text: '箱号',
						value: 2
					},
					{
						text: '件号',
						value: 3
					},
				],
				checkStorageLocationBarcode: true,
				formData: {
					saleOutTaskCode: null,
					warehouseCode: null,
					customerCode: null,
					wmsSaleOutDetailList: [],
					billType: '1'
				},
				formIn: {

				},
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					saleOutTaskCode: {
						rules: [{
							required: true,
							errorMessage: '请输入销售出库任务单!'
						}]
					},
				}
			}
		},

		methods: {
			deleteDetail(index) {
				this.formData.wmsSaleOutDetailList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			test() {},
			splitStlBarcode(i) {
				const _this = this;
				const detail = _this.formData.wmsSaleOutDetailList[i];

				detail.whCode = null;
				detail.storageAreaCode = null;
				detail.storageShelvesCode = null;
				detail.storageLocationCode = null;

				let barcode = detail.storageLocationBarcode;
				if (!barcode) {
					return; // 如果没有条码，不做任何处理
				}

				let [whCode, storageAreaCode, storageShelvesCode, storageLocationCode, extra] = barcode.split('-');

				if (whCode) {
					let warehouseObj = _this.$store.getters.warehouseOptions.find(item => item.warehouseCode == whCode);
					if (!warehouseObj) {
						_this.checkStorageLocationBarcode = false;
						_this.$modal.msg("货架不存在！");
						return;
					}
					if (storageAreaCode) {
						let areaObj = _this.$store.getters.areaOptions.find(item => item.storageAreaCode ==
							storageAreaCode);
						if (!areaObj) {
							_this.checkStorageLocationBarcode = false;
							_this.$modal.msg("库区不存在！");
							return;
						}
						if (storageShelvesCode) {
							let shelvesObj = _this.$store.getters.shelvesOptions.find(item => item.storageShelvesCode ==
								storageShelvesCode);
							if (!shelvesObj) {
								_this.checkStorageLocationBarcode = false;
								_this.$modal.msg("货架不存在！");
								return;
							};
							if (storageLocationCode) {
								let locationObj = _this.$store.getters.locationOptions.find(item => item
									.storageLocationCode == (extra ?
										(storageLocationCode + '-' + extra) : storageLocationCode));
								if (!locationObj) {
									_this.checkStorageLocationBarcode = false;
									_this.$modal.msg("库位不存在！");
									return;
								};
							}
						}
					}
				}
				_this.checkStorageLocationBarcode = true;
				detail.whCode = whCode || null;
				detail.storageAreaCode = storageAreaCode || null;
				detail.storageShelvesCode = storageShelvesCode || null;
				detail.storageLocationCode = extra ? `${storageLocationCode}-${extra}` : storageLocationCode || null;
			},
			scanBarstorageLocationBarcode(i) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						console.log(res.result)
						// _this.formData.wmsSaleOutDetailList[i].storageLocationBarcode = res.result;
						_this.$refs.sbcode.onClear();
						_this.$set(_this.formData.wmsSaleOutDetailList[i], 'storageLocationBarcode', res
							.result);
						_this.formData.wmsSaleOutDetailList[i] = Object.assign({}, _this.formData
							.wmsSaleOutDetailList[i])
						_this.splitStlBarcode(i);
					}
				});
			},
			scanBarTaskCode() {
				var id = null;
				if (this.formData.saleOutTaskCode && this.formData.saleOutTaskCode != "") {
					getOutBillBySourceCode({
						saleOutTaskCode: this.formData.saleOutTaskCode
					}).then(async res => {
						if (res.wmsSaleOut) {
							console.log(res)
							this.formData = res.wmsSaleOut;
							this.formData.billType = '1';
							if (res.wmsSaleOut.wmsSaleOutDetailList.length > 0) {
								this.formData.wmsSaleOutDetailList = res.wmsSaleOut.wmsSaleOutDetailList.map(
									item => {
										item.storageLocationBarcode = item.whCode ? (item.storageAreaCode ?
											(
												item.shelvesCode ? (item.storageLocationCode ? (item
													.whCode + '-' + item.storageAreaCode + '-' +
													item
													.shelvesCode +
													'-' + item.storageLocationCode) : (item
													.whCode +
													'-' + item.storageAreaCode + '-' + item
													.shelvesCode
												)) : (item.whCode + '-' + item.storageAreaCode)) : item
											.whCode) : ''

										return item
									});
							}

						} else {
							this.$modal.msg("未检索到相关物料明细!");
						}
					});
				} else {
					this.$modal.msg("请输入销售出库任务单!");
				}
			},
			//领料任务单
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.saleOutTaskCode = res.result;
						_this.scanBarTaskCode();
					}
				});
			},

			//仓库编码
			scanBarCk() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.warehouseCode = res.result;
					}
				});
			},
			//仓库编码
			scanBarKh() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.customerCode = res.result;
					}
				});
			},
			//库区编码
			scanBarKq() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.storageLocationBarcode = res.result;
					}
				});
			},
			//箱号编码
			scanBarXh() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.materialLotNo = res.result;
					}
				});
			},
			//箱号编码
			scanBarCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.code = res.result;
					}
				});
			},
			//防串货录入
			codeIn(i) {
				if (this.code && this.radio1) {
					this.search(this.code, this.radio1, i)
				}
			},
			search(c, t, i) {
				searchByCode(c, t).then(async res => {
					if (res.data) {
						console.log(res);
						if (res.data.batchNo == this.formData.wmsSaleOutDetailList[i].materialBatchNo &&
							res.data.materialCode == this.formData.wmsSaleOutDetailList[i].materialCode) {
							this.scanBarCode1();
							let data = {
								customerId: this.formIn.customerId,
								allowDealPlace: this.formIn.country,
								pieceNo: res.data.pieceNo,
								pieceSignId: res.data.pieceSignId,
								materialCode: res.data.materialCode,
								materialName: res.data.materialName,
								batchNo: res.data.batchNo,
								lotNo: res.data.lotNo
							}
							console.log(data);
							addAntiChannelRecord(data).then(async res => {
								this.$modal.msgSuccess("防串货录入成功!");
							});
						} else {
							this.$modal.msg("物料信息不匹配！")
						}
					} else {
						this.$modal.msg("未检索到相应的物料信息！")
					}
				});
			},
			//根据客户编码获取客户信息
			scanBarCode1() {
				if (this.formData.customerCode && this.formData.customerCode != "") {
					let obj = {
						customerCode: this.formData.customerCode
					}
					listCustomer(obj).then(async res => {
						if (res.rows.length != 0) {
							this.formIn.allowDealPlace = res.rows[0].country,
								this.formIn.customerId = res.rows[0].id
						} else {
							this.$modal.msg("未检索到该客户!");
						}
					});
				} else {
					this.$modal.msg("请输入客户编码！")
				}
			},
			submit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定出库该物料吗？',
						success: function(res) {
							if (res.confirm) {
								// let data = {
								// 	saleOutTaskCode: _this.formData.saleOutTaskCode,
								// 	warehouseCode: _this.formData.warehouseCode,
								// 	customerCode: _this.formData.customerCode,
								// 	wmsSaleOutDetailList: _this.formData.wmsSaleOutDetailList
								// }
								_this.formData.wmsSaleOutDetailList.map(item => {
									item.secondNumber = item.number;
									item.secondUnitId = item.unitId;
									return item
								})
								console.log(_this.formData)
								if (!_this.checkStorageLocationBarcode) {
									_this.$modal.msg("库位条码校验错误，请重新输入！")
									return;
								}
								_this.$modal.loading('提交中')
								addOut(_this.formData).then(async res => {
									_this.$modal.msgSuccess("出库成功!");
									_this.$modal.closeLoading();
									setTimeout(() => {
										_this.$tab.switchTab("/pages/work/index");
									}, 500);
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
	.divider {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 10px;
		margin-bottom: 10px;
		height: 1px;
		background-color: #F1F1F1;
	}

	.divider span {
		padding: 5px;

	}
</style>