<template>
	<view>
		<!-- 位置 -->
		<view class="box tap-area" v-if="merchant&&merchant.title" @click="jumpUrl('/pages/nearby/nearby')">
			<view class="header" v-if="merchant.logo">
				<image :src="merchant.logo" mode="aspectFill"></image>
			</view>
			<view class="header-title">
				<view class="header-name">{{merchant.title}}</view>
				<view class="header-text" v-if="merchant.range && merchant.range!=50000"><text>距离您</text>{{merchant.range ? merchant.mDistance < 1000 ? merchant.mDistance+'m' : merchant.range+'km' : ''}}</view>
			</view>
			<view class="header-more">
				<image src="../../static/pages/images/more.png" class="more"></image>
			</view>
		</view>
		<!-- 选择服务 -->
		<view class="service">
			<view class="service-title">请选择您要预约的服务</view>
			<view class="service-bar">
				<view :class="'service-item'+(serviceIndex==index ? ' active':'')" v-for="(item,index) in pageList" :key="index" @click="selectService(item,index)">
					<image src="../static/onlineregistration/image/tooth.png"></image>
					<view class="service-name fflex">{{item.title}}</view>
				</view>
			</view>
		</view>
		<!-- 信息填写 -->
		<view class="ddinfo">
			<view class="li ddflex">
				<text>姓名</text>
				<input v-model="info.name" placeholder="请输入您的姓名" class="fflex"/>
			</view>
			<view class="li ddflex">
				<text>电话</text>
				<input v-model="info.phone" placeholder="请输入您的手机号" class="fflex"/>
			</view>
			<view class="li ddflex">
				<text>时间</text>
				<view>
					<picker mode="date" :value="date" @change="bindDateChange">
						<view class="flex">{{ date ? date : '请选择时间' }}</view>
					</picker>
				</view>
				<image src="/static/pages/images/more.png" class="rico"></image>
			</view>
			<view class="li">
				<text>症状描述</text>
				<textarea placeholder="请输入症状描述" placeholder-class="placeholder" v-model="info.brief" class="textarea"></textarea>
			</view>
		</view>
		<view class="regist-bottom-btn" @tap="nextStep">下一步</view>
	
		<!-- 确认信息 -->
		<view v-show="showPop" class="confirmation-bar">
			<view class="pop-title">预约信息</view>
			<view class="pop-content">
				<view class="ddinfo">
					<view class="li ddflex">
						<text>姓名</text>
						<view>{{info.name}}</view>
					</view>
					<view class="li ddflex">
						<text>电话</text>
						<view>{{info.phone}}</view>
					</view>
					<view class="li ddflex">
						<text>预约院区</text>
						<view>{{merchant.title}}</view>
					</view>
					<view class="li ddflex">
						<text>预约服务</text>
						<view>{{service.title}}</view>
					</view>
					<view class="li ddflex">
						<text>预约时间</text>
						<view>{{date}}</view>
					</view>
					<view class="li ddflex">
						<text>症状描述</text>
						<view>{{info.brief}}</view>
					</view>
				</view>
			</view>
			<view class="pop-btn">
				<view class="pop-btn-cancal" @tap="popCancal">取消</view>
				<view class="pop-btn-submit" @tap="popSubmit">确认预约</view>
			</view>
		</view>
		<!-- 遮罩层 -->
		<view v-show="showPop" class="mask"></view>
	</view>
</template>

<script> 
	const req = require("../../utils/request.js");
	const api = require("../../utils/api.js");
	const app = getApp();
	export default {
		data() {
			return {
				merchant: {},
				pageList: [],
				date: '',
				serviceIndex: 0,
				service: {},
				showPop: false,
				info:{
					name:'',
					phone:'',
					brief:''
				},
				isSubmit: true,
				params: {}
			};
		},

		components: {},
		props: {},
		onLoad() {
			this.getMerchant()
			this.getPageList()
		},
		onShow: function() {
		},

		methods: {
			getMerchant(){
				if(req.getStorage('MERCHANT')){
					this.merchant = req.getStorage('MERCHANT')
				}else{
					this.merchant = req.getStorage('defaultMerchant')
				}
			},
			getPageList(){
				req.getRequest(api.reservation_service_list,{merchantId: this.merchant.id},data=>{
					this.pageList = data;
					if(data&&data.length>0){
						this.service = data[0]
					}
				})
			},
			
			jumpUrl(url) {
				uni.navigateTo({
					url: url
				})
			},
			bindDateChange(e) {
			   this.date = e.detail.value
			},
			selectService(item,index){
				this.serviceIndex = index;
				this.service = item
			},
			nextStep(){
				let params = this.info
				params.appointmentTime = this.date
				params.productId = this.service.id
				params.merchantId = this.merchant.id
				if(!params.name) return req.msg('请输入您的姓名')
				if(!params.phone) return req.msg('请输入您的手机号码')
				if(!params.appointmentTime) return req.msg('请选择预约时间')
				if(!params.brief) return req.msg('请输入症状描述')
				this.showPop = true
				this.params = params
			},
			popCancal(){
				this.showPop = false
			},
			popSubmit(){
				if(!this.isSubmit) return false;
				req.postRequest(api.reservation_service_create,this.params,data=>{
					req.msg('预约成功')
					this.popCancal()
					this.isSubmit = true;
					setTimeout(suc=>{
						this.jumpUrl('/onlineregistration/appointmentlist/appointmentlist')
					},200)
				})
			}
		}
	};
</script>

<style>
	@import './appointment.css'
</style>