<template>
	<view class="pages">
		<view class="status-bar">
			<view class="status-bar-item" :class="params.status == -1?'active':''" @click="changestatus(-1)">全部</view>
			<view class="status-bar-item" :class="params.status == 0?'active':''" @click="changestatus(0)">待付款</view>
			<view class="status-bar-item" :class="params.status == 1?'active':''" @click="changestatus(1)">待发货</view>
			<view class="status-bar-item" :class="params.status == 2?'active':''" @click="changestatus(2)">已发货</view>
			<view class="status-bar-item" :class="params.status == 3?'active':''" @click="changestatus(3)">已完成</view>
			<view class="status-bar-item" :class="params.status == 4?'active':''" @click="changestatus(4)">已关闭</view>
			<!-- <view class="status-bar-item" :class="params.status == 5?'active':''" @click="changestatus(5)">无效</view> -->
		</view>
		<mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="order">
				<view v-for="(item,index) in orderlist" :key="index" class="order-item" @click="goOrderDetails(item.id)">
					<view class="status" v-if="item.status == 0">待付款</view>
					<view class="status" v-if="item.status == 1">待发货</view>
					<view class="status" v-if="item.status == 2">已发货</view>
					<view class="status" v-if="item.status == 3">已完成</view>
					<view class="status" v-if="item.status == 4">已关闭</view>
					<view class="status" v-if="item.status == 5">无效订单</view>
					<view class="order-info">
						<view class="order-info-imgwrap">
							<image :src="item.orderItemList[0].productPic" class="order-info-img" mode="aspectFill"></image>
						</view>
						<view class="order-info-text">
							<view class="order-info-title">
								{{item.orderItemList[0].productBrand}}{{item.orderItemList[0].productName}}
							</view>
							<view class="order-info-num">
								<view class="order-info-bynum">数量<text class="bynum">X{{item.orderItemList[0].productQuantity}}</text></view>
								<view class="order-info-amount">商品总额<text class="rmb">¥</text><text class="amount">{{item.orderItemList[0].productQuantity*item.orderItemList[0].realAmount}}</text></view>
							</view>
						</view>
					</view>
					<!-- order-info end -->
					<view class="order-tool" v-if="item.status == 0 || item.status == 2">
						<!-- <view class="order-tool-more">
							<svg class="icon" aria-hidden="true">
							    <use xlink:href="#iconfengyunbang"></use>
							</svg>
							更多操作
						</view> -->
						<!-- <button type="default" class="btn" v-if="item.status != 0" @click="delOrder">删除订单</button> -->
						<button type="default" class="btn" v-if="item.status == 0" @click.stop="cancelOrder(item)">取消订单</button>
						<button type="default" class="btn" v-if="item.status == 0" @click.stop="pay(item)">我要付款</button>
						<button type="default" class="btn" v-if="item.status == 2" @click.stop="received(item)">确认收货</button>
					</view>
				</view>
				<!-- order-item end -->
			</view>
			<!-- order end -->
		</mescroll-body>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog type="info"  :content="popupContent" :duration="2000" :before-close="true" @close="closepopup" @confirm="confirmpopup"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";	
	// 引入mescroll-body组件 (如已在main.js注册全局组件,则省略此步骤)
	import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"; // 注意.vue后缀不能省
	
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			MescrollBody,
			uniPopup,
			uniPopupDialog,
		},
		data() {
			return {
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: { 
					// ...
				},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 3, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				tokenhead:'',
				token:'',				
				params:{
					status:-1,
					// tokenhead:'',
					// token:'',
				},
				orderlist:[],
				cancelId:null,
				receiveId:null,
				popupContent:'',
				popupType:'',
				
			}
		},
		onShow() {
			let serf = this;
			//检查登录,获取token
			// let loginRes = this.checkLogin('/pages/index/index', '2');
			// if(!loginRes){return false;}
			// serf.params.token=loginRes[0];			
		},
		onLoad() {
			let self = this;
			uni.getStorage({
				key:'token',
				success: function (res) {
					self.token = res.data;
				   // console.log(res.data);
				}
			});
			uni.getStorage({
				key:'tokenhead',
				success: function (res) {
					self.tokenhead = res.data;
				   // console.log(res.data);
				}
			});

		},
		methods: {
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调, 有三种处理方式:*/
			downCallback(){			
				// 第2种: 下拉刷新和上拉加载调同样的接口, 那么不用第1种方式, 直接mescroll.resetUpScroll()即可
				this.mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			upCallback(page) {
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});				
				let thetoken = this.tokenhead + this.token;
				// console.log('thetoken',thetoken);
				this.$api.http.get(this.config.apiBaseurl+'/order/list',{params:this.params,header: {Authorization:thetoken}}).then(data => {
					// console.log('1111111111',data);
					if(data.data.code=='1001'){
						// uni.redirectTo({
						// 	url:'/pages/login/login?backpage=/pages/index/index'+'&backtype='+2,
						// });							
					};
					// console.log(data);
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.data.list||new Array(); 
					// console.log('curPageData',curPageData);
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					// let totalPage = data.xxx; 
					// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
					let totalSize = data.data.data.total; 
					// this.mescrollList = curPageData;
					// 接口返回的是否有下一页 (true/false)
					// let hasNext = data.xxx; 
					
					//设置列表数据
					if(page.num == 1) this.orderlist = []; //如果是第一页需手动置空列表
					this.orderlist = this.orderlist.concat(curPageData); //追加新数据
					// console.log('page.num',page.num);
					console.log('this.orderlist',JSON.parse(JSON.stringify(this.orderlist)));
					
					//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
					this.mescroll.endBySize(curPageLen, totalSize); 
			
					// setTimeout(()=>{
					// 	this.mescroll.endSuccess(curPageLen)
					// },20)
				
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				
				});
			},
			//删除订单
			delOrder(item){
				
			},
			cancelOrder(item){
				console.log(JSON.parse(JSON.stringify(item)));
				this.cancelId = item.id;
				this.$refs.popup.open();
				this.popupContent = '确认取消此订单吗?';
				this.popupType = 'cancel';				
			},
			received(item){
				this.receiveId = item.id;
				this.$refs.popup.open();
				this.popupContent = '确认收货?';
				this.popupType = 'received';
			},
			closepopup(done){
				done()
			},
			confirmpopup(done,value){
				if(this.popupType == 'cancel'){
					this.$api.http.post(this.config.apiBaseurl + '/order/cancelUserOrder/?orderId='+this.cancelId,{},{
						header: {
						Accept:'application/json',
						Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
					},
					}).then(res => {
						this.downCallback();
						// console.log('res',res)
					}).catch(err =>{
						console.log('err',err)
					});
				}else if(this.popupType == 'received'){
					this.$api.http.post(this.config.apiBaseurl + '/order/confirmReceiveOrder/?orderId='+this.receiveId,{},{
						header: {
						Accept:'application/json',
						Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
					},
					}).then(res => {
						this.downCallback();
						// console.log('res',res)
					}).catch(err =>{
						console.log('err',err)
					});
				}
				
				done()
			},
			changestatus(status){
				this.params.status  = status;
				this.downCallback();
			},
			goOrderDetails(id){
				console.log(id)
				uni.navigateTo({
					url: './orderdetails/orderdetails?orderid=' + id,
				});
			},
			pay(item){
				let userInfo = uni.getStorageSync("userInfo");
					let params = {
						orderId:item.id,
						payType:item.payType,
						openid:userInfo.wxUsers.openid,
						// "tradeType":"test"
					}
					this.$pay.wxPay(params).then(res =>{
						this.downCallback();
						// console.log('payres',res);
					}).catch(err =>{console.log('err',err);});
			},

		}
	}
</script>

<style scoped>
@import url("./myorder.css");
</style>
