<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="客户编码" :labelWidth='90' name="partnerCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode1"
							v-model="formData.partnerCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="客户名称" :labelWidth='90' name="partnerName">
						<uni-easyinput disabled v-model="formData.partnerName" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="允许经销地区" :labelWidth='90' name="country">
						<uni-easyinput disabled v-model="formData.country" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="件号" :labelWidth='90' name="pieceNo">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar2" @confirm="scanBarCodePieceNo"
							v-model="formData.pieceNo" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料编码" :labelWidth='90' name="wlCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar1" v-model="formData.wlCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料名称" :labelWidth='90' name="wlName">
						<uni-easyinput type="text" v-model="formData.wlName" disabled />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料批号" :labelWidth='90' name="wlBatchNum">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar3" type="text"
							v-model="formData.wlBatchNum" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="箱号" :labelWidth='90' name="lotNo">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar4" type="text" v-model="formData.lotNo" />
					</uni-forms-item>
				</uni-col>
				<!-- <uni-col :span="24">
						<uni-forms-item label="箱号标记码" :labelWidth='90' name="pieceSignId">
							<uni-easyinput disabled    type="text" v-model="formData.pieceSignId"/>	
						</uni-forms-item>
					</uni-col> -->
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listCustomer,
		addAntiChannelRecord,
		searchByCode
	} from "@/api/srm/antiCrossCargo.js";
	import {
		listMaterial
	} from "@/api/wms/request.js";
	export default {
		mounted() {
			this.test();
		},
		data() {
			return {
				formData: {
					partnerCode: null,
					partnerName: null,
					wlName: null,
					wlBatchNum: null,
					country: null,
					pieceNo: null,
					lotNo: null,
					pieceSignId: null,
					customerId: null,
					wlCode: null
				},
				rules: {
					partnerCode: {
						rules: [{
							required: true,
							errorMessage: '请输入客户编码!'
						}]
					},
					pieceNo: {
						rules: [{
							required: true,
							errorMessage: '请输入件号!'
						}]
					}
				}
			}
		},
		methods: {
			test() {},
			//根据物料编码查询物料名称
			scanBarCode() {
				if (this.formData.wlCode && this.formData.wlCode != "") {
					let obj = {
						materialCode: this.formData.wlCode
					}
					console.log(obj);
					listMaterial(obj).then(async res => {
						if (res.rows.length != 0) {
							console.log(res);
							this.formData.wlName = res.rows[0].materialName
						} else {
							this.$modal.msg("未检索到该物料编码!");
						}
					});
				} else {
					this.$modal.msg("请输入物料编码!");
				}
			},
			scanBarCodePieceNo() {
				if (this.formData.pieceNo) {
					searchByCode(this.formData.pieceNo, 3).then(async res => {
						if (res.data) {
							this.formData.wlName = res.data.materialName;
							this.formData.wlBatchNum = res.data.batchNo;
							this.formData.lotNo = res.data.lotNo;
							this.formData.pieceSignId = res.data.splitStockId;
							this.formData.wlCode = res.data.materialCode;
						} else {
							this.$modal.msg("未找到相应的物料信息！")
						}
					});
				}
			},
			//根据客户编码获取客户信息
			scanBarCode1() {
				if (this.formData.partnerCode && this.formData.partnerCode != "") {
					let obj = {
						partnerCode: this.formData.partnerCode
					}
					console.log(obj);
					listCustomer(obj).then(async res => {
						if (res.rows.length != 0) {
							console.log(res);
							this.formData.partnerName = res.rows[0].partnerName,
								this.formData.country = res.rows[0].country,
								this.formData.customerId = res.rows[0].id
						} else {
							this.$modal.msg("未检索到该客户!");
						}
					});
				} else {
					this.$modal.msg("请输入客户编码!");
				}
			},
			//客户编码
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.partnerCode = res.result;
						_this.scanBarCode1();
					}
				});
			},
			//件号
			scanBar2() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.pieceNo = res.result;
						_this.scanBarCodePieceNo();
					}
				});
			},
			//物料编码
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.wlCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			//物料批号
			scanBar3() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.wlBatchNum = res.result;

					}
				});
			},
			//箱号
			scanBar4() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.lotNo = res.result;

					}
				});
			},
			//箱号标记码
			// scanBar5() {
			//   const _this = this;
			//   uni.scanCode({
			// 	scanType: ['barCode','qrCode'],
			//   	success: function (res) {
			// 		_this.formData.pieceSignId = res.result;

			//   	}
			//   });
			// },
			submit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定录入吗？',
						success: function(res) {
							if (res.confirm) {
								let data = {
									customerId: _this.formData.customerId,
									allowDealPlace: _this.formData.country,
									pieceNo: _this.formData.pieceNo,
									pieceSignId: _this.formData.pieceSignId,
									materialCode: _this.formData.wlCode,
									materialName: _this.formData.wlName,
									batchNo: _this.formData.wlBatchNum,
									lotNo: _this.formData.lotNo
								}
								console.log(data);
								_this.$modal.loading('提交中')
								addAntiChannelRecord(data).then(async res => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("防串货录入成功!");
									setTimeout(() => {
										_this.$tab.switchTab("/pages/work/index");
									}, 500);
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
</style>