<template>
	<view>
		<view class="box">
			<view class="top">
				<text class="title">{{itemType.name}}</text>
			</view>

			<view class="item-line">
				基本信息
			</view>
			<view class="item" v-if="!customerId||customerId=='1'">
				<view class="l"><text style="color: red;">*</text>客户：</view>
				<view class="r">
					<picker v-if="customer.list.length>0" class="p-picker" @change="customerChange($event)"
						:value="customer.index" :range="customer.list" range-key="name">
						<text class="p-text">{{customer.list[customer.index].name}}</text>
						<u-icon class="p-icon" name="arrow-down-fill" size="20"></u-icon>
					</picker>
				</view>
			</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">车辆类型：</view>
				<view class="r">
					<picker v-if="type.list.length>0" 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].name}}</text>
						<u-icon class="p-icon" name="arrow-down-fill" size="20"></u-icon>
					</picker>
				</view>
			</view>
			<view class="item">
				<view class="l">车辆规格(米)：</view>
				<view class="r">
					<u-input placeholder="请填写车辆规格" v-model="form.carSize" />
				</view>
			</view>
			<view class="item" v-if="type.index==0">
				<view class="l">载重(kg)：</view>
				<view class="r">
					<u-input placeholder="请填写车辆载重" v-model="form.carWeight" />
				</view>
			</view>
			<view class="item">
				<view class="l">作业人员：</view>
				<view class="r">
					<u-input placeholder="请填写作业人员" v-model="form.operator" />
				</view>
			</view>

			<view class="item">
				<view class="l">作业时间：</view>
				<view class="r">
					<uni-datetime-picker placeholder="请选择" :clear-icon="false" type="datetime"
						v-model="form.operateTime" />
				</view>
			</view>
			<view class="item-line">
				收费明细<text style="color: red;">（总共{{totalPrice}}元）</text>
			</view>
			<u-checkbox-group v-model="checkList" placement="column">
				<uni-list v-for="item in itemType.items">
					<uni-list-item style="display: inline;" :title="item.itemName" :note="item.price+''+item.unit">
						<template v-slot:header>
							<u-checkbox :name="item.id" :disabled="item.need==1" style="display: inline;">
							</u-checkbox>
						</template>

						<template v-slot:footer>
							<div style="position: absolute;right: 230rpx;top: 65rpx;">
								<u-number-box :disabled="item.inc==0" v-model="item.num">
									<view slot="minus" class="minus">
										<u-icon name="minus" size="12"></u-icon>
									</view>
									<text v-if="item.inc==0" slot="input"
										style="width: 50px;text-align: center;color: #E5E5E5;"
										class="input">{{item.num}}</text>
									<text v-else slot="input" style="width: 50px;text-align: center;"
										class="input">{{item.num}}</text>
									<view slot="plus" class="plus">
										<u-icon name="plus" size="12"></u-icon>
									</view>
								</u-number-box>
							</div>
							<u-button v-show="item.needRemark" @click="openRemarkFn(item)" style="width: 50px;"
								type="primary" size="mini" text="备注"></u-button>
						</template>
					</uni-list-item>
				</uni-list>
			</u-checkbox-group>
		</view>
		<view v-if="!form.adminConfirmInput">
			<u-button type="primary" text="保存" @click="saveFn"></u-button>
		</view>
		<!-- ---------------------------------------------------------- -->
		<view class="bottom-safety"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showRemark: false,
				type: {
					index: 0,
					list: []
				},
				customer: {
					index: 0,
					list: []
				},
				customerId: '1',
				form: {
					customerId: '',
					carNo: '',
					carSize: '',
					carWeight: '',
					operator: ''
				},
				list: [],
				checkList: [],
				itemType: {
					id: '',
					name: '',
					items: []
				}
			}
		},
		computed: {
			totalPrice() {
				let items = this.itemType.items;
				let checkList = this.checkList;
				items = items.filter(obj => checkList.indexOf(obj.id) !== -1);
				let price = 0;
				for (let i in items) {
					let item = items[i];
					price = price + item.price * item.num;
				}
				return price;
			}
		},
		onLoad(options) {
			let customerId = uni.getStorageSync('customerId');
			this.customerId = customerId;
			let typeId = options.typeId;
			this.itemType.id = typeId;

			this.getItemTypeEnum();
			if (!customerId || customerId == '1') {
				this.getCustomerList();
			}
			let that = this;
			uni.$on('getRemark', data => {
				that.$nextTick(function() {
					let items = that.itemType.items;
					items.filter(obj => obj.id == data.itemId)[0].remark = data.remark;
				})
			})
		},

		methods: {
			getItemTypeEnum() {
				this.$api.getItemTypeEnum().then(resp => {
					this.type.list = resp.data;
					this.getItemType();
				})
			},
			getCustomerList() {
				let p = {
					judgeStatus: 2,
					pageNo: 1,
					pageSize: 30,
					sortType: 20
				}
				this.$api.getCustomerList(p).then(resp => {
					this.customer.list = resp.data;
				})
			},
			openRemarkFn(data) {
				this.$common.to('/pages/onely-disinfect/add-remark?itemId=' + data.id + '&title=' + data.itemName +
					'&remark=' + data.remark + '&mustRemark=' + data.mustRemark)
			},
			closeFn() {
				this.$refs.popup.close()
			},
			getItemType() {
				this.$api.getItemType({
					id: this.itemType.id
				}).then(resp => {
					let data = resp.data;
					let items = data.items
					.filter(obj => obj.itemType && obj.itemType.indexOf(this.type.list[this.type.index].name) !== -1);
					data.items=items;
					this.itemType = data;
					this.checkList = items.filter(obj => obj.need == 1).map(obj => obj.id)

				})
			},
			bindPickerChange(e) {
				let index = e.detail.value;
				this.type.index = index //当前picker选中的值
				this.getItemType();
			},
			customerChange(e) {
				let index = e.detail.value;
				this.customer.index = index //当前picker选中的值
			},
			saveFn() {
				if (!this.form.carNo) {
					this.$common.toast('请输入车牌号')
					return;
				}
				let itemList = this.itemType.items;
				let checkList = this.checkList;
				let list = itemList.filter(obj => checkList.indexOf(obj.id) !== -1);
				if (this.customerId == '1' || !this.customerId) {
					this.form.customerId = this.customer.list[this.customer.index].id;
				} else {
					this.form.customerId = this.customerId
				}
				this.form.itemTypeId = this.itemType.id;
				this.form.items = list;
				this.form.carType = this.type.list[this.type.index].name
				let that = this;
				let content = '确认录入该车辆的业务?'
				uni.showModal({
					title: '提示',
					content: content,
					success(res) {
						if (res.confirm) {
							that.addFn();
						}
					}
				})
			},
			addFn() {
				this.$api.addCarDisinfect(this.$common.removeNull(this.form)).then(resp => {
					if (resp.code == 200) {
						this.$common.to('/pages/onely-disinfect/addSuccess?typeId=' + this.itemType.id)
					} else {
						this.$common.toast(resp.msg);
					}
				})
			},
		},

	}
</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>
