<template>
	<view>
		<view class="ordertop">
			<view>
				<view class="iconblue"></view>
				<text class="license">基础信息</text>
			</view>
			<view class="line">
				<u-line color="#ccc" border-style='dashed' />
			</view>
			<u-form :model="form" :rules="rules" ref="uForm" :errorType="errorType">
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="150rpx" label="入库日期：" prop="fBsdate"
					 required>
						<u-input type="type" :clearable="false" placeholder="请选择入库日期" v-model="form.fBsdate" disabled @click="shOw = !shOw" />
						<u-calendar v-model="shOw" mode="date" @change="bsdate"></u-calendar>
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="品名：" prop="fGoodsid" required>
						<u-input type="type" :clearable="false" placeholder="请选品名" v-model="fGoodsid" disabled @click="goods = !goods" />
						<u-select v-model="goods" @confirm="confiRm" :list="good_s" label-name="fName" value-name="fId">
						</u-select>
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="150rpx" label="货物属性：" prop="fBusinessType"
					 required disabled>
						<u-input type="type" :clearable="false" placeholder="请选择货物属性" v-model="fBusinessType" disabled @click="iness = !iness" />
						<u-select v-model="iness" @confirm="business" :list="fBusiness" label-name="dictLabel" value-name="dictSort" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="150rpx" label="属性详情：" prop="fMarks" required>
						<u-input type="type" :clearable="false" placeholder="请输入属性详情" v-model="form.fMarks" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="库区：" prop="fWarehouseInformation"
					 required>
						<u-select v-model="fQty" mode="mutil-column-auto" :list="list" @confirm="confirm" label-name="label" value-name="id"
						 child-name="children"></u-select>
						<u-input type="type" :clearable="false" placeholder="请选库区" v-model="fWarehouseInformation" disabled @click="fQty = !fQty" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="件数：" prop="fQty" required>
						<u-input type="digit" :clearable="false" placeholder="请输入件数" v-model="form.fQty" @input="calculation" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="毛重：" prop="fGrossweight"
					 required>
						<u-input type="digit" :clearable="false" placeholder="请输入毛重" v-model="form.fGrossweight" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="净重：" prop="fNetweight"
					 required>
						<u-input type="digit" :clearable="false" placeholder="请输入净重" v-model="form.fNetweight" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="车号：" prop="fTruckno" required>
						<u-input type="type" :clearable="false" placeholder="请输入车号" v-model="form.fTruckno" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="车队：" prop="fFleet">
						<u-input type="type" :clearable="false" placeholder="请选车队" v-model="fFleet" disabled @click="fEleet = !fEleet" />
						<u-select v-model="fEleet" @confirm="confiEm" :list="fleEt" label-name="fName" value-name="fId">
						</u-select>
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="箱号：" prop="fCntrno">
						<u-input type="type" :clearable="false" placeholder="请输入箱号" v-model="form.fCntrno" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="箱型：" prop="fcntrtype">
						<u-input type="type" :clearable="false" placeholder="请输入箱型" v-model="fcntrtype" disabled @click="rtype = !rtype" />
						<u-select v-model="rtype" @confirm="fcntrtPe" :list="fcnt" label-name="fName" value-name="fId"></u-select>
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="箱量：" prop="fCntqty">
						<u-input type="digit" :clearable="false" placeholder="请输入箱量" v-model="form.fCntqty" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="150rpx" label="劳务公司：" prop="labour">
						<u-input type="type" :clearable="false" placeholder="请选择劳务公司" v-model="labour" disabled @click="laBour = !laBour" />
						<u-select v-model="laBour" @confirm="confiLm" :list="laboUr" label-name="fName" value-name="fId">
						</u-select>
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="130rpx" label="装卸工：" prop="fStevedore">
						<u-input type="type" :clearable="false" placeholder="请输入装卸工" v-model="form.fStevedore" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="130rpx" label="提单号：" prop="fOriginalbillno">
						<u-input type="type" :clearable="false" placeholder="请输入提单号" v-model="form.fOriginalbillno" disabled />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;height: 55rpx;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="司机：" prop="fDriverName">
						<u-input type="type" :clearable="false" placeholder="请输入司机" v-model="form.fDriverName" />
					</u-form-item>
				</view>
				<view class="basic" style="margin: 0 auto;">
					<u-form-item style="width: 100%;" :border-bottom="false" label-width="100rpx" label="备注：">
						<u-input type="textarea" height="20rpx" placeholder="请输入备注" :clearable="false" v-model="form.remark" />
					</u-form-item>
				</view>
			</u-form>
		</view>
		<view class="ordertop" style="margin-bottom: 60rpx;">
			<view>
				<view class="iconblue"></view>
				<text class="license">附件上传</text>
				<!-- <view class="various" @click="attachmentP">保存附件</view> -->
			</view>
			<view class="line">
				<u-line color="#ccc" border-style='dashed' />
			</view>
			<view style="width: 94%;margin: 0 auto;">
				<u-upload ref="uUpload" :deletable="false" :action="action" :header="headers" @on-success="success" :file-list="fileList">
				</u-upload>
			</view>
		</view>
		<br>
		<view style="width: 100%;position:fixed; bottom:0;background-color: #FFFFFF;padding-left: 20px;padding-right: 20px;">
			<u-button type="primary" @click="appCheckCode">提交保存</u-button>
		</view>
		<u-modal v-model="submIt" @confirm="submit" :content="content" @cancel="submIt = false" :show-cancel-button="true" />
		<u-modal v-model="CheckStatus" @confirm="goBack" :content="content2" @cancel="CheckStatus = false"
		 :show-cancel-button="true" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content2:"数据发生变化，是否需要返回到列表？",
				fcntrtype: '',
				fcnt: [],
				errorType: ['toast'],
				rules: {
					fBsdate: [{
						required: true,
						message: '请选择入库日期',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fGoodsid: [{
						required: true,
						type: 'number',
						message: '请选择品名',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fBusinessType: [{
						required: true,
						type: 'number',
						message: '请选择货物属性',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fMarks: [{
						required: true,
						message: '请输入属性详情',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fWarehouseInformation: [{
						required: true,
						message: '请选择库区',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fQty: [{
						required: true,
						message: '请输入件数',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fGrossweight: [{
						required: true,
						message: '请输入毛重',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fNetweight: [{
						required: true,
						message: '请输入净重',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fTruckno: [{
						required: true,
						message: '请输入车号',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
					fOriginalbillno: [{
						required: true,
						message: '请输入提单号',
						// blur和change事件触发检验
						trigger: ['blur', 'change']
					}],
				},
				title: '', //标题
				fQty: false,
				iness: false,
				fBusiness: [],
				fBusinessType: '',
				action: 'https://test.wms.tubaosoft.com/prod-api/common/upload',
				headers: {
					Authorization: 'Bearer ' + uni.getStorageSync('lifeData').vuex_token
				},
				content: '是否确认提交',
				fileList: [],
				submIt: false,
				operationType: '',
				goods: false,
				good_s: [],
				lists: [],
				list: [],
				form: {
					fBsdate: '',
					fGoodsid: '',
					fBusinessType: '',
					fMarks: '',
					fWarehouseInformation: '',
					fQty: '',
					fGrossweight: '',
					fNetweight: '',
					fTruckno: '',
					fFleet: '',
					fCntrno: '',
					fcntrtype: '',
					fCntqty: '',
					labour: '',
					fStevedore: '',
					fOriginalbillno: ''
				},
				fEleet: false,
				rtype: false,
				fleEt: [],
				labour: '',
				laBour: false,
				laboUr: [],
				shOw: false,
				fFleet: '',
				fId: '',
				fGoodsid: '',
				fWarehouseInformation: '',
				warehouseBills: {},
				planNumber: {},
				CheckStatus:false,
			}
		},
		onLoad(option) {
			console.log(JSON.parse(decodeURIComponent(option.item)))
			this.dataList = JSON.parse(decodeURIComponent(option.item))
			this.planNumber.fQty = JSON.parse(decodeURIComponent(option.item)).fQty
			this.planNumber.fGrossweight = JSON.parse(decodeURIComponent(option.item)).fGrossweight
			this.planNumber.fNetweight = JSON.parse(decodeURIComponent(option.item)).fNetweight


			this.fGoodsid = JSON.parse(decodeURIComponent(option.item)).goodsName
			this.form.fOriginalbillno = JSON.parse(decodeURIComponent(option.item)).fMblno
			this.form.fBsdate = JSON.parse(decodeURIComponent(option.item)).fBsdate
			let lists = {
				fId: JSON.parse(decodeURIComponent(option.item)).fId
			}
			this.fId = JSON.parse(decodeURIComponent(option.item)).fId
			this.$u.get('/warehouseBusiness/applets/appBasicInformation',lists).then(res => {
				console.log(res)
				this.list = res.warehouse
				this.fBusiness = res.attributes
				this.good_s = res.goodsList
				this.fleEt = res.feelCorpList
				this.fcnt = res.cntrList
				this.laboUr = res.corpsList
				this.warehouseBills = res.warehouseBills
				console.log(res.warehouseBills)
				this.form.fGoodsid = res.warehouseBills.fGoodsid
				this.form.fBillno = res.warehouseBills.fBillno
				this.form.fMblno = res.warehouseBills.fMblno
				this.form.fBillingway = res.warehouseBills.fBillingway
				console.log(this.list)
			}).catch(err => {
				console.log(err)
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			goBack(){
				uni.navigateBack()
			},
			appCheckCode() {
				console.log(this.form.fDateChanged)
				this.$u.get('/warehouseBusiness/applets/appCheckCode',{
						fId: this.warehouseBills.fId,
						fType: "warehouse",
						codeVal: this.warehouseBills.fDateChanged,
					}).then(res => {
					if (!res) {
						this.CheckStatus=true
					} else {
						this.submIt=true
					}
				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: '网络繁忙请稍后再试',
						position: "bottom"
					})
				})
			},
			calculation() {
				// console.log(this.form.fQty)
				// this.warehouseBills.fQty //总件数
				// this.warehouseBills.fGrossweight //总净重
				// this.warehouseBills.fNetweight  //总毛重
				let sumQty=this.warehouseBills.fPlanqty-this.warehouseBills.fQty
				if (this.form.fQty <= sumQty) {
					let fQty = Number(this.warehouseBills.fPlangrossweight / this.warehouseBills.fPlanqty)
					let fqTy = Number(this.warehouseBills.fPlannetweight / this.warehouseBills.fPlanqty)
					let fGrossweight = fQty * this.form.fQty
					let fNetweight = fqTy * this.form.fQty
					this.form.fGrossweight = fGrossweight.toFixed(2)
					this.form.fNetweight = fNetweight.toFixed(2)
				} else {
					this.form.fQty = ''
					this.form.fGrossweight = ''
					this.form.fNetweight = ''
					uni.showToast({
						icon: 'none',
						title: '总计划件数为:' + sumQty + '不能大于总计划件数',
						position: "bottom"
					})
				}
			},
			business(e) {
				// console.log(e)
				this.form.fBusinessType = e[0].value
				this.fBusinessType = e[0].label
			},
			bsdate(e) {
				// console.log(e)
				this.form.fBsdate = e.result
			},
			confiLm(e) {
				// console.log(e)
				this.form.labour = e[0].value
				this.labour = e[0].label
			},
			confiEm(e) {
				// console.log(e)
				this.form.fFleet = e[0].value
				this.fFleet = e[0].label
			},
			fcntrtPe(e) {
				console.log(e)
				this.form.fcntrtype = e[0].value
				this.fcntrtype = e[0].label
			},
			confiRm(e) {
				// console.log(e)
				this.form.fGoodsid = e[0].value
				this.fGoodsid = e[0].label
			},
			confirm(e) {
				// console.log(e)
				let res = ''
				let id = ''
				for (let item in e) {
					id = e[item].value
					res += e[item].label + '/'
				}
				this.form.fWarehouseInformation = this.dataList.fWarehouseName + '/' + res.substr(0, res.length - 1)
				this.fWarehouseInformation = this.dataList.fWarehouseName + '/' + res.substr(0, res.length - 1)
				this.form.fWarehouselocid = id
				console.log(this.form.fWarehouseInformation, this.fWarehouseInformation)
			},
			receiving(id, i) {
				// 获取当前时间
				let date = new Date();
				let dateYear = date.getFullYear(); //获取年 
				let dateMonth = date.getMonth(); //获取月  
				let dateDate = date.getDate(); //获取当日
				uni.setNavigationBarTitle({
					title: this.title
				})
				this.$u.post('/fleet/ftmsorderbillscars/selectDriver',{
						id: id
					}).then(res => {
					// console.log(res)
					this.form = res.cars
					switch (i) {
						case 1:
							this.form.acceptDate = dateYear + '-' + dateMonth + '-' + dateDate
							break
						case 3:
							this.form.loadDate = dateYear + '-' + dateMonth + '-' + dateDate
							break
						case 4:
							this.form.mdLoadDate = dateYear + '-' + dateMonth + '-' + dateDate
							break
						case 5:
							this.form.unLoadDate = dateYear + '-' + dateMonth + '-' + dateDate
							break
						default:
							uni.showToast({
								icon: 'none',
								title: '未知错误状态=' + i,
								position: "bottom"
							})
							break
					}
					if (this.form.acceptDate) {
						this.form.acceptDate = this.form.acceptDate.slice(0, 10)
					}
					if (this.form.loadDate) {
						this.form.loadDate = this.form.loadDate.slice(0, 10)
					}
					if (this.form.mdLoadDate) {
						this.form.mdLoadDate = this.form.mdLoadDate.slice(0, 10)
					}
					if (this.form.unLoadDate) {
						this.form.unLoadDate = this.form.unLoadDate.slice(0, 10)
					}
				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: '网络繁忙请稍后再试',
						position: "bottom"
					})
				})
			},
			success(data, index, lists, name) {
				// console.log(data)
				// console.log(index)
				// console.log(lists)
				// console.log(name)
				// sssconsole.log(this.fileList)
			},
			attachmentP() {
				this.lists = []
				console.log(this.fileList)
				console.log(this.$refs.uUpload.lists)
				let lists = this.$refs.uUpload.lists
				if (lists.length != 0) {
					for (let item in this.$refs.uUpload.lists) {
						console.log(this.$refs.uUpload.lists[item].response)
						if (this.$refs.uUpload.lists[item].response) {
							this.lists.push({
								fUrl: this.$refs.uUpload.lists[item].response.url,
								fName: this.$refs.uUpload.lists[item].response.fileName
							})
						}
					}
				}
				let fileList = []
				console.log(this.fileList)
				if (this.fileList.length != 0) {
					for (let item in this.fileList) {
						fileList.push({
							fUrl: this.fileList[item].response.url,
							fName: this.fileList[item].response.fileName
						})
					}
				}
				this.lists = this.lists.concat(fileList)
				// console.log(this.lists)
				this.$u.post('/warehouseBusiness/applets/saveWarehouse',{
						'warehousebillsitems': JSON.stringify({
							fId: this.form.fId,
							fQty: this.form.fQty,
							fGrossweight: this.form.fGrossweight,
							fNetweight: this.form.fNetweight,
							fPid: this.form.fPid
						}),
						'tEnclosure': JSON.stringify(this.lists)
					}).then(res => {
					// console.log(res)
					if (res.code == 500) {
						if (res.msg == 'parseDecimal error, field : oilAmt') {
							uni.showToast({
								icon: 'none',
								title: '数字不正确，请检查后再提交',
								position: "bottom"
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
								position: "bottom"
							})
						}
					} else if (res.code == 200) {
						// uni.navigateBack();
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							position: "bottom"
						})
					}
				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: '网络繁忙请稍后再试',
						position: "bottom"
					})
				})
			},
			//提交
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log(valid)
						this.lists = []
						console.log(this.fileList)
						console.log(this.$refs.uUpload.lists)
						let lists = this.$refs.uUpload.lists
						if (lists.length != 0) {
							for (let item in this.$refs.uUpload.lists) {
								console.log(this.$refs.uUpload.lists[item].response)
								if (this.$refs.uUpload.lists[item].response) {
									this.lists.push({
										fUrl: this.$refs.uUpload.lists[item].response.url,
										fName: this.$refs.uUpload.lists[item].response.fileName
									})
								}
							}
						}
						let fileList = []
						console.log(this.fileList)
						if (this.fileList.length != 0) {
							for (let item in this.fileList) {
								fileList.push({
									fUrl: this.fileList[item].response.url,
									fName: this.fileList[item].response.fileName
								})
							}
						}
						this.lists = this.lists.concat(fileList)
						this.form.fPid = this.fId
						let data = new Date();
						this.form.fChargedate = this.form.fOriginalbilldate = this.form.fBsdate
						this.form.fBilltype = 'SJRK'
						console.log(this.form)
						this.$u.post('/warehouseBusiness/applets/saveWarehouse',{
								item: this.form,
								attachs: this.lists
							}).then(res => {
							console.log(res)
							if (res.code == 500) {
								if (res.msg == 'parseDecimal error, field : oilAmt') {
									uni.showToast({
										icon: 'none',
										title: '数字不正确，请检查后再提交',
										position: "bottom"
									})
								} else {
									uni.showToast({
										icon: 'none',
										title: res.msg,
										position: "bottom"
									})
								}
							} else if (res.code == 200) {
								uni.navigateBack();
							}
						}).catch(err => {
							uni.showToast({
								icon: 'none',
								title: '网络繁忙请稍后再试',
								position: "bottom"
							})
						})
					} else {
						// console.log('验证失败');
						// console.log(this.form.fGoodsid)
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.basic {
		width: 94%;
		margin: 10rpx auto;
		display: flex;
		justify-content: space-between;
	}

	.basic>view {
		margin-bottom: 10rpx;
	}

	.basic>view:nth-child(1) {
		color: #797979;
	}

	.license {
		float: left;
		font-size: 32rpx;
	}

	.ordertop {
		width: 96%;
		background-color: #fff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		padding-top: 20rpx;
		box-shadow: 0px 0px 8px 0px rgba(165, 189, 251, 0.4);
		padding-bottom: 10rpx;
	}

	.ordertop>view:nth-child(1) {
		width: 98%;
		margin-bottom: 60rpx;
	}

	.iconblue {
		width: 10rpx;
		height: 45rpx;
		float: left;
		background-color: #3a63cf;
		margin-right: 10rpx;
	}

	.various {
		float: right;
		width: 150rpx;
		border: 2rpx solid #3a63cf;
		text-align: center;
		border-radius: 100rpx;
		color: #3a63cf;
	}

	.line {
		width: 92%;
		margin: 0 auto;
	}
</style>
