<template>
	<!-- 添加运单 -->
	<view>
		<u-navbar title="添加运单" autoBack placeholder></u-navbar>
		<view class="title_waybill">寄件人:</view>
		<x-form ref="sender" :list="list" :model="senderModel" :rules="rules"></x-form>
		<view class="title_waybill">收件人:</view>
		<x-form ref="recipients" :list="list" :model="recipientsModel" :rules="rules"></x-form>
		<view class="title_waybill">物品信息:</view>
		<x-form ref="goods" :list="goodsList" :model="goodsModel" :rules="goodsRules"></x-form>
		<view style="width: 100%;height: 150rpx;"></view>
		<view class="btn_print" @click="printWaybill">
			<u-button type="primary" text="添加运单"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		formRules,
		pickupRulesil
	} from "./waybill.js";
	export default {
		name: 'addWaybill',
		data() {
			return {
				senderModel: {
					name: '',
					phone: '',
					address: '',
				},
				recipientsModel: {
					name: '',
					phone: '',
					address: '',
				},
				goodsModel: {
					temperatureInterval: '',
					deliveryCondition: '',
					cargoType: '',
					remark: '',
					customerName: '',
				},
				rules: {
					name: {
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					},
					phone: {
						required: true,
						message: '请输入电话',
						trigger: ['blur', 'change']
					},
					address: {
						required: true,
						message: '请输入地址',
						trigger: ['blur', 'change']
					},
				},
				goodsRules: {
					temperatureInterval: {
						required: true,
						message: '请选择温度需求',
						trigger: ['blur', 'change']
					},
					deliveryCondition: {
						required: true,
						message: '请选择配送要求',
						trigger: ['blur', 'change']
					},
					cargoType: {
						required: true,
						message: '请选择货物类型',
						trigger: ['blur', 'change']
					},
				},
				list: pickupRulesil(),
				goodsList: formRules(),
				userInfo: {},
			}
		},
		mounted() {
			var userInfo = this.$cache.getCache('userInfo')
			this.userInfo = userInfo
		},
		methods: {
			// 打印运单
			async printWaybill() {
				let flag = await this.$refs['sender'].validateForm();
				let flag1 = await this.$refs['recipients'].validateForm();
				let flag2 = await this.$refs['goods'].validateForm();
				if (flag && flag1 && flag2) {
					let params = {
						senderAddressName: this.senderModel.name,
						senderAddressPhone: this.senderModel.phone,
						senderAddressDetails: this.senderModel.address,
						consigneeAddressName: this.recipientsModel.name,
						consigneeAddressPhone: this.recipientsModel.phone,
						consigneeAddressDetails: this.recipientsModel.address,
						...this.goodsModel,
					}
					uni.showLoading();
					if (this.userInfo.userType == 'sys') {
						this.$api.post('/api/waybill/applet', params).then(res => {
							if (res.code == 200) {
								uni.redirectTo({
									url: '/pages/order/index'
								});
							} else {
								uni.$u.toast('添加失败')
							}
							uni.hideLoading();
						}).catch(() => {
							uni.hideLoading();
						})
					} else {
						this.$api.post('/api/waybill/customer', params).then(res => {
							if (res.code == 200) {
								uni.redirectTo({
									url: '/pages/order/index'
								});
							} else {
								uni.$u.toast('添加失败')
							}
							uni.hideLoading();
						}).catch(() => {
							uni.hideLoading();
						})
					}
				} else {
					uni.$u.toast('请先完善表单')
				}
			}
		}
	}
</script>

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

	.title_waybill {
		position: relative;
		margin: 20rpx;
		border-bottom: 1rpx;
		padding-bottom: 20rpx;
	}

	.title_waybill:before {
		content: " ";
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 1px;
		border-top: 1px solid #e7e6e4;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}
</style>