<template>
	<view class="uploadWeightBill content_box">
		<u-toast ref="uToast" />
		<!-- 磅单简要信息 -->
		<shipmentBriefInfo />
		<!-- 磅单明细 -->
		<view class="weightBill-datail">
			<u--form :model="formData" :rules="rules" ref="form" errorType='none'>
				<u-form-item prop="tareWeight" required label="过磅皮重/吨" labelWidth="30%" borderBottom>
					<u-input border="none" inputAlign="left" placeholder="输入皮重" placeholderClass="input-tip"
						type="digit" v-model="formData.tareWeight" holdKeyboard/>
					<u-icon slot="right" name="arrow-right" />
				</u-form-item>
				<u-form-item prop="grossWeight" required label="过磅毛重/吨" labelWidth="30%" borderBottom>
					<u-input border="none" inputAlign="left" placeholder="输入毛重" placeholderClass="input-tip"
						type="digit" v-model="formData.grossWeight" holdKeyboard/>
					<u-icon slot="right" name="arrow-right" />
				</u-form-item>
				<u-form-item prop="netWeight" label="过磅净重" labelWidth="30%" borderBottom>
					<u-input border="none" type="text" readonly inputAlign="left" placeholderClass="input-tip"
						v-model="formData.netWeight" />
				</u-form-item>
				<u-form-item prop="poundNo" label="磅单号" labelWidth="30%" borderBottom>
					<u-input border="none" type="text" inputAlign="left" placeholder="输入磅单号"
						placeholderClass="input-tip" v-model="formData.poundNo" holdKeyboard/>
				</u-form-item>
				<u-form-item prop="weightTime" label="过磅时间" labelWidth="30%">
					<view @click="onVisible">
						<u-input border="none" readonly inputAlign="left" placeholder="选择时间"
							placeholderClass="input-tip" v-model="formData.weightTime" />
					</view>
					<u-icon slot="right" name="arrow-right" @click="onVisible" />
				</u-form-item>
			</u--form>
		</view>
		<!-- 上传图片 -->
		<view class="uploader-img">
			<bs-uploader uploadTitle="上传磅单" required ref="weightImgs" />
		</view>
		<!-- 底部按钮 -->
		<view class="operation-btn flex_sb">
			<button class="backBtn common_btn" @click="onNavBack">取消</button>
			<button class="submitBtn common_btn" @click="onSubmit">确认上传</button>
		</view>
		<!-- 时间选择 -->
		<bs-datetimePicker ref="myTimePicker" dateType="datetime" :visible="showPopup" @onVisible="onVisible"
			@onSelsectDate="onSelsectDate" />
	</view>
</template>

<script>
	import { shipmentLocation, shipmentStop } from '../../api/apiList'
	import { formatTime } from '../../utils/util'
	const getUserLocation = require('../../mixins/getUserLocation')
	const app = getApp()
	export default {
		mixins: [getUserLocation],
		data() {
			return {
				formData: {
					grossWeight: "", //过磅毛重
					tareWeight: "", //过磅皮重
					netWeight: "", //过磅净
					poundNo: "", //磅单号
					weightTime: "", //过磅时间
				},
				rules: {
					'grossWeight': { required: true },
					'tareWeight': { required: true },
				},

				selectedIds: [],
				loadStop: true, //是否为装货站点
				isMassLoad: false, //判断是否为大宗模式

				stopsInfo: {}, //站点信息
				shipmentInfo: {}, //运单信息

				//时间选择配置
				showPopup: false,
				timestamp: '',
			}
		},
		onLoad: function(options) {
			const data = JSON.parse(options.data)
			const { grossWeight, tareWeight, netWeight, loadStop} = data.currentLocationInfo
			const isMassLoad = getApp().globalData.shipmentInfo.shipmentBizName === 'tm.MassShipment' //判断是否为大宗模式
			this.loadStop = loadStop
			this.isMassLoad = isMassLoad
			this.formData.grossWeight = grossWeight || ''
			this.formData.tareWeight = tareWeight || ''
			this.formData.netWeight = netWeight
			this.selectedIds = [data.selectedId]
			this.shipmentInfo = app.globalData.shipmentInfo
			this.stopsInfo = app.globalData.stopsInfo
			this.getUserSetting()
		},
		methods: {
			// 时间选择
			onSelsectDate(dateInfo) {
				const { dateValue, timestamp } = dateInfo
				this.formData.weightTime = dateValue.replace(/(.*)-/,'$1:')
				this.timestamp = timestamp
				this.showPopup = false
			},
			//关闭时间选择框
			onVisible() {
				this.showPopup = !this.showPopup
			},
			// 提交表单数据
			onSubmit() {
				this.$refs.form.validate().then(res => {
					let { grossWeight, tareWeight, poundNo, weightTime } = this.formData
					const imgInstance = this.$refs.weightImgs
					imgInstance.uploadImage(images => {
						const { longitude, latitude, address, time } = this.locationInfo
						let weightTime = time
						if (this.timestamp) {
							weightTime = formatTime(new Date(this.timestamp))
						}

						const baseData = {
							"aux": {
								grossWeight,
								tareWeight,
								weightTime,
								images,
								longitude,
								latitude,
								address,
								poundNo,
								eventSource: 'DriverApp'
							},
							args: {
								"selectedIds": this.selectedIds
							}
						}

						if(this.isMassLoad){
							const loadAction = this.loadStop ? 'loadWeight' : 'unloadWeight'
							shipmentStop(loadAction, baseData).then(res => {
								if (res.data.messageType === 'success') {
									uni.showToast({
										title: '磅单上传成功',
										duration: 3000
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 1500)
								}
							})
						} else {
							shipmentLocation('uploadWeight', baseData).then(res => {
								if (res.data.messageType === 'success') {
									uni.showToast({
										title: '磅单上传成功',
										duration: 3000
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 1500)
								}
							})
						}
					})
				}).catch(errors => {
					this.$refs.uToast.show({
						message: '请完善必填信息',
					})
				})
			},
			//返回上一级页面
			onNavBack() {
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss">
	.uploadWeightBill {
		/deep/.weightBill-datail {
			padding: 0 20rpx;
			background-color: #ffffff;

			.input-tip {
				font-size: 28rpx;
				color: #BFBFBF;
			}

			.u-form-item__body__left__content__label {
				color: #8C8C8C;
				font-size: 28rpx;
			}

			.u-form-item__body {
				padding: 30rpx 0;
			}

			.u-icon {
				margin-left: 20rpx;
			}
		}

		.uploader-img {
			margin-top: 24rpx;
			padding: 25rpx;
			background-color: #ffffff;
		}

		.operation-btn {
			min-height: 160rpx;
		}
	}
</style>
