<template>
	<view class="box" v-if="params">
		<view class="ddinfo">
			<view class="li">
				<text>预约院区</text>
				<view>{{merchant.title}}</view>
			</view>
			<view class="li">
				<text>预约科室</text>
				<view>{{params.departmentName}}</view>
			</view>
			<view class="li">
				<text>医生姓名</text>
				<view>{{params.sysUidName}}</view>
			</view>
			<view class="li" v-if="params.doctorTitle">
				<text>医生职称</text>
				<view>{{params.doctorTitle}}</view>
			</view>
			<!-- <view class="li">
				<text>就诊序号</text>
				<view>{{params.diagnosisNo}}</view>
			</view> -->
			<view class="li">
				<text>就诊时段</text>
				<view class="textcolor-orange">{{captureTime(params.endTime,false)}} {{getweekday(params.startTime)}}
					<view class="textcolor-orange">
						{{captureTime(params.startTime,true)}}~{{captureTime(params.endTime,true)}}
					</view>
				</view>
			</view>
			<view class="li" v-if="params.money>0">
				<text>预约费</text>
				<view class="textcolor-red">￥{{params.money}}</view>
			</view>
		</view>
		<view class="ddinfo">
			<view class="li">
				<text>姓名</text>
				<input v-model="name" placeholder="请填写预约人姓名" placeholder-class="placeholder" />
			</view>
			<view class="li">
				<text>电话</text>
				<input v-model="phone" maxlength="11" placeholder="请填写预约人电话" placeholder-class="placeholder" />
			</view>
		</view>
		<view class="regist-bottom-btn" @click="registered()">确认预约</view>
	</view>
</template>

<script>
	// mine/coupons/coupons.js
	const req = require("../../utils/request.js");
	const api = require("../../utils/api.js");
	const util = require("../../utils/util.js");
	const requestmessage = require("../../utils/requestmessage.js");
	const app = getApp();

	export default {
		data() {
			return {
				query: '',
				params: '',
				merchant: {},
				name: '',
				phone: '',
				isSubmit: false,
				orderId: '',
			};
		},

		components: {},
		props: {},
		onLoad(options) {
			this.query = options
			this.getInfo()
		},
		onShow: function() {
			this.merchant = req.getStorage('MERCHANT') ? req.getStorage('MERCHANT') : req.getStorage(
				'defaultMerchant');
			this.name = req.getStorage('userInfo').nickName
			this.phone = req.getStorage('userInfo').mobile ? req.getStorage('userInfo').mobile : ''

			// #ifndef MP-WEIXIN
			//校验支付成功
			if (this.orderId) {
				req.checkPaySuccess(this.orderId, sucs => {
					console.log('付款结果>>>>', sucs);
					this.payJumpSuc(sucs);
				});
			}
			// #endif
		},

		methods: {
			payJumpSuc(sucs) {
				if (sucs) {
					req.alertMsg('预约成功', res => {
						this.isSubmit = false;
						uni.redirectTo({
							url: '/onlineregistration/detail/detail?id=' + this.orderId
						})
					})
					this.orderId = '';
				}
			},

			captureTime(time, isTime) {
				return util.captureTime(time, isTime)
			},
			getweekday(dateString) {
				var dateArray = this.captureTime(dateString, false).split('-');
				let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
				return '星期' + '日一二三四五六'.charAt(date.getDay());
				// var weekArray = new Array("星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六")
				// var week  = weekArray[new Date(date).getDay()]
				// return week
			},
			getInfo() {
				req.postRequest(api.rostering_msg, {
					id: this.query.id,
					departmentId: this.query.departmentId
				}, data => {
					this.params = data
				})
			},
			registered() {
				let _ts = this;
				if (!this.name) return req.msg('请填写预约人姓名')
				if (!this.phone) return req.msg('请填写预约人电话')
				if (!util.isMobile(this.phone)) return req.msg('请填写正确的电话号码')
				if (this.isSubmit) return false;
				this.isSubmit = true;
				let params = {
					id: this.query.id,
					merchantId: this.merchant.id,
					diagnosisNo: this.params.diagnosisNo,
					departmentId: this.query.departmentId,
					name: this.name,
					phone: this.phone,
				}
				uni.showLoading({
					mask: true
				})
				requestmessage.registeredSuccess().then(res => {
					uni.hideLoading()
					req.postRequest(api.rostering_create, params, data => {
						// #ifdef MP-WEIXIN
						this.payOrder(data);
						// #endif
						// #ifndef MP-WEIXIN
						_ts.orderId = data;
						req.payOrder(_ts.orderId, success => {});
						// #endif
					})
				});
			},
			payOrder(id) {
				req.payOrders(id, success => {
					this.payJumpSuc(true);
				}, 'registered')
			},
			jumpUrl(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
	};
</script>

<style>
	@import './confirmation.css';
</style>