<template>
	<view class="page">
		<view class="sticky">
			<NavigateBar title="我的订单" bgcolor="#a09cc4"></NavigateBar>
			<u-tabs :list="orderTypelist" @click="tabChange" lineWidth="40" scrollable="false"
				:itemStyle="{width:'205rpx', height:'80rpx'}"></u-tabs>
		</view>
		<view class="order-item" v-for="(order,index) in orderList" :key="index">
			<view class="one-order">
				<view class="order--no" @click="gotoOrderDetail(order.orderId)">
					<view class="order--no__no">
						<view>订单号：</view>
						<view>{{ order.orderId.toUpperCase() }}</view>
					</view>
					<view class="order--no__status"></view>
				</view>
				<view class="order--room" @click="gotoOrderDetail(order.orderId)">
					<view class="order--room__pic">
						<u-image :src="getPicPath(order.roomPicPath)" width="180rpx" height="180rpx"></u-image>
					</view>
					<view class="order--room__desc">
						{{ order.roomTypeName }}
					</view>
					<view class="order--room__price" v-if="order.status=='ORDER_PLACED'||order.status=='ROOM_SELECTED'">
						{{ "￥"+ order.price }}
					</view>
				</view>
				<view class="order--duration" @click="gotoOrderDetail(order.orderId)">
					<view class="order--duration__start">
						<view class="order--duration__start-title">入住日期</view>
						<view class="order--duration__start-date">
							<view>{{ formatTime(order.startTime) }}</view>
							<view>{{ getWeek(order.startTime) }}</view>
						</view>
					</view>
					<view class="order--duration__end">
						<view class="order--duration__end-title">离店日期</view>
						<view class="order--duration__end-date">
							<view>{{ formatTime(order.endTime) }}</view>
							<view>{{ getWeek(order.endTime) }}</view>
						</view>
					</view>
					<view class="order--duration__total">
						<view>{{ `共${order.dayNum}晚` }}</view>
						<view>{{ order.breakfastNum ? `含${order.breakfastNum}早` : "不含早" }}</view>
					</view>
				</view>
				<view class="order--option">
					<view class="order--option__btn" v-if="order.status=='ORDER_PLACED'||order.status=='ROOM_SELECTED'">
						<u-button text="去入住" shape="circle" @click="gotoAddGuest(index)"></u-button>
					</view>
					<view class="order--option__btn" v-if="order.status=='CHECK_IN'">
						<u-button text="续住" shape="circle" @click="gotoExtend"></u-button>
					</view>
					<view class="order--option__btn" v-if="order.status=='CHECK_IN'">
						<u-button text="退房" shape="circle" @click="gotoExtend"></u-button>
					</view>
					<!-- <view class="order--option__btn" v-if="order.status=='FINISHED'">
						已完成
					</view> -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		stringHasText
	} from '../../utils/commonUtils.js'
	import moment from "moment"
	import {
		IMG_BASE_URL
	} from "../../config"
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import NavigateBar from '../../components/navigateBar/navigate-bar.vue';
	export default {
		components: {
			NavigateBar
		},
		data() {
			return {
				orderList: [],
				orderTypelist: [{
						index: 0,
						name: '未入住',
					},
					{
						index: 1,
						name: '已入住',
					},
					{
						index: 2,
						name: '已完成',
					}
				],
				status: ['NOT_CHECKIN', 'CHECKIN', 'FINISHED'],
				current: 0,
				pageSize: 10,
				pageNo: 1,
				total: 0,
			};
		},
		computed: {
			...mapState('m_user', ['userInfo']),
			...mapState('m_business', ['currentHotel']),
		},
		methods: {
			...mapMutations('m_business', ['updateOrder', 'updateCheckinInfo']),
			tabChange(item) {
				if (this.current != item.index) {
					this.orderList = []
					this.pageNo = 1
					this.current = item.index
					this.getOrders(item.index)
				}
			},
			gotoAddGuest(index) {
				if (new Date(this.orderList[index].endTime).valueOf() < new Date().valueOf()) {
					uni.$showMsg('订单已过期，无法入住！')
					return
				}
				this.updateOrder(this.orderList[index])
				this.updateCheckinInfo([])
				uni.navigateTo({
					url: '/subpkg_checkin/addGuest/addGuest'
				})
			},
			gotoExtend(index) {
				this.updateOrder(this.orderList[index])
				uni.navigateTo({
					url: '/subpkg/extend/extend'
				})
			},
			async getOrders(index) {
				uni.showLoading({
					title: '正在加载'
				})

				let condition = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					orderStatus: this.status[index],
					hotelId: this.currentHotel.hotelId
				}
				
				let res = await uni.$http.post(`/order/status`, condition)
				this.orderList = [...this.orderList, ...res.data.data.records]
				this.total = res.data.data.total
				uni.hideLoading()
			},
			onReachBottom() {
				if (this.orderList.length < this.total) {
					this.pageNo += 1
					this.getOrders(this.current)
				} else {
					uni.$showMsg('已经到底啦！')
				}
			},
			onPullDownRefresh() {
				this.orderList = []
				this.pageNo = 1
				//加一点延迟，不然闪的很快，看起来不舒服
				setTimeout(() => {
					this.getOrders(this.current)
					uni.stopPullDownRefresh()
				}, 100)
			},
			formatTime(text) {
				return moment(text).format("MM月DD日")
			},
			getWeek(date) {
				let week = moment(date).day()
				switch (week) {
					case 1:
						return "周一"
					case 2:
						return "周二"
					case 3:
						return "周三"
					case 4:
						return "周四"
					case 5:
						return "周五"
					case 6:
						return "周六"
					case 0:
						return "周日"
					default:
						break;
				}
			},
			getPicPath(path) {
				return IMG_BASE_URL + '/' + path
			},
			gotoOrderDetail(orderId) {
				uni.navigateTo({
					url: '/subpkg/orderDetail/orderDetail?orderId=' + orderId
				})
			}
		},
		onShow() {
			this.orderList = []
			this.getOrders(this.current)
		},
		// #ifdef MP-WEIXIN
		onShareAppMessage(info) {
			return {
				title: '源享住',
				path: 'pages/login/login',
				imageUrl: "/static/logo.png"
			}
		}
		// #endif
	}
</script>

<style lang="scss">
	page {
		background-color: #EBEAF0;
		width: 100%;

		.sticky {
			position: sticky;
			background-color: #fff;
			width: 100%;
			top: 0;
			z-index: 999;
		}

		.order-item {
			width: 100%;
			display: flex;
			flex-direction: column;

			.one-order {
				display: flex;
				flex-direction: column;
				background-color: #FFFFFF;
				margin-top: 2*5rpx;
				margin-left: 2*10rpx;
				margin-right: 2*10rpx;
				margin-bottom: 2*5rpx;
				border-radius: 2*12rpx;

				.order--no {
					display: flex;
					flex-direction: row;
					font-size: 28rpx;
					margin-top: 2*10rpx;
					margin-left: 2*10rpx;
					margin-right: 2*10rpx;
					margin-bottom: 2*5rpx;

					.order--no__no {
						display: flex;
						flex-direction: row;
					}
				}

				.order--room {
					display: flex;
					flex-direction: row;
					background-color: #F1F1F4;
					margin-left: 2*10rpx;
					margin-right: 2*10rpx;
					margin-top: 2*5rpx;
					margin-bottom: 2*5rpx;
					padding-top: 2*12rpx;
					padding-bottom: 2*12rpx;
					border-radius: 2*8rpx;

					.order--room__pic {
						padding-left: 5rpx;
						width: 190rpx;
					}

					.order--room__desc {
						display: flex;
						align-items: center;
						width: 100%;
						padding-left: 20rpx;
					}

					.order--room__price {
						display: flex;
						align-items: center;
						justify-content: end;
						width: 200rpx;
						padding-right: 20rpx;
						color: red;
						font-size: 40rpx;
					}
				}

				.order--duration {
					display: flex;
					flex-direction: row;
					background-color: #F1F1F4;
					margin-left: 2*10rpx;
					margin-right: 2*10rpx;
					margin-top: 2*5rpx;
					margin-bottom: 2*5rpx;
					border-radius: 2*8rpx;

					.order--duration__start {
						display: flex;
						flex-direction: column;
						width: 1000rpx;
						margin-top: 2*5rpx;
						margin-bottom: 2*10rpx;
						margin-left: 2*5rpx;
						margin-right: 2*5rpx;

						.order--duration__start-title {
							display: flex;
							font-size: 28rpx;
							height: 60rpx;
							align-items: center;
						}

						.order--duration__start-date {
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							height: 50rpx;
							align-items: center;
							font-size: 24rpx;
						}
					}

					.order--duration__end {
						display: flex;
						flex-direction: column;
						width: 1000rpx;
						margin-top: 2*5rpx;
						margin-bottom: 2*10rpx;
						margin-left: 2*30rpx;
						margin-right: 2*5rpx;

						.order--duration__end-title {
							display: flex;
							font-size: 28rpx;
							height: 60rpx;
							align-items: center;
						}

						.order--duration__end-date {
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							height: 50rpx;
							align-items: center;
							font-size: 24rpx;
						}
					}

					.order--duration__total {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: center;
						padding-right: 20rpx;
						font-weight: bold;
						flex-direction: column;
					}
				}

				.order--option {
					display: flex;
					justify-content: flex-end;
					flex-direction: row;
					margin-left: 2*10rpx;
					margin-right: 2*15rpx;
					margin-top: 2*5rpx;
					margin-bottom: 2*5rpx;

					.order--option__btn {
						margin-left: 20rpx;
						width: 160rpx;

						.u-button {
							height: 60rpx;
							border: 2rpx solid #9e97c3;
							color: #9e97c3;
						}

						.u-button--active {
							background-color: #9e97c3;
							color: #FFFFFF;

							&:before {
								opacity: 0;
							}
						}
					}
				}
			}

			.card-item {
				margin-top: 10rpx;
				margin-bottom: 10rpx;
				display: flex;

				.card-item-left {
					width: 140rpx;
				}
			}
		}
	}
</style>