<template>
	<view>
		<view class="box">
			<view class="top">
				<text class="title">车辆消杀</text>
			</view>
			<view class="item-line">
				基本信息
			</view>
			<view class="item">
				<view class="l"><text style="color: red;">*</text>车牌号：</view>
				<view class="r">
					<u-input placeholder="请填写车牌号" v-model="form.carNo" />
				</view>
			</view>
			<view class="item">
				<view class="l"><text style="color: red;">*</text>车辆类型：</view>
				<view class="r">
					<picker class="p-picker" id="qy" @change="bindPickerChange($event)" :value="type.index"
						:range="type.list" range-key="name">
						<text class="p-text">{{type.list[type.index]}}</text>
						<u-icon class="p-icon" name="arrow-down-fill" size="20"></u-icon>
					</picker>
				</view>
			</view>
			<view class="item-line">
				业务项
			</view>
			<view class="item" v-for="item in itemList" :key="item.id">
				<view class="l" style="flex: 6;">{{item.itemName}}：
				</view>
				<view class="r">
					<view style="margin-left: 20rpx;">
						{{item.price}}元
					</view>
				</view>
			</view>
			<view class="item">
				<view class="l">停车费：
				</view>
				<view class="r">
					<view style="margin-left: 20rpx;">
						30元
					</view>
				</view>
			</view>
		</view>
		<view v-if="!form.adminConfirmInput">
			<u-button type="primary" v-if="!obj" text="保存" @click="saveFn"></u-button>
			<!-- <u-button type="primary" v-if="obj" text="修改" @click="saveFn"></u-button> -->
			<u-button type="primary" v-if="obj" text="确认账单" @click="confirmFn" style="margin-top: 20rpx;"></u-button>
			<u-button type="info" text="重置" @click="resetFn" style="margin-top: 20rpx;"></u-button>
		</view>
		<!-- ---------------------------------------------------------- -->
		<view class="bottom-safety"></view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: {
					index: 0,
					list: ['空车', '载货']
				},
				obj: null,
				form: {
					carNo: ''
				},
				itemList: [],
				list: []
			}
		},
		onLoad(options) {
			let data = options.json;
			if (data) {
				this.obj = JSON.parse(options.json)
			}
		},
		mounted() {
			this.getTypeList();
		},
		onBackPress() {
			this.$common.to('/pages/onely-disinfect/Index');
			return true;
		},
		methods: {
			bindPickerChange(e) {
				let index = e.detail.value;
				this.type.index = index //当前picker选中的值
				if (index == 0) {
					this.itemList = this.list.filter(obj => obj.itemName.indexOf('车头车厢') !== -1)
				} else {
					this.itemList = this.list.filter(obj => obj.itemName.indexOf('车头消杀') !== -1)
				}
			},
			getTypeList() {
				this.$api.getItemList({
					business: 0
				}).then(resp => {
					let list = resp.data;
					let itemList = [];
					for (let i in list) {
						let item = list[i];
						itemList.push(item.items[0]);
					}
					this.list = itemList;
					this.itemList = itemList.filter(obj => obj.itemName.indexOf('车头车厢') !== -1);
					if (this.obj) {
						this.form.carNo = this.obj.carNo;
						this.form.businessCarId = this.obj.businessCarId;
						this.form.adminConfirmInput = this.obj.adminConfirmInput;
						this.form.id = this.obj.id;
						let index = this.type.list.indexOf(this.obj.goodsName);
						this.type.index = index
						if (index == 0) {
							this.itemList = this.list.filter(obj => obj.itemName.indexOf('车头车厢') !== -1)
						} else {
							this.itemList = this.list.filter(obj => obj.itemName.indexOf('车头消杀') !== -1)
						}
					}
				})
			},
			resetFn() {
				this.form.carNo = '';
				this.type.index = 0;
				this.itemList = this.list;
			},
			saveFn() {
				if (!this.form.carNo) {
					this.$common.toast('请输入车牌号')
					return;
				}
				this.form.itemJson = JSON.stringify(this.itemList);
				this.form.carType = this.type.list[this.type.index]
				let that = this;
				let content = this.obj ? '确认修改该业务？' : '确认录入该车辆的消杀业务?'
				uni.showModal({
					title: '提示',
					content: content,
					success(res) {
						if (res.confirm) {
							if (!that.obj) {
								that.addFn();
							} else {
								that.editFn()
							}

						}
					}
				})
			},
			addFn() {
				this.$api.addCarDisinfect(this.$common.removeNull(this.form)).then(resp => {
					if (resp.code == 200) {
						this.$common.to('/pages/onely-disinfect/addSuccess')
					}
				})
			},
			confirmFn() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认该账单？',
					success(res) {
						if (res.confirm) {
							that.$api.adminConfirm({
								ids: that.form.id
							}).then(resp => {
								if (resp.code == 200) {
									that.$common.toast('已确认');
									setTimeout(() => {
										that.$common.to('/pages/onely-disinfect/Index')
									}, 1500)
								}
							})
						}
					}
				})
			},
			editFn() {
				this.$api.editCarDisinfect(this.$common.removeNull(this.form)).then(resp => {
					if (resp.code == 200) {
						this.$common.toast('修改成功');
						setTimeout(() => {
							this.$common.to('/pages/onely-disinfect/Index')
						}, 1500)
					}
				})
			}
		},

	}
</script>

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

	.hs-item {
		text-align: center;
	}

	.item-line {
		color: #a2a2a2;
		padding: 5px 0 10px 29px;
		border-bottom: 1px solid #E5E5E5;
	}

	.hj {
		padding: 50rpx;
		font-size: 40rpx;
		color: red;
		font-weight: bold;
	}

	.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>
