<template>
	<view>
		<!-- 顶部导航 -->
		<view class="topTabBar" :style="{position:headerPosition,top:headerTop}">
			<view class="grid" v-for="(grid,tbIndex) in orderType" :key="tbIndex" @tap="showType(tbIndex)">
				<view class="text" :class="[tbIndex==tabbarIndex?'on':'']">{{grid}}</view>
			</view>
		</view>
		<!-- 考虑非APP端长列表和复杂的DOM使用scroll-view会卡顿，所以漂浮顶部选项卡使用page本身的滑动 -->
		<view class="order-list">
			<view class="list">
				<view class="onorder" v-if="list.length==0">
					<image src="../../../static/img/noorder.png"></image>
					<view class="text">
						没有相关订单
					</view>
				</view>
				<view class="row" v-for="(row,index) in list" :key="index">
					<!-- <view class="type">{{typeText[row.type]}}</view> -->
					<view class="type">
						<span v-if="row.if_invoice==0" style="color:darkblue">不开票</span>
						<span v-else-if="row.if_invoice==1" >
						  <text style="color:darkblue">普票</text>
						  <text v-if="row.invoice_status==1" style="color:green">(已开)</text>
						  <text v-else style="color:gray">(待开)</text>
						</span>
						<span v-else>
							<text style="color:darkblue">专票</text>
						<text v-if="row.invoice_status==1" style="color:green">(已开)</text>
						<text v-else style="color:gray">(待开)</text>
						</span>
						<view style="margin-left:auto">
							<uni-tag v-if="row.pay_status==0" text='待付款' :inverted="true" type="default" size="small"></uni-tag>
							<uni-tag v-if="row.pay_status==2" text='付款确认失败' :inverted="true" type="error" size="small"></uni-tag>
							<uni-tag style="margin-left:4upx" v-if="row.pay_status==1 && row.shipping_status==0" text='待发货' :inverted="true" type="default" size="small"></uni-tag>
							<uni-tag style="margin-left:4upx" v-if="row.invoice_status==2 && row.if_invoice!=0" text='待开票' :inverted="true" type="default" size="small"></uni-tag>
							<uni-tag style="margin-left:4upx" v-if="row.shipping_status==1 && row.receive_status==0" text='待收货' :inverted="true" type="default" size="small"></uni-tag>
							<uni-tag style="margin-left:4upx" v-if="row.receive_status>0" text="已完成" :inverted="true"type="default" size="small"></uni-tag>
							
						</view>
					</view>
					<view>{{row.shop_db[0].username}}</view>
					<view class="order-info" v-for="item in row.shop_db" v-bind:key="item.id" @click="orderDetail(row,index)">
						<view class="left">
							<image :src="item.picurl"></image>
						</view>
						<view class="right">
							<view class="name">
								{{item.full_title}}
							</view>
							<!-- <view class="spec">{{row.spec}}</view> -->
							<view class="price-number">
								￥<view class="price">{{item._price}}</view>
								x<view class="number">{{item._num}}</view>
							</view>
						</view>
						
					</view>
					<view style="text-align: right;">
						<!-- <view class="number" style="font-size: 24upx;">收货人:{{row.linkman}} {{row.telphone}} {{row.address}}</view> -->
						<view class="number" style="font-size: 24upx;">收货人:{{row.linkman}} {{row.telphone}}</view>
						<!-- <view class="number" style="font-size: 24upx;">订单号:{{row.order_sn}}<span style="color:red">|{{row.create_time}}</span></view> -->
					</view>
					<view class="detail">
						<!-- <view class="number">共{{row.shop_db.length}}件商品</view> -->
						<view class="sum">合计：<view class="price">￥{{row.totalmoney}}</view></view><view class="nominal">(含运费 ￥{{row.real_freight}})</view>
					</view>
					<view class="btns">
						<view class="default" @tap="cancelOrder(row.id, index)" v-if="row.shipping_status==0">取消订单</view>
						<view class="pay" @tap="toUppay(row)" v-if="row.pay_status==0||row.pay_status==2">上传付款凭证</view></block>
						<!-- <block v-if="row.type=='back'"><view class="default" @tap="remindDeliver(row)">提醒发货</view></block> -->
							<!-- <view class="default" @tap="showLogistics(row)">查看物流</view> -->
						<view class="default" @tap="qrsh(row, index)" v-if="row.shipping_status==1 && row.receive_status==0">确认收货</view>
							<view class="default" @tap="seeImg(row.voucher_url)" v-if="row.voucher_url!=''">查看付款</view>
							<view class="primary" @tap="applayInvoice(row)" v-if="row.if_invoice==0&&row.voucher_url==''">提交开票</view>
							<view class="primary" @tap="qrfp(row, index)" v-if="row.invoice_status ==1 && row.receive_invoice_status ==0">确认收票</view>
							<!-- <view class="default" @tap="seeInvoice(row)" v-if="row.invoice_status==1">查看开票</view> -->
<!-- 						<view class="pay">我要退货</view></block>
						<block v-if="row.type=='received'"><view class="default">评价</view>
						<view class="default">再次购买</view></block>
						<block v-if="row.type=='completed'"><view class="default">再次购买</view></block>
						<block v-if="row.type=='refunds'"><view class="default">查看进度</view></block> -->
						<!-- <block v-if="tabbarIndex==3"><view class="default" v-if="row.if_invoice==0">提交开票</view></block> -->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				headerPosition:"fixed",
				headerTop:"0px",
				typeText:{
					unpaid:'等待付款',
					back:'等待商家发货',
					unreceived:'商家已发货',
					received:'等待用户评价',
					completed:'交易已完成',
					refunds:'商品退货处理中',
					cancelled:'订单已取消'
				},
				// orderType: ['全部','待付款','待发货','待收货','待评价','退换货'],
				orderType: ['待付款','待发货','待收货', '待开票',' 待收票', '全部'],
				orderQuery:['ispay', 'noshipping', 'noreceive', 'noinvoice','noreceive_invoice', ''],
				//订单列表 演示数据
				orderList:[
					[],[],[],[],[]
				],
				list:[],
				pagelist:[1,1,1,1,1,1],
				tabbarIndex:0,
			};
		},
		onLoad(option) {
			//option为object类型，会序列化上个页面传递的参数
			console.log("option: " + JSON.stringify(option));
			let tbIndex = parseInt(option.tbIndex);
			// this.list = this.orderList[tbIndex];
			this.tabbarIndex = tbIndex;
			//兼容H5下排序栏位置
			// #ifdef H5
				let Timer = setInterval(()=>{
					let uniHead = document.getElementsByTagName('uni-page-head');
					if(uniHead.length>0){
						this.headerTop = uniHead[0].offsetHeight+'px';
						clearInterval(Timer);//清除定时器
					}
				},1);
			// #endif
			
		},
		onShow(){
			this.getOrders()
		},
		onPageScroll(e){
			return;
			//兼容iOS端下拉时顶部漂移
			this.headerPosition = e.scrollTop>=0?"fixed":"absolute";
		},
		onPullDownRefresh() {
			this.pagelist[this.tabbarIndex] = 1
			this.getOrders()
			uni.stopPullDownRefresh();
		},
		onReachBottom(){
			this.pagelist[this.tabbarIndex]  = this.pagelist[this.tabbarIndex] + 1
			this.getOrders()
		},
		methods: {
			getOrders(){
				let query = {}
				query.type = this.orderQuery[this.tabbarIndex]
				query.page = this.pagelist[this.tabbarIndex]
				let rdata = this.orderList[this.tabbarIndex]
				
				this.$u.api.getOrders(query).then(res=>{
					if(query.page==1){
						rdata=res.data
					}else{
						if(res.data.length){
							rdata = rdata.concat(res.data)
						}
					}
					this.list = rdata
					
					this.orderList[this.tabbarIndex] = rdata
				})
			},
			showType(tbIndex){
				this.tabbarIndex = tbIndex;
				this.getOrders()
			},
			showLogistics(row){
				
			},
			remindDeliver(row){
				uni.showToast({
					title:'已提醒商家发货'
				})
			},
			seeImg(val){
				let urls = val.split(',')
				for(var i=0;i<urls.length;i++){
					urls[i] = this.vuex_host + '/public/' + urls[i];
				}
				uni.previewImage({
					current:urls[0],
					urls: urls
				});
			},
			seeInvoice(row){
				
			},
			cancelOrder(row, index){
				// uni.showModal({
				// 	title: '取消订单',
				// 	content: '确定取消此订单？',
				// 	success: (res)=>{
				// 		if (res.confirm) {
				// 			console.log('用户点击确定');
				// 			this.doCancelOrder(row.ordersn);
				// 		} else if (res.cancel) {
				// 			console.log('用户点击取消');
				// 		}
				// 	}
				// });
				var that = this
				uni.showModal({
				    content: '确认取消订单吗?',
				    success: function (res) {
				        if (res.confirm) {
				           that.$u.api.qxdd(row).then(res=>{
				           	uni.showToast({
				           	    title: '取消成功',
				           	    icon:'none'
				           	});
							that.orderList[that.tabbarIndex].splice(index,1)
				           })
				        }
				    }
				});
				
			},
			qrsh(row, index){
				var that = this
				uni.showModal({
				    content: '确认收货吗?',
				    success: function (res) {
				        if (res.confirm) {
				           that.$u.api.qrsh(row.id).then(res=>{
				           	uni.showToast({
				           	    title: '确认成功',
				           	    icon:'none'
				           	});
							that.orderList[that.tabbarIndex].splice(index,1)
				           })
				        }
				    }
				});
			},
			qrfp(row, index){
				var that = this
				uni.showModal({
				    content: '确认已收到发票吗?',
				    success: function (res) {
				        if (res.confirm) {
				           that.$u.api.qrfp(row.id).then(res=>{
				           	uni.showToast({
				           	    title: '确认成功',
				           	    icon:'none'
				           	});
							that.getOrders()
				           })
				        }
				    }
				});
			},
			doCancelOrder(ordersn){
				let typeNum = this.orderList.length;
				for(let i=0;i<typeNum;i++){
					let list = this.orderList[i];
					let orderNum = list.length;
					if(orderNum>0 && list[0].type=='unpaid'){
						for(let j=0;j<orderNum;j++){
							if(this.orderList[i][j].ordersn == ordersn){
								this.orderList[i][j].type = 'cancelled';
								break;
							}
						}
					}
					
				}
			},
			applayInvoice(row){
				uni.showLoading({
					title:'正在获取订单...'
				})
				setTimeout(()=>{
					uni.setStorage({
						key:'paymentOrder',
						data:row,
						success: () => {
							uni.hideLoading();
							uni.navigateTo({
								url:'/pages/invoice/apply'
							})
						}
					})
				},500)
			},
			toPayment(row){
				//本地模拟订单提交UI效果
				uni.showLoading({
					title:'正在获取订单...'
				})
				let paymentOrder = [];
				paymentOrder.push(row);
				setTimeout(()=>{
					uni.setStorage({
						key:'paymentOrder',
						data:paymentOrder,
						success: () => {
							uni.hideLoading();
							uni.navigateTo({
								url:'../../pay/payment/payment?amount='+row.payment
							})
						}
					})
				},500)
			},
			toUppay(row){
				//本地模拟订单提交UI效果
				uni.showLoading({
					title:'正在获取订单...'
				})
				setTimeout(()=>{
					uni.setStorage({
						key:'paymentOrder',
						data:row,
						success: () => {
							uni.hideLoading();
							uni.navigateTo({
								url:'../../pay/payment/uppayment'
							})
						}
					})
				},500)
			},
			orderDetail(row,index){
				uni.showLoading({
					title:'正在获取订单...'
				})
				debugger;
				console.log(row)
				setTimeout(()=>{
					uni.setStorageSync('orderId',row.id);
					uni.navigateTo({url:'./orderDetail?orderId='+row.id}) 
					/* uni.setStorage({
						key:'orderDetail',
						data:JSON.stringify(this.list[index]),
						success: () => {
							uni.hideLoading();
							uni.navigateTo({
								url:'./orderDetail'
							})
						}
					}) */
				},100)
			}
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f3f3f3;
}
.topTabBar{
	width: 100%;
	position: fixed;
	top: 0;
	z-index: 10;
	background-color: #f8f8f8;
	height: 80upx;
	display: flex;
	justify-content: space-around;
	.grid{
		width: 20%;
		height: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #444;
		font-size: 28upx;
		.text{
			height: 76upx;
			display: flex;
			align-items: center;
			&.on{
				color: #f06c7a;
				border-bottom: solid 4upx #f06c7a;
			}
		}
		
	}
}
.order-list{
	margin-top: 80upx;
	padding-top: 20upx;
	width: 100%;
	.list{
		width: 94%;
		margin: 0 auto;
		.onorder{
			width: 100%;
			height: 50vw;
			display: flex;
			justify-content: center;
			align-content: center;
			flex-wrap: wrap;
			image{
				width: 20vw;
				height: 20vw;
				border-radius: 100%;
			}
			.text{
				width: 100%;
				height: 60upx;
				font-size: 28upx;
				color: #444;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.row{
			width: calc(100% - 40upx);
			padding: 10upx 20upx;
			border-radius: 10upx;
			background-color: #fff;
			margin-bottom: 20upx;
			.type{
				font-size: 28upx;
				// color: #ec652f;
				height: 50upx;
				display: flex;
				align-items: center;
			}
			.order-info{
				width: 100%;
				display: flex;
				margin-top: 4upx;
				.left{
					flex-shrink: 0;
					width: 25vw;
					height: 20vw;
					image{
						width: 25vw;
						height: 20vw;
						border-radius: 10upx;
					}
				}
				.right{
					width: 100%;
					margin-left: 10upx;
					position: relative;
					.name{
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.spec{
						color: #a7a7a7;
						font-size: 22upx;

					}
					.price-number{
						position: absolute;
						bottom: 0;
						width: 100%;
						display: flex;
						justify-content: flex-end;
						font-size: 22upx;
						color: #333;
						display: flex;
						align-items: flex-end;
						.price{
							font-size: 24upx;
							margin-right: 5upx;
						}
						
					}
				}
			}
			.detail{
				display: flex;
				justify-content: flex-end;
				align-items: flex-end;
				height: 46upx;
				font-size: 26upx;
				.sum{
					padding: 0 8upx;
					display: flex;
					align-items: flex-end;
					.price{
						color:#ec652f;
						font-size: 30upx;
					}
				}
				
			}
			.btns{
				height: 80upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				view{
					min-width: 120upx;
					height: 50upx;
					padding: 0 20upx;
					border-radius: 30upx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28upx;
					margin-left: 20upx;
				}
				.default{
					border: solid 1upx #ccc;
					color: #666;
				}
				.primary{
					border: solid 1upx #0055ff;
					color: #0055ff;
				}
				.pay{
					border: solid 1upx #ec652f;
					color: #ec652f;
				}
			}
		}
	}
}
</style>
