<template>
	<view>
		<view class="navtab flex-center">
			<view class="navtabpost flex-center">
				<view :class="navtabindex == 0 ? 'navtabone':'navtabtow'" @click="navtabclick(0)">
					到店订单
				</view>
				<view :class="navtabindex == 1 ?  'navtabone':'navtabtow'" @click="navtabclick(1)">
					团购订单
				</view>
			</view>
		</view>
		<u-sticky bgColor="#fff">
			<view class="tab-wrap">
				<block v-if="navtabindex==1">
					<view class="item" v-for="(item, index) in tabs2" :key="index" @click="orderswitch(item,index)">
						<text>{{ item.name }}</text>
					</view>
				</block>

				<block v-if="navtabindex==0">
					<view class="item" v-for="(item, index) in tabs" :key="index" @click="orderswitch(item,index)">
						<text>{{ item.name }}</text>
					</view>
				</block>
				<view class="line-wrap"
					:style="{ left: `${(100 / tabs.length) * active}%`, width: `${100 / tabs.length}%` }">
					<view class="line">
						<image style="width:40rpx; height: 10rpx;"
							src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/index/today/dgs.png"
							mode="aspectFill">
						</image>
					</view>
				</view>
			</view>
		</u-sticky>
		<view class="list-wrap" v-if="navtabindex == 0">
			<view class="item" v-for="(item,index) in list" :key="index" @click="orderinfo(item)">
				<view class="header-wrap">
					<text v-if="item.sendType == 'post'">快递</text>
					<text v-if="item.sendType == 'takeaway'">外卖</text>
					<text v-if="item.sendType == 'takeself'">自提</text>
					<text v-if="item.sendType == 'table'">堂食</text>
					<text class="status" v-if="item.status == 'unpaid' || item.status == 'paying'">
						<text>待支付</text>
					</text>
					<text class="status" v-if="item.status == 'unsend'">
						<text>待发货</text>
					</text>


					<text class="status" v-if="item.status == 'closed'">
						<text>订单完成</text>
					</text>
					<text class="status" v-if="item.status == 'send'">
						<text> 已发</text>
					</text>
					<text class="status" v-if="item.status == 'refunding'">
						<text>申请退单</text>
					</text>
					<text class="status" v-if="item.status == 'refund'">
						<text>退单</text>
					</text>
					<text class="status" v-if="item.status == 'cancelled'">
						<text>取消订单</text>
					</text>
					<text class="status" v-if="item.status == 'merge'">
						<text>合台</text>
					</text>
				</view>
				<view class="content">
					<view class="shop-info">
						<view class="shop-item">
							<view class="cover flex-start" v-for="(item1,index1) in item.detailList" :key="index1">
								<!-- <image :src="item1.productImg" style="width: 200rpx;height: 200rpx; border-radius: 20rpx;" mode="widthFix"></image> -->
								<c-image width="200" height="200" radius="20" :src="item1.productImg"></c-image>
							</view>
							<!-- <view class="info">
									<text class="name">美味大宅蟹</text>
									<text class="num">数量：1</text>
								</view> -->
						</view>
					</view>
					<view class="total">
						<text class="t">{{item.status == 'unpaid' || item.status == 'paying'?'需':'已'}}付款</text>
						<text class="i">￥</text>
						<text class="num">{{item.orderAmount}}</text>
					</view>
					<view class="intro-wrap">
						<text class="time">下单日期：{{$u.timeFormat(item.createdAt, 'yyyy-mm-dd hh:MM')}}</text>
						<text class="intro num">共{{item.totalNumber}}件商品 合计：￥{{item.orderAmount}}</text>
					</view>
				</view>
				<view class="footer-wrap">
					<view class="btn">
						<text>查看详情</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 新订单 -->
		<view class="orderList" v-if="navtabindex == 1">
			<view class="listBox" v-for="(item,i) in  groupList" :key="i" @click="orderinfoTo(item)">
				<view class="df">
					<view style="display: flex;">
						<text>{{item.proName.length>10?item.proName.substring(0,10)+'...':item.proName}}</text><u-icon
							name="arrow-right" color="#000" size="28"></u-icon>
					</view>
					<text
						:class="[item.status=='unpaid'||item.status=='unused'?'state':'state2']">{{item.status|statusFirter}}</text>
				</view>
				<view class="df" style="justify-content: flex-start;margin-top: 32rpx;">
					<image style="width:120rpx; height: 120rpx;border-radius: 12rpx 12rpx 12rpx 12rpx;"
						:src="item.proImg" mode="aspectFill">
					</image>
					<view class="ml-20 fontStyle">
						<view>数量：{{item.number}}份</view>
						<view>实付：<text style="color: #FF4C11;">￥{{item.payAmount}}</text></view>
					</view>
				</view>
				<button v-if="item.status=='unused'" type="primary" class="buttonStyle">查看券码</button>
				<button v-if="item.status=='unpaid'" type="primary" class="buttonStyle">去付款</button>
			</view>
		</view>
		<image style="margin:32rpx auto;" src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/nomore.png"
			v-if="is_end" mode="aspectFill"></image>
		<u-loadmore :status="form.status" fontSize="28" color="#999" iconSize="28" />
	</view>
</template>

<script>
	import list from '../../uni_modules/uview-ui/libs/config/props/list';
	export default {
		data() {
			return {
				active: 0,
				navtabindex: 0,
				tabs: [{
						name: '全部',
						type: 1,
						status: ''
					},
					{
						name: '待支付',
						type: 2,
						status: 'unpaid'
					},
					{
						name: '待发货',
						type: 3,
						status: 'unsend'
					},

					{
						name: '已完成',
						type: 4,
						status: 'closed'
					},
					{
						name: '退款',
						type: 5,
						status: 'refund'
					},
				],
				tabs2: [{
						name: '全部',
						type: 1,
						status: ''
					},
					{
						name: '待支付',
						type: 2,
						status: 'unpaid'
					},
					{
						name: '待使用',
						type: 3,
						status: 'unused'
					},

					{
						name: '已完成',
						type: 4,
						status: 'closed'
					},
					{
						name: '退款',
						type: 5,
						status: 'refund'
					},
				],
				list: [],
				groupList: [],
				is_end: false,
				swiperCurrent: '',
				form: {
					page: 1,
					size: 10,
					status: 'loadmore',
				},
			};
		},
		onShow() {
			if (uni.cache.get('token')) {
				this.loginwxuserInfo()
			}
			if (this.tabs.findIndex((item) => item.type == this.useStorage.get('orderType')) == -1) {
				this.active = 0
			} else {
				this.active = (this.tabs.findIndex((item) => item.type == this.useStorage.get('orderType')));
			}
			this.init_fn()
		},
		onReachBottom() {
			if (this.navtabindex == 1) {
				// 获取团购订单
				this.getorderList()
			} else {
				this.orderorderList()
			}
		},
		filters: {
			statusFirter(e) {
				// 状态: unpaid-待付款;unused-待使用;closed-已完成;refunding-退款中;refund-已退款;cancelled-已取消;
				if (e == 'closed') return '已完成'
				else if (e == 'paying') return '支付中'
				else if (e == 'unpaid') return '待付款'
				else if (e == 'unused') return '待使用'
				else if (e == 'refunding') return '退款中'
				else if (e == 'refund') return '已退款'
				else if (e == 'cancelled') return '已取消'
			}
		},
		methods: {
			// 切换导航栏
			navtabclick(i) {
				this.navtabindex = i
				this.init_fn()
			},
			async loginwxuserInfo() {
				let res = await this.api.loginwxuserInfo({
					userId: uni.cache.get('userInfo').id
				})
				if (res.code == 0) {
					uni.cache.set('userInfo', res.data);
					this.userInfo = uni.cache.get('userInfo')
				}
			},
			orderinfo(e) {
				uni.pro.navigateTo('order/order_detail', {
					orderId: e.id
				})
			},
			orderinfoTo(e) {
				uni.pro.navigateTo('group_order/order_groupdetail', {
					orderId: e.id
				})
			},
			init_fn() {
				this.list = []
				this.groupList = []
				this.is_end = false
				this.form.page = 1
				this.form.size = 10
				this.form.status = 'loadmore'
				if (this.navtabindex == 1) {
					// 获取团购订单
					this.getorderList()
				} else {
					this.orderorderList()
				}
			},
			orderswitch(e, a) {
				this.active = a
				if (e.type == 1) {
					this.swiperCurrent = ''
				} else {
					this.swiperCurrent = e.status
				}
				this.init_fn()
			},
			async orderorderList() {
				let res = await this.api.orderorderList({
					page: this.form.page,
					size: this.form.size,
					userId: uni.cache.get('userInfo').id, //userId
					status: this.swiperCurrent
				})
				if (res.data.pages < this.form.page) {
					this.form.status = 'nomore'
					if (this.form.page == 1 && res.data.list.length == 0) {
						this.is_end = true
					}
					return false;
				} else {
					this.form.status = 'loading';
					this.form.page = ++this.form.page;
					setTimeout(() => {
						this.list = [...this.list, ...res.data.list];
						this.form.status = 'loading';
						if (res.data.pageNum == res.data.pages) {
							this.form.status = 'nomore';
						} else {
							this.form.status = 'loading';
						}
					}, 500)
				}
			},
			async getorderList() {
				let res = await this.api.groupOrderInfo({
					page: this.form.page,
					size: this.form.size,
					userId: uni.cache.get('userInfo').id, //userId
					status: this.swiperCurrent
				})
				if (res.data.pages < this.form.page) {
					this.form.status = 'nomore'
					if (this.form.page == 1 && res.data.list == 0) {
						this.is_end = true
					}
					return false;
				} else {
					this.form.status = 'loading';
					this.form.page = ++this.form.page;
					setTimeout(() => {
						this.groupList = [...this.groupList, ...res.data.list];
						this.form.status = 'loading';
						if (res.data.pageNum == res.data.pages) {
							this.form.status = 'nomore';
						} else {
							this.form.status = 'loading';
						}
					}, 500)
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	$tabH: 80upx;
	$color: #ff411d;

	page {
		background: #f6f6f6;
	}

	.navtab {
		width: 100%;
		height: 56rpx;
		background: #ffd158;

		.navtabpost {
			position: relative;
			background: #FFFFFF;
			border-radius: 12rpx;
			padding:0 10rpx;
			.navtabone {
				// margin-left: -10rpx;
				width: 166rpx;
				height: 36rpx;
				background: #FFEAB1;
				border-radius: 12rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				line-height: 36rpx;
				text-align: center;
				z-index: 10;
			}

			.navtabtow {
				// margin-left: -10rpx;
				z-index: 9;
				width: 146rpx;
				height: 56rpx;
				line-height: 56rpx;
				text-align: center;
				// border-radius: 0 12rpx 12rpx 0;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				font-size: 24rpx;
				color: #666666;
			}
		}
	}

	.tab-wrap {
		display: flex;
		position: relative;
		background: #ffd158;
		padding: 48rpx 0 72rpx 0;

		.item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			z-index: 2;

			text {
				color: #fff;
			}
		}

		.line-wrap {
			position: absolute;
			bottom: 48rpx;
			display: flex;
			justify-content: center;
			padding-bottom: 8upx;
			transition: all 0.3s ease-in-out;

			.line {
				$h: 6upx;
				margin-top: 6rpx;
				// width: 30%;
				// height: $h;
				border-radius: $h;
				// background-color: $color;
			}
		}
	}

	.list-wrap {
		padding: $paddingSize;
		position: relative;
		margin-top: -24rpx;
		z-index: 99999;
		background: #f6f6f6;
		border-radius: 24rpx 24rpx 0rpx 0rpx;

		.item {
			border-radius: 20upx;
			background-color: #fff;

			&:not(:first-child) {
				margin-top: $paddingSize;
			}

			.header-wrap {
				padding: $paddingSize;
				border-bottom: 1upx solid #ececec;
				display: flex;
				justify-content: space-between;

				.status {
					color: $color;
				}
			}

			.content {
				padding: $paddingSize;

				.shop-info {
					.shop-item {
						display: flex;
						overflow: hidden;
						overflow-x: scroll;

						.cover:nth-child(1) {
							margin-left: 0rpx;
						}

						.cover {

							// white-space: nowrap;
							margin-left: 10rpx;
						}

						.info {
							flex: 1;
							display: flex;
							flex-direction: column;
							justify-content: space-around;
							padding-left: 20upx;
						}
					}
				}

				.total {
					display: flex;
					align-items: flex-end;
					justify-content: flex-end;

					.t {
						font-weight: bold;
						font-size: 32upx;
					}

					.i {
						font-size: 24upx;
					}

					.num {
						font-size: 38upx;
						position: relative;
						top: 8upx;
					}
				}

				.intro-wrap {
					display: flex;
					justify-content: space-between;
					padding-top: $paddingSize;

					.time {
						color: #999;
						font-size: 24upx;
					}

					.intro {
						font-size: 24upx;
					}
				}
			}

			.footer-wrap {
				display: flex;
				justify-content: flex-end;
				border-top: 1upx solid #ececec;
				padding: $paddingSize;

				.btn {
					padding: 8upx 24upx;
					border-radius: 100upx;
					border: 1px solid #333;
					display: flex;
					align-items: center;
				}
			}
		}
	}

	.orderList {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;

		.listBox {
			padding: 16rpx 18rpx;
			margin-top: 48rpx;
			font-size: 32rpx;
			width: 694rpx;
			height: 248rpx;
			background: #FFFFFF;
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			position: relative;

			.buttonStyle {
				position: absolute;
				right: 20rpx;
				bottom: 50rpx;
				font-size: 28rpx;
				width: 170rpx;
				height: 64rpx;
				background: #FFD100;
				border-radius: 32rpx 32rpx 32rpx 32rpx;
				border: none;
				color: #000;
			}

			.state {
				color: #FF4C11;
			}

			.state2 {
				color: #999;
			}

			.fontStyle {
				font-size: 28rpx;
				font-weight: 400;
				color: #666666;
			}
		}

	}

	.df {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.ml-20 {
		margin-left: 20rpx;
	}
</style>