<template>
	<view class="wrap">
		<view class="list">
			<view class="list-item" v-for="(item,index) in orders" :key="index">
				<view class="list-item-top" @click="toorder(item.id)">
					<view class="status">
						<text class="statusName">{{item.status_name}}</text>
					</view>
					<view>订单编号：{{item.serial}}</view>
					<view>{{item.bed.building.name}}-{{item.bed.room.name}}-{{item.bed.name}}</view>
				</view>
				<view class="list-item-body" @click="toorder(item.id)">
					<view class="">
						<view>
							<text>{{item.project.name}}</text>
							{{item.product.name}}
						</view>
						<view>{{item.patient.name}}</view>
					</view>
					<view class="">
						<view>预约：{{item.from_date}}</view>
						<view>X{{item.days}}</view>
					</view>
					<view>
						<view>合计：¥{{item.total}}</view>
						<view>已扣：¥{{item.paid_total}}</view>
						<view>余额：¥{{item.customer.balance}}</view>
					</view>
					<view>
						<view>
							<text v-for="(mod,i) of item.factorsJson">{{mod.factor_item_name}}</text>
						</view>
					</view>

				</view>
				<view class="list-item-foot">
					<view class="cancel" v-if="item.status==0" :data-id="item.id" @click="toCancel">取消</view>
					<view class="" @click="toPay" :data-id="item.id" v-if="item.status==20">立即付款</view>
					<view class="judge" v-if="!item.scored_at&&item.status==100" :data-id="item.id" @click="toJuge">评价</view>
				</view>
			</view>
		</view>
		<tabbar :current-page="3"></tabbar>
	</view>
</template>

<script>
	import tabbar from "../../components/tabbar/tabbar.vue";
	import util from "../../utils/util.js";
	export default {
		components: {
			tabbar
		},

		data() {
			return {
				currentPage: 1,
				orders: [],
				loading: true
			}

		},

		onLoad: function(e) {},

		onShow: function() {
			this.loadPage(1);
		},

		onReady: function() {},
		onHide: function() {},
		onUnload: function() {},

		onPullDownRefresh: function() {
			this.loadPage(1);
		},

		onReachBottom: function() {
			this.loadPage(this.currentPage + 1);
		},

		methods: {
			getErrorMessage: function(err, fallback) {
				fallback = fallback || '支付失败，请稍后重试';
				if (!err) return fallback;
				if (typeof err === 'string') return err;
				if (err.errormsg) return err.errormsg;
				if (err.errorMsg) return err.errorMsg;
				if (err.message) return err.message;
				if (err.data && err.data.errormsg) return err.data.errormsg;
				if (err.data && err.data.message) return err.data.message;
				try {
					return JSON.stringify(err);
				} catch (e) {
					return fallback;
				}
			},
			toorder(id) {
				uni.navigateTo({
					url: "/packages/packagesOrder/pages/orderInfo/index?id=" + id
				})
			},
			loadPage: function(o) {
				var that = this;
				util.request({
					bindThis: that,
					api: 'customer/get-orders',
					customLoading: false,
					data: {
						page: o,
						page_size: 10
					},
					utilSuccess: function(n) {
						var i = n.data;
						console.log(i);
						uni.stopPullDownRefresh();
						if ((that.orders.length < 10 && o > 1) || (0 == i.length && o > 1)) {
							uni.showToast({
								title: '已加载到最后一页',
								icon: 'none'
							});
						} else {
							var s;
							var l;
							var u = that.orders;
							var c = Math.floor(new Date().getTime() / 1000);
							var d = i;
							try {
								for (var g of d) {
									g.statusStr = util.orderStatusDic[g.status];
									g.ThumbUrl = util.isNull(g.ThumbUrl) ? '/resources/nonepro.jpg' : '';
									var f = g.from_date.replace(/-/g, '/').replace(/T/g, ' ');
									g.isPreParamedic = Math.floor(new Date(f).getTime() / 1000) > c;

									g.factorsJson = JSON.parse(g.factors.replace(/\\/g, ''));
									// 定义两个日期
									const date1 = that.$moment(g.from_date);
									const date2 = that.$moment(g.to_date);
									// 计算差异
									const duration = that.$moment.duration(date2.diff(date1));
									console.log(duration)
									g.days = duration.days() + 1;

								}
							} catch (e) {

							} finally {

							}
							if (1 == o) {
								u = i;
							} else {
								(l = u).push.apply(l, e(i));
							}
							that.currentPage = o;
							that.orders = u;
							that.loading = false;
						}
					},
					utilFail: function(e) {
						uni.stopPullDownRefresh();
						if (1 == o) {
							that.currentPage = o;
							that.orders = [];
						}
						util.alert(e);
					}
				});
			},

			toPay: function(e) {
				var that = this;
				var t = e.currentTarget.dataset.id;
				util.request({
					api: 'customer/get-order/' + t,
					utilSuccess: function(e) {
						var o = (e.total - e.balance - e.paid_total).toFixed(2);
						util.payOrder(
							o,
							t,
							function(e) {
								that.loadPage(1);
							},
							function(e) {
								util.alert(that.getErrorMessage(e, '支付失败，请稍后重试'));
							}
						);
					},
					utilFail: function(e) {
						util.alert(e);
					}
				});
			},

			toJuge: function(e) {
				var t = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: "/packages/packagesOrder/pages/judge/index?id=" + t
				});
			},

			toCancel: function(e) {
				var that = this;
				var o = e.currentTarget.dataset.id;
				uni.showModal({
					title: '提示',
					content: '取消订单',
					confirmText: '确认',
					confirmColor: '#000',
					cancelColor: '#eee',
					success: function(e) {
						e.confirm ?
							util.request({
								bindThis: this,
								customLoading: true,
								api: 'customer/cancel-order/' + o,
								data: {
									id: o
								},
								method: 'POST',
								utilSuccess: function(e) {
									that.loadPage(1);
								},
								utilFail: function(e) {
									util.alert(e);
								}
							}) :
							e.cancel;
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.wrap {
		background-color: #E7F5F4;
		width: 100vw;
		padding-bottom: 200rpx;

		.list {
			padding: 30rpx;
			font-size: 24rpx;

			&-item {
				background: #FFFFFF;
				box-shadow: 2rpx 0rpx 20rpx 0rpx #ddd;
				border-radius: 4rpx 4rpx 4rpx 4rpx;
				margin-bottom: 30rpx;
				position: relative;

				&-top {
					color: #838180;
					background: #FAFAFA;
					display: flex;
					justify-content: space-between;
					padding: 0 20rpx;
					height: 70rpx;
					line-height: 70rpx;
					position: relative;

					.status {
						&::before {
							color: #fff;
							content: '';
							position: absolute;
							top: 0;
							left: 0;
							width: 0;
							height: 0;
							border-top: 35rpx solid orangered;
							border-right: 35rpx solid transparent;
							border-left: 35rpx solid orangered;
							border-bottom: 35rpx solid transparent;
						}

						text {
							transform: rotate(313deg);
							display: block;
							color: #fff;
							font-size: 16rpx;
							position: relative;
							left: -23rpx;
							top: -12rpx;
						}
					}
				}

				&-body {
					padding: 30rpx;
					border-bottom: 1px solid #eceded;
					padding-bottom: 20rpx;

					>view {
						display: flex;
						justify-content: space-between;
						color: #838180;
						margin-bottom: 10rpx;

						&:first-child {
							font-size: 26rpx;
						}

						text {
							color: #f96f40;
							margin-right: 20rpx;
						}
					}
				}

				&-foot {
					padding: 30rpx;
					display: flex;
					justify-content: flex-end;

					view {
						border-radius: 25rpx 25rpx 25rpx 25rpx;
						// padding: 10rpx 20rpx;
						background: linear-gradient(to right, #0EE2BA 0%, #67F7DC 100%);
						color: #fff;
						width: 140rpx;
						text-align: center;
						height: 54rpx;
						line-height: 54rpx;
						margin-left: 20rpx;
					}

					.cancel {
						background: #fff;
						color: #838180;
						border: 2rpx solid #838180;
					}

					.judge {
						background: rgba(247, 114, 63, 1);
						color: #fff;
						border: 2rpx solid #rgba(247, 114, 63, 1);
					}
				}
			}
		}
	}
</style>