<template>
	<view class="container">
		<view class="tabs">
			<u-tabs :list="tabs" @click="onChangeTab" lineColor="#ff0000"></u-tabs>
		</view>
		<!-- 订单列表 -->
		<view class="order-list" v-if="list">
			<view class="order-item" v-for="(item, index) in list" :key="index">
				<view class="item-top">
					<view class="item-top-left">
						<text class="order-time">
							{{ item.planName }}
						</text>
					</view>
					<view class="item-top-right">
						<u-tag text="待支付" plain size="mini" type="warning" v-if="item.payStatus == 10"></u-tag>
						<u-tag text="已支付" plain size="mini" type="success"
							v-else="item.payStatus == 20 && item.receiptStatus == 10"></u-tag>
					</view>
				</view>
				<u-divider></u-divider>
				<!-- 商品列表 -->
				<view class="goods-list">
					<block>
						<view class="goods-item">
							<view class="icon">
								<image :src="item.planIcon" mode="widthFix"></image>
							</view>
							<view class="goods-content">
								<text class="goods-title">订单号：{{ item.logId }}</text>
								<text class="goods-title">描述：{{ item.priceName }}</text>
								<text class="goods-title">价格：￥{{ item.money }}</text>
								<text class="goods-title">时间：{{ item.createTime }}</text>
								<text class="goods-title">状态：{{ item.isSettled == 1 && item.status == 0 ? '已生效' : '处理中' }}</text>
								<block v-if="item.planId == 17 || item.planId == 18">
									<text class="goods-title">服务门店：{{ item.merchantName }}</text>
									<text class="goods-title">有效期至：{{ item.expirationTime }}</text>
									<text class="goods-title">门店地址：{{ item.address }}</text>
								</block>


							</view>
						</view>
					</block>
				</view>
				<u-divider></u-divider>
				<!-- 订单合计 -->
				<view class="order-total">
					<view class="pay-btn">
						<view class="btn" v-if="item.payStatus == 10">
							<u-button text="立即支付" plain type="error" size="small" @click="onPay(item.logId)"></u-button>
						</view>
						<!-- <view class="btn" v-if="item.payStatus == 10">
							<u-button text="取消订单" size="small" @click="onRemove(item.logId)"></u-button>
						</view> -->
					</view>
					<view class="total-price">
						<text>合计</text>
						<text class="unit" style="color: #ff0000;">￥</text>
						<text class="money">{{ item.money }}</text>
					</view>
				</view>

			</view>
		</view>
		<u-loadmore v-if="list.length > 0" :status="status" />
		<u-empty mode="order" icon="http://cdn.uviewui.com/uview/empty/car.png" v-if="list.length == 0">
		</u-empty>
		<u-gap height="20"></u-gap>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import * as UserPlanLogApi from '@/api/love-user-plan-log.js'
	import {
		dateFormat,
		getWeek
	} from '@/utils/util.js'

	// 每页记录数量
	const pageSize = 10

	// tab栏数据
	const tabs = [{
		name: `我的订单`,
		value: 'delivery'
	}]

	export default {
		data() {
			return {
				// 当前页面参数
				options: {
					dataType: 'all'
				},
				// tab栏数据
				tabs,
				// 当前标签索引
				curTab: 0,
				// 订单列表数据
				list: [],
				total: 0,
				loadMore: true,
				status: '加载更多',
				page: 1,
				where: {
					userId: 0
				},
				// 选择的设备
				getWeek,
				dateFormat
			}
		},

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

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
		},
		// 触底函数
		onReachBottom() {
			const app = this
			if (app.loadMore) {
				app.page = ++app.page;
				app.onRefreshList()
			}
		},
		onPullDownRefresh() {
			const app = this
			app.page = 1
			app.list = []
			app.onRefreshList();
			uni.stopPullDownRefresh();
		},

		methods: {
			// 切换标签项
			onChangeTab(e) {
				const app = this
				// 设置当前选中的标签
				app.curTab = e.index
				app.page = 1
				app.list = []
				// 刷新订单列表
				app.onRefreshList()
			},

			// 刷新订单列表
			onRefreshList() {
				const app = this
				const {
					curTab
				} = this
				app.where.showGoodsList = true
				app.where.page = app.page
				app.where.payStatus = 20;
				app.where.receiptStatus = 10;
				app.where.agent = undefined
				console.log("curTab: ",curTab);
				if (curTab == 0) {
					// app.where.agent = undefined
					// app.where.payStatus = 20;
					// app.where.receiptStatus = 10;
				}
				if (curTab == 1) {
					app.where.agent = true
					// app.where.payStatus = 20;
					// app.where.receiptStatus = 10;
				}
				const userId = uni.getStorageSync('userId')
				if(userId && userId > 0){
					app.where.userId = userId
					// 只查询已支付订单
					UserPlanLogApi.pageUserPlanLog(app.where).then(res => {
						const newList = res.data.list
						if (newList.length > 0) {
							app.list = app.list.concat(newList)
						} else {
							app.status = '没有更多了'
							app.loadMore = false
						}
					})
				}
				// if (curTab == 2) {
				// 	app.where.agent = undefined
				// 	app.where.payStatus = 20;
				// 	app.where.receiptStatus = 10;
				// }
				
			},

			// 取消订单
			onCancel(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '确认要取消该订单吗？',
					success(o) {
						if (o.confirm) {
							OrderApi.removeOrder(orderId)
								.then(result => {
									// 显示成功信息
									app.$toast(result.message)
									// 刷新订单列表
									app.list = []
									app.onRefreshList()
								})
						}
					}
				});
			},

			onCancelOne(item) {
				const app = this
				// 报餐截止时间
				let date = new Date();
				if (date.getHours() > 20 && date.getMinutes() > 30) {
					return app.$toast('报餐截止时间')
				}
				OrderGoodsApi.cancelfood(item).then(res => {
					app.$toast(res.message)
					// 刷新订单列表
					app.list = []
					app.onRefreshList()
				})
			},
			// 点击去支付
			onPay(orderIds) {
				this.$navTo('pages/checkout/cashier/index', {
					orderIds
				})
			},

			// 跳转到订单详情页
			handleTargetDetail(orderId) {
				this.$navTo('pages/order/detail', {
					orderId
				})
			},

			// 跳转到订单评价页
			handleTargetComment(orderId) {
				this.$navTo('pages/order/comment/index', {
					orderId
				})
			},

			getDeliveryTime(time) {
				return dateFormat('YYYY-mm-dd', new Date(new Date(time).toLocaleDateString()));
			}

		},

	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #f3f3f3 !important;
	}
	.container {
		width: 750rpx;
		background-color: #f3f3f3;
	}
	.order-list {}

	// 项目内容
	.order-item {
		padding: 30rpx;
		margin: 20rpx auto 10rpx auto;
		width: 660rpx;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.1);
		border-radius: 15rpx;
		background: #fff;
	}

	// 项目顶部
	.item-top {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		margin-bottom: 20rpx;

		.order-time {
			color: #333333;
			font-size: 26rpx;
			font-weight: 600;
		}

		.state-text {
			color: $main-bg;
		}
	}

	// 商品列表
	.goods-list {

		// 商品项
		.goods-item {
			display: flex;
			margin-bottom: 20rpx;

			.cancel {
				width: 70rpx;
				position: absolute;
				top: 4rpx;
				right: 0;
			}

			.icon {
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
				background: url('data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20version%3D%221.1%22%3E%3Cdefs%3E%3ClinearGradient%20id%3D%221%22%20x1%3D%220%22%20x2%3D%221%22%20y1%3D%220%22%20y2%3D%220%22%20gradientTransform%3D%22matrix(6.123233995736766e-17%2C%201%2C%20-0.024693877551020406%2C%206.123233995736766e-17%2C%200.5%2C%200)%22%3E%3Cstop%20stop-color%3D%22%230a060d%22%20stop-opacity%3D%221%22%20offset%3D%220%22%3E%3C%2Fstop%3E%3Cstop%20stop-color%3D%22%23660061%22%20stop-opacity%3D%221%22%20offset%3D%220.95%22%3E%3C%2Fstop%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22url(%231)%22%3E%3C%2Frect%3E%3C%2Fsvg%3E');

				// background: linear-gradient(to bottom, #010002, #681752);
				image {
					width: 50rpx;
					height: 50rpx;
				}
			}

			// 商品图片
			.goods-image {
				width: 120rpx;
				height: 90rpx;

				.image {
					display: block;
					width: 120rpx;
					height: 90rpx;
					border-radius: 8rpx;
				}
			}

			// 商品内容
			.goods-content {
				width: 500rpx;
				padding-left: 16rpx;
				padding-top: 0;
				display: flex;
				color: #666666;
				flex-direction: column;
				position: relative;

				.goods-title {
					font-size: 26rpx;
					max-height: 76rpx;
					line-height: 36rpx;
				}

				.goods-props {
					margin-top: 14rpx;
					height: 40rpx;
					color: #ababab;
					font-size: 24rpx;
					overflow: hidden;

					.goods-props-item {
						display: inline-block;
						margin-right: 14rpx;
						padding: 4rpx 16rpx;
						border-radius: 12rpx;
						background-color: #F5F5F5;
						width: auto;
					}
				}


			}

			// 交易信息
			.goods-trade {
				padding-top: 16rpx;
				width: 200rpx !important;
				text-align: right;
				color: $uni-text-color-grey;
				font-size: 26rpx;

				.goods-price {
					vertical-align: bottom;
					margin-bottom: 16rpx;

					.unit {
						margin-right: -2rpx;
						font-size: 24rpx;
					}
				}
			}

		}

	}

	// 订单合计
	.order-total {
		font-size: 26rpx;
		vertical-align: bottom;
		text-align: right;
		height: 50rpx;
		padding-top: 10rpx;
		display: flex;
		justify-content: space-between;
		
		.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;
		}
	}

	// 订单操作
	.order-handle {
		.btn-group {

			.btn-item {
				border-radius: 10rpx;
				padding: 8rpx 20rpx;
				margin-left: 15rpx;
				font-size: 26rpx;
				float: right;
				color: #383838;
				border: 1rpx solid #a8a8a8;

				&:last-child {
					margin-left: 0;
				}

				&.active {
					color: $main-bg;
					border: 1rpx solid $main-bg;
				}
			}

		}

	}

	.tabs {
		width: 600rpx;
		margin: auto;
		display: flex;
		justify-content: center;
	}

	.yuqi-text {
		color: #ff0000;
	}
</style>
