<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 >
			<view class="tab-wrap">
				<view class="item" :class="{'active':active == index}" v-for="(item, index) in tabs" :key="index" @click="orderswitch(item,index)">
					<text>{{ item.name }}</text>
				</view>
			</view>
		</u-sticky>
		<view class="list-wrap" v-if="!is_end">
			<view class="item" v-for="(item,index) in list" :key="index" @click="orderinfo(item)">
				<view class="header-wrap">
					<view class="header-wrap-left">
						<text class="sendType" v-if="item.sendType == 'post'">快递</text>
						<text class="sendType" v-if="item.sendType == 'takeaway'">外卖</text>
						<text class="sendType" v-if="item.sendType == 'takeself'">自提</text>
						<text class="sendType" v-if="item.sendType == 'table'">堂食</text>
						<text class="shopName"> {{ item.shopName || ''}} </text>
					</view>
				
					<text class="status" v-if="item.status == 'unpaid' || item.status == 'paying'">
						<text style="color: #333;">待付款</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></view>
					<view class="intro-wrap">
						下单日期：{{$u.timeFormat(item.createdAt, 'yyyy-mm-dd hh:MM')}}
					</view>
					<view class="shop-info">
						<view class="shop-item">
							<view class="cover" v-for="(item1,index1) in item.detailList" :key="index1">
								<u-image width="112" height="112" radius="20" :src="item1.productImg"></u-image>
								<text class="productName"> {{ item1.productName }} </text>
							</view>
						</view>
						<view class="shop-amount">
							<text class="orderAmount">￥{{item.orderAmount}}</text>
							<text class="totalNumber">共{{item.totalNumber}}件</text>
						</view>
					</view>
					
				</view>
				<view class="footer-wrap">
					<view class="btn" @click.stop="$u.debounce(isRemoveOrder(item,index),1000)" v-if="item.status != 'unpaid' &&  item.status != 'paying'"> 删除订单 </view>
					<view class="btn s"  @click.stop="$u.debounce(showpopupclick(item),1000)" v-if="item.status == 'unpaid' ||  item.status == 'paying'"> 去付款 </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="width: 402rpx;height: 442rpx;margin:240rpx auto 32rpx;" src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/nomore.png"
			v-if="list.length <= 0 " mode="aspectFill"></image>
		<u-modal width="450rpx" :show="removeOrderShow" @confirm="$u.debounce(removeOrder(),1000)" @cancel="removeOrderShow = false" @close="removeOrderShow = false" :showCancelButton="true" :closeOnClickOverlay="true" :title="'是否删除当前订单'" ></u-modal>
		<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',
				},
				removeOrderShow: false,
				orderItem: null,
				orderIndex: 0,
			};
		},
		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.swiperCurrent = ''
			this.init_fn()
			uni.cache.set('forceUpdate',1)
		},
		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.list = []
						this.is_end = true
					}
					return false;
				} else {
					this.form.status = 'loading';
					if (this.form.page == 1) {
						this.list = res.data.list
					} else {
						this.list = [...this.list, ...res.data.list];
					}
					this.form.page = ++this.form.page;
					if (this.form.page > res.data.pages) {
						this.form.status = 'nomore';
					} else {
						this.form.status = 'loading';
					}

				}
			},
			
			isRemoveOrder (  item , index  ) {
				this.removeOrderShow = true;
				this.orderItem = item;
				this.orderIndex = index;
			},
		
			/**
			 * 删除订单
			 */
			async removeOrder () {
				let res = await this.api.removeOrder({
					orderId: this.orderItem.id,
				})
				if ( res.code == 0 ) {
					this.removeOrderShow = false;
					this.list.splice(this.orderIndex , 1 )
					uni.showToast({
						title: "删除成功",
						icon: "none"
					})
				}
			},
			
			/**
			 * 立即付款
			 */
			async showpopupclick(item) {
				let res = await this.api.payorderPay({
					orderId: item.id,
				}) //判断是否支付成功
				if (res.code == 0) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					// #ifdef MP-WEIXIN
					uni.requestPayment({
						provider: 'wxpay', //支付类型-固定值
						partnerid: res.data.appId, // 微信支付商户号
						timeStamp: res.data.timeStamp, // 时间戳（单位：秒）
						nonceStr: res.data.nonceStr, // 随机字符串
						package: res.data.package, // 固定值
						signType: res.data.signType, //固定值
						paySign: res.data.paySign, //签名
						success: (res) => {
							uni.showToast({
								title: "支付成功"
							})
							setTimeout(res => {
								uni.hideLoading()
								uni.switchTab({
									url: '/pages/order/order'
								});
							}, 500)
							this.paymodfiyOrderInfo()
						},
						fail: (err) => {
							uni.showToast({
								icon: 'none',
								title: '支付失败'
							})
							setTimeout(res => {
								uni.hideLoading()
								uni.switchTab({
									url: '/pages/order/order'
								});
							}, 500)
			
						}
					});
					// #endif	
				}
			},
			/**
			 * 支付完成后请求
			 */
			async paymodfiyOrderInfo() {
				let res = await this.api.paymodfiyOrderInfo({
					orderId: this.listinfo.orderId,
				})
			},
			
			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">


	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: #fff;
		padding: 40rpx 46rpx;
		border-radius: 0rpx 0rpx 34rpx 34rpx;
		.item {
			flex: auto;
			display: flex;
			flex-shrink: 0;
			align-items: center;
			justify-content: center;
			position: relative;
			z-index: 2;
			text {
				font-weight: 400;
				font-size: 28rpx;
				color: #999999;
			}
		}
		
		.item.active{
			text{
				font-weight: bold;
				font-size: 32rpx;
				color: #000000;
			}
		}

		
	}

	.list-wrap {
		padding: 0 28rpx 48rpx 28rpx;
		position: relative;
		margin-top: 48rpx;
		border-radius: 24rpx 24rpx 0rpx 0rpx;

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

			&:not(:first-child) {
				margin-top: 48rpx;
			}

			.header-wrap {
				padding: 16rpx 18rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.header-wrap-left{
					display: flex;
					align-items: center;
				}
				.sendType{
					height: 48rpx;
					line-height: 48rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #E3AD7F;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					border: 2rpx solid #E3AD7F;
					margin-right: 24rpx;
					padding: 0 20rpx;
				}
				.shopName{
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
				}
				.status {
					text{
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
					}
				}
			}

			.content {
				padding: 0 18rpx 18rpx 18rpx;

				.shop-info {
					display: flex;
					.shop-item {
						width: 100%;
						display: flex;
						overflow: hidden;
						overflow-x: scroll;
						.cover:nth-child(1) {
							margin-left: 0rpx;
						}

						.cover {
							margin-left: 20rpx;
							display: flex;
							flex-direction: column;
							align-items: center;
							flex-shrink: 0;
							.productName{
								flex-shrink: 0;
								margin-top: 16rpx;
							}
						}

					}
					.shop-amount{
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: flex-end;
						flex-shrink: 0;
						padding-left: 20rpx;
						.orderAmount{
							font-weight: bold;
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 22rpx;
						}
						.totalNumber{
							font-weight: 400;
							font-size: 24rpx;
							color: #999999;
						}
					}
				}

				.intro-wrap {
					font-weight: 400;
					color: #666;
					font-size: 24upx;
					margin-bottom: 16rpx;
				}
			}

			.footer-wrap {
				display: flex;
				justify-content: flex-end;
				padding: 0 18rpx 32rpx 18rpx;
				
				.btn {
					width: 128rpx;
					height: 48rpx;
					line-height: 43rpx;
					text-align: center;
					background: #FFFFFF;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					border: 2rpx solid #EDEDED;
					font-weight: 400;
					font-size: 24rpx;
					color: #333333;
					margin-left: 32rpx;
				}
				
				.s{
					background: #343030;
					border: 2rpx solid #EDEDED;
					color: #fff;
				}
			}
		}
	}

	.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>