<template>
	<view>
		<z-navbar :title="(type==0?'新建':'编辑')+'婚庆预定'" back>
			<view slot="right" @click="submit">保存</view>
		</z-navbar>
		<z-form :model="info" :rules="rules" ref="info">
			<z-form-item label="新郎姓名" prop="maleName">
				<image slot="icon" class="common_icon" src="../../../static/icon_nv.png"></image>
				<z-input v-model="info.maleName" placeholder="点击填写"></z-input>
			</z-form-item>
			<z-divider></z-divider>
			<z-form-item label="新娘姓名" prop="femaleName" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_nan.png"></image>
				<z-input v-model="info.femaleName" placeholder="点击填写"></z-input>
			</z-form-item>
			<z-form-item label="手机号码" prop="phoneMale" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_phone.png"></image>
				<z-input type="number" v-model="info.phoneMale" placeholder="点击填写" maxlength="11"></z-input>
			</z-form-item>
			<z-form-item label="其他手机号码" prop="phoneFemale" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_phone.png"></image>
				<z-input type="number" v-model="info.phoneFemale" placeholder="点击填写"></z-input>
			</z-form-item>
			<z-form-item label="典礼开始时间" prop="beginTime" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_time.png"></image>
				<z-text v-model="info.beginTime" placeholder="选择典礼开始时间" @click="showBeginTime=true"></z-text>
				</picker>
			</z-form-item>
		    <z-time-select :visible.sync="showBeginTime"  v-model="info.beginTime" :start="beginTime||'07:00'" :end="endTime||'24:00'"></z-time-select>
			<z-form-item label="婚庆套餐" prop="weddingPackageId" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_dui.png"></image>
				<wedding-package type="wedding" v-model="info.weddingPackageId" placeholder="选婚庆套餐" @change="changeWedPackage"></wedding-package>
			</z-form-item>
			<z-form-item label="增加项目" prop="exItemMoney" arrow showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_jia.png"></image>
				<z-text :value="(info.exItemMoney||0)+'元'" placeholder="选择增加项目" @click="addItem=true"></z-text>
				<extra-item isWedding title="增加项目" v-model="info.weddingExtra" :visible.sync="addItem" @getTotal="getExtraTotal"></extra-item>
			</z-form-item>
			<z-form-item label="婚庆价格" prop="weddingMoney" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_qian.png"></image>
				<z-input type="number" v-model="info.weddingMoney" placeholder="¥"></z-input>
			</z-form-item>
			<z-form-item label="订金(元)" prop="downPayment" showline>
				<image slot="icon" class="common_icon" src="../../../static/icon_money.png"></image>
				<z-input type="number" v-model="info.downPayment" placeholder="¥"></z-input>
			</z-form-item>
			<z-form-item label="婚庆接待人员" bgcolor="#eee" :labelStyle="{color:'#999'}" prop="createUserName">
				<image slot="icon" class="common_icon" src="../../../static/icon_person.png"></image>
				<z-text textColor="#999" :value="info.createUserName"></z-text>
			</z-form-item>
			<z-divider></z-divider>
			<z-form-item label="备注信息" prop="remark" labelPos="top">
				<textarea class="textarea" v-model="info.remark" placeholder="请输入备注信息" maxlength="512" auto-height></textarea>
			</z-form-item>
		</z-form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	const app = getApp({
		allowDefault: true
	});
	import extraItem from '../../component/extraItem/extraItem.vue';
	import weddingPackage from '../../component/package/package.vue'
	export default {
		components: {
			extraItem,
			weddingPackage
		},
		data() {
			var showtext = (rule, value, callback) => {
				//validator
				return {
					state: false,
					messages: '输入错误'
				};
			};
			return {
				addItem: false,
				showBeginTime: false,
				beginTime:'',
				endTime:'',
				id: '',
				orderId: '',
				type: 0,
				info: {
					maleName: '',
					femaleName: '',
					phoneMale: '',
					phoneFemale: '',
					weddingMoney: '',
					weddingPackageId: '',
					exItemMoney: '', //额外项目金额
					downPayment: '',
					beginTime: '',
					"packageName": '',
					"packageId": '',
					"weddingItemsExs": [], //婚庆套餐项目
					weddingExtra: [],
					"remark": ''
				},
				rules: {
					maleName: [{
						required: true,
						message: ''
					}],
					femaleName: [{
						required: true,
						message: ''
					}],
					weddingMoney: [{
						required: true,
						message: ''
					}],
					downPayment: [{
						required: true,
						message: ''
					}]
				}
			};
		},

		computed: {

		},
		onLoad(option) {
			this.id = option.wedId || '';
			this.orderId = option.orderId || '';
			this.type = option.type;
			this.beginTime=option.beginTime;
			this.endTime=option.endTime;
			if (this.type == 1)
				this.getData(option.wedId);
			else {
				this.getdownPayment();
			}
		},
		methods: {
			getData(wedId) {
				this.$axios.get('app/wedding/orderWeddingDetail/' + wedId).then(res => {
					console.log(res)
					var info = res.data;
					// if(info.beginTime)
					// info.beginTime = info.beginTime.substring(0, info.beginTime.length - 3)||''
					info.weddingItems = info.weddingItemsExs || []

					info.weddingExtra = info.extra || [];
					for (var i = 0; i < info.weddingItems.length; i++) {
						info.weddingItems[i].id = info.weddingItems[i].itemId;
						info.weddingItems[i].itemCount = Number(info.weddingItems[i].itemCount).toFixed(0);
					}
					for (var i = 0; i < info.weddingExtra.length; i++) {
						info.weddingExtra[i].id = info.weddingExtra[i].itemId;
						info.weddingExtra[i].itemCount = Number(info.weddingExtra[i].itemCount).toFixed(0);
					}
					this.info = info;
				}).catch(err => {
					console.log(err)
					this.$refs.uToast.error(err.msg);
				})
			},
			//新增时获取默认定金
			getdownPayment() {
				this.$axios.get('app/config/baseByStoreId/' + uni.getStorageSync('storeId')).then(res => {
					console.log('定金',res.data.orderDefaultMoney)
					this.$set(this.info, 'downPayment', res.data.orderDefaultMoney || 0);

				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},
			getExtraTotal(data) {
				console.log(data)
				this.$set(this.info, 'exItemMoney', data.realMoney)
			},
			changeWedPackage(data) {
				this.info.weddingPackageName = data.packageName;
				this.$axios.post('app/foodPackage/foodPackageByIdAndType', {
					"id": this.info.weddingPackageId,
					"packageTypeCode": '02'
				}).then(res => {
					console.log(res)
					this.info.weddingItems = res.data.itemsWeddingTree || []


				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},
			submit() {
				this.$refs.info.validate(val => {
					console.log(val)
					if (val) {
						var info = this.info;
						for (var i = 0; i < info.weddingExtra.length; i++) {
							info.weddingExtra[i].itemId = info.weddingExtra[i].id;
						}
						if (this.type == 0) {
							this.addSubmit();
						} else {
							this.editSubmit();
						}
					} else {
						this.$refs.uToast.error('表单填写有误');
					}
				})
			},
			addSubmit() {
				var info = this.info;
				console.log(info)

				this.$axios.post('app/wedding/orderWeddingAdd', {
					"storeId": uni.getStorageSync('storeId'),
					"orderId": this.orderId,
					"maleName": info.maleName,
					"femaleName": info.femaleName,
					"phoneMale": info.phoneMale,
					"phoneFemale": info.phoneFemale,
					"beginTime": info.beginTime || null,
					"packageName": info.weddingPackageName,
					"packageId": info.weddingPackageId,
					"exItemMoney": info.exItemMoney, //额外项目金额
					"weddingItemsExs": info.weddingItems, //婚庆套餐项目
					"extra": info.weddingExtra, //已点增加项目列表
					"orderMoney": info.weddingMoney,
					"downPayment": info.downPayment,
					"remark": info.remark
				}).then(res => {

					this.$refs.uToast.success('新增婚庆预定成功！');
					uni.navigateBack({
						delta: 1
					})
				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},
			editSubmit() {
				var info = this.info;
				console.log(info)

				this.$axios.post('app/wedding/orderWeddingUpdate', {
					"storeId": uni.getStorageSync('storeId'),
					"id": info.id,
					"weddingOrderId": info.weddingOrderId,
					"orderId": info.orderId,
					"maleName": info.maleName,
					"femaleName": info.femaleName,
					"phoneMale": info.phoneMale,
					"phoneFemale": info.phoneFemale,
					"beginTime": info.beginTime,
					"packageName": info.weddingPackageName,
					"packageId": info.weddingPackageId,
					"exItemMoney": info.exItemMoney, //额外项目金额
					"weddingItemsExs": info.weddingItems, //婚庆套餐项目
					"extra": info.weddingExtra, //已点增加项目列表
					"orderMoney": info.weddingMoney,
					"downPayment": info.downPayment,
					"remark": info.remark
				}).then(res => {
					this.$refs.uToast.success('编辑婚庆预定成功！');
					uni.navigateBack({
						delta: 1
					})

				}).catch(err => {
					console.log(err.msg)
					this.$refs.uToast.error(err.msg);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order_package_manage {
		background: #0075FF;
		color: #0075FF;
		width: 34rpx;
		height: 34rpx;
	}

	.textarea {
		font-size: 28rpx;
		min-height: 80rpx;
		padding-bottom: 20rpx;
		width: 100%;
	}

	.common_icon {
		width: 58rpx;
		height: 58rpx;
		min-width: 58rpx;
		margin-right: 30rpx;
	}
</style>
