<template>
	<view class="search">
		<view class="house">
			<view class="list">
				<view class="item">
					<image class="image" :src="form.files[0].url">
					</image>
					<view class="info">
						<view class="title">{{ form.houseTitle }}</view>
						<view class="desc"><text>{{ form.extent }}m²|{{ form.toward }}</text></view>
						<view class="price">{{ form.monthlyRent }}元/月</view>
					</view>
				</view>
			</view>
		</view>
		<u-gap></u-gap>
		<!-- 提交表单 -->
		<view class="user-card">
			<u--form :model="form" ref="uForm" :rules="rules" labelPosition="top" :labelStyle="{paddingLeft: '10rpx'}"
				label-width="200rpx">
				<!-- 表单组件 -->
				<view class="form-wrapper">
					<u-cell-group :border="false">
						<u-form-item prop="realName">
							<u-cell title="姓名" :isLink="true">
								<u-input slot="value" class="input" v-model="form.realName" inputAlign="right"
									maxlength="30" :border="false" placeholder="请输入姓名" />
							</u-cell>
						</u-form-item>
						<u-form-item prop="phone">
							<u-cell title="联系电话" :isLink="true">
								<u-input slot="value" class="input" v-model="form.phone" inputAlign="right"
									maxlength="30" :border="false" placeholder="请填写联系电话" />
							</u-cell>
						</u-form-item>
						<u-form-item prop="expirationTime">
							<u-cell title="预约看房时间" :isLink="true" @click="showExpirationTime = true">
								<u-input slot="value" class="input" v-model="expirationTime" inputAlign="right"
									maxlength="30" :border="false" placeholder="请选择看房时间" />
							</u-cell>
						</u-form-item>
					</u-cell-group>
				</view>
				</block>
			</u--form>
		</view>
		<view class="float">
			<view class="release">
				<u-button text="提交预约" type="primary" shape="circle" @click="handleSubmit"></u-button>
			</view>
		</view>
		
		<!-- 组件 -->
		<u-calendar :show="showExpirationTime" @confirm="confirmexpirationTime"></u-calendar>
		<!-- <u-datetime-picker ref="datetimePicker" :show="showExpirationTime" v-model="expirationTime" mode="date" @close="closeexpirationTime"
			@confirm="confirmexpirationTime" @cancel="closeexpirationTime" :closeOnClickOverlay="true">
		</u-datetime-picker> -->
	</view>
</template>

<script>
	import store from '@/store'
	import {
		fileUrl
	} from '@/config.js'
	import {
		dateFormat
	} from '@/utils/util.js'
	import * as HouseInfoApi from '@/api/house-info.js'
	import * as HouseReservationApi from '@/api/house-reservation.js'
	import * as DictApi from '@/api/dict.js'
	

	export default {
		data() {
			return {
				select: [],
				bgColor: '#ffffff',
				showExpirationTime: false,
				form: {
					name: '',
					phone: '',
					expirationTime: ''
				},
				// 生日
				expirationTime: '',
				// 验证规则
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写您的姓名',
						trigger: ['blur', 'change']
					},
					'phone': {
						type: 'string',
						required: true,
						message: '请填写您的手机号码',
						trigger: ['blur', 'change']
					},
					'expirationTime': {
						type: 'string',
						required: true,
						message: '请选择预约看房时间',
						trigger: ['blur', 'change']
					},
				},
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.houseId = options.id
			this.getHouseInfo()
		},

		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			// this.$refs.uForm.setRules(this.rules)
			// 微信小程序需要用此写法
			// this.$refs.datetimePicker.setFormatter(this.formatter)
		},

		methods: {
			getHouseInfo() {
				const app = this
				const {
					houseId
				} = this
				HouseInfoApi.getHouseInfo(houseId).then(res => {
					app.form = res.data
					app.form.files = JSON.parse(res.data.files) || []
					app.form.houseLabel = JSON.parse(res.data.houseLabel) || []
					// app.form.supporting = JSON.parse(res.data.supporting) || []
				}).catch(err => {
					app.$error(err.message)
				})
			},
			onSearch() {
				this.$push('/sub_pages/member/member', this.where)
			},
			onBadge(id) {
				this.bgColor = '#ff0000'
			},
			confirmexpirationTime(e) {
				this.expirationTime = e[0]
				this.form.expirationTime = e[0] + ' 00:00:00'
				this.showExpirationTime = false
			},
			closeexpirationTime() {
				this.showExpirationTime = false
			},
			// 确认修改
			async handleSubmit() {
				const app = this
				const { form } = this
				if (app.disabled === true) return
				this.$refs.uForm.validate().then(() => {
					HouseReservationApi.addHouseReservation(app.form).then(result => {
						app.$toast(result.message)
					}).catch(err => {
						uni.$u.toast(err.message)
					})
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			},
		}
	}
</script>

<style>
	page {
		background: #f7f8fa;
	}
</style>
<style lang="scss" scoped>
	.container {}


	.search-wrapper {
		display: flex;
		height: 64rpx;
		padding: 20rpx;
		background-color: #FFFFFF;
	}

	// 搜索输入框
	.search-input {
		width: 80%;
		background: #fff;
		height: 72rpx;
		line-height: 72rpx;
		border-radius: 10rpx 0 0 10rpx;
		box-sizing: border-box;
		overflow: hidden;

		.search-input-wrapper {
			display: flex;

			.left {
				display: flex;
				width: 60rpx;
				justify-content: center;
				align-items: center;

				.search-icon {
					display: block;
					color: #b4b4b4;
					font-size: 28rpx;
				}

			}

			.right {
				flex: 1;

				input {
					font-size: 28rpx;
					height: 72rpx;
					line-height: 72rpx;
					display: flex;
					align-items: center;

					.input-placeholder {
						color: #aba9a9;
					}
				}

			}
		}
	}

	// 搜索按钮
	.search-button {
		width: 20%;
		box-sizing: border-box;

		.button {
			height: 64rpx;
			font-size: 28rpx;
			border-radius: 0 10rpx 10rpx 0;
			background: $main-bg;
			color: $main-text;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	// 订单合计
	.order-total {
		margin: 10rpx auto;
		font-size: 26rpx;
		vertical-align: bottom;
		text-align: right;
		height: 50rpx;
		padding-top: 10rpx;
		display: flex;
		justify-content: center;

		.pay-btn {
			display: flex;

			.btn {
				margin-right: 16rpx;
			}
		}

		.total-price {
			display: flex;
			align-items: center;
		}

		.unit {
			margin-left: 8rpx;
			margin-right: -2rpx;
			font-size: 26rpx;
		}

		.money {
			font-size: 28rpx;
			color: #ff0000;
		}
	}

	.house {
		margin: 30rpx auto;
		width: 680rpx;

		.btn {
			width: 150rpx;
			margin: 40rpx 0;
		}

		.list {
			width: 680rpx;
			margin: auto;

			.item {
				border-radius: 30rpx 30rpx 0 0;
				box-shadow: 0 3rpx 10rpx 0px #999999;
				background-color: #FFFFFF;
				margin: 50rpx auto;
				position: relative;

				.badge {
					position: absolute;
					left: 30rpx;
					top: 30rpx;
				}

				.image {
					width: 680rpx;
					height: 360rpx;
					border-radius: 30rpx 30rpx 0 0;
				}

				.info {
					padding: 20rpx 20rpx;
					display: flex;
					flex-direction: column;

					.title {
						font-size: 40rpx;
					}

					.desc {
						color: #999999;
						font-size: 28rpx;
					}

					.price {
						color: #ff0000;
						font-size: 30rpx;
					}
				}
			}
		}
	}

	.user-card {
		width: 660rpx;
		background-color: #FFFFFF;
		border-radius: 15rpx;
		margin: auto;
		padding: 20rpx;
		z-index: 100;
	}

	.float {
		width: 750rpx;
		display: flex;
		justify-content: center;
		position: fixed;
		bottom: 100rpx;

		.release {
			width: 500rpx;
			opacity: 0.9;
		}
	}
</style>