<template>
	<view>
		<view class="box">
			<view class="top">
				<text class="title">业务车辆</text>
			</view>
			<view class="item">
				<view class="l"><text style="color: red;">*</text>车牌号：</view>
				<view class="r">
					<u-input placeholder="输入车牌号" v-model="form.carNo">
					</u-input>
				</view>
			</view>
			<view class="item">
				<view class="l"><text style="color: red;" v-if="needCarSize==1">*</text>规格(米)：</view>
				<view class="r">
					<u-input placeholder="输入规格" type="number" v-model="form.carSize">
					</u-input>
				</view>
			</view>
			<view class="item">
				<view class="l">车辆类型：</view>
				<view class="r" style="flex: 12;">
					<picker v-if="carType.list.length>0" class="p-picker" @change="carTypeChange($event)"
						:value="carType.index" :range="carType.list" range-key="name">
						<text class="p-text">{{carType.list[carType.index].name}}</text>
					</picker>
				</view>
			</view>

			<view class="item">
				<view class="l"><text style="color: red;" v-if="needWeight==1&&carType.index==1">*</text>载重(kg)：</view>
				<view class="r">
					<u-input placeholder="车辆载重" v-model="form.netWeight">
					</u-input>
				</view>
			</view>
		</view>
		<view style="margin-top: 50px;">
			<u-button type="primary" text="确定" @click="saveFn"></u-button>
			<u-button type="info" text="返回" @click="backFn" style="margin-top: 20rpx;"></u-button>
		</view>
		<!-- ---------------------------------------------------------- -->
		<view class="bottom-safety"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				carType: {
					index: 0,
					list: []
				},
				needCarSize: 0,
				needWeight: 0,
				form: {
					carNo: '',
					carType: '',
					netWeight: '',
					carSize: ''
				}
			}
		},
		onLoad(options) {
			this.form = JSON.parse(options.carJson);
			this.needCarSize = options.needCarSize;
			this.needWeight = options.needWeight;
			this.getCarType();
		},
		methods: {
			getCarType() {
				this.$api.getCarType().then(resp => {
					let list = resp.data;
					this.carType.list = list;
					let form=this.form;
					if(form.netWeight&&form.netWeight>0){
						this.carType.index=1;
					}
				})
			},
			carTypeChange(e) {
				var value = e.detail.value; //当前picker选中的值
				this.carType.index = value;
			},
			backFn(){
			this.$common.back();	
			},
			saveFn() {
				let data = this.form;
				if (!data.carNo) {
					this.$common.toast('请填写车牌号')
					return false;
				}
				let needCarSize = this.needCarSize;
				if (!data.carSize&&needCarSize==1) {
					this.$common.toast('请填写车辆规格')
					return false;
				}
				let needWeight = this.needWeight;
				if (needWeight==1&&!data.netWeight&&this.carType.index==1) {
					this.$common.toast('请填写车辆载重')
					return false;
				}
				data.carNo=data.carNo.toUpperCase();
				uni.navigateBack({
					delta: 1,
					success: function(resp) {
						uni.$emit('getCar', data) //触发事件
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.save-btn {
		background-color: #ff4200;
		height: 88rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 60rpx;
		color: #fff;
		font-size: 30rpx;
		font-weight: bold;
		border-radius: 10rpx;
	}

	@import '@/common/common.scss'
</style>
