<template>
	<view class="content">
		<view class="top-fixed">
			 <u-tabs
				:current="status"
			  :scrollable="true"
			  :list="tabs"  
			  lineColor="#2583EB"
			 @change="tabChange">
			 </u-tabs>
		</view>
		<mescroll-body  top="88rpx" bottom="0"  ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="order-box">
				<view class="order-item" @click="showDetails(item)" v-for="(item) in dataList">
					<view class="top-box" >
						<view class="num">订单号：{{item.orderSn}}</view>
						<view class="status-box">
							<text   class="text info" v-if="item.status==1">
							{{$getDictLabelName(orderStatusOptions,item.status)}}
							</text>
							<text   class="text success" v-if="item.status>1">
							{{$getDictLabelName(orderStatusOptions,item.status)}}
							</text>
							<text   class="text info" v-if="item.status<0">
							{{$getDictLabelName(orderStatusOptions,item.status)}}
							</text>
						</view>
					</view>
					<view class="package-box">
						<view class="left">
							<image :src="item.imgUrl"></image>
						</view>
						<view class="right">
							<view class="title">{{item.packageName}}</view>
							<view class="desc">
								<view class="cycle">用药周期{{item.cycle}}天</view>
								<view class="duration">签约时长{{item.duration}}天</view>
							</view>
							<!-- <view class="price-box">
								<view class="price">￥30元/日</view>
								<view class="count">6.2w人已购</view>
							</view> -->
						</view>
					</view>
					<view class="bottom-box">
						<view class="amount-paid">
							<text class="label">订单金额：</text>
							<view class="price-box">
								<view class="unit">¥</view>
								<view class="num" >{{item.payPrice.toFixed(2)}}</view>
							</view>
						</view>
						<view class="btn-box">
							<view v-if="item.status == 1" class="btn cancel" @click.stop="cancelOrder(item)">取消订单</view>
							<view v-if="item.status == 1" class="btn pay" @click.stop="pay(item)">支付订单</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {cancelOrder,getMyPackageOrderList,pay} from '@/api/packageOrder'
	
	import {getDictByKey} from '@/api/common.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				orderStatusOptions:[],
				searchKey:"",
				status:"0",
				tabs: [
					{name:"全部",id:"0"},
					{name:"待支付",id:"1"},
					{name:"服务中",id:"2"},
					{name:"已完成",id:"3"},
					{name:"已取消",id:"-1"},
					{name:"已退款",id:"-2"}
				],
				mescroll:null,
				downOption: {   //下拉刷新
				 	use:true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll:false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore:"已经到底了",
					empty: {
						icon:'https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/empty_icon.png',
						tip: '暂无数据'
					}
				},
				dataList: []
			}
		},
		onLoad(options) {
			if(options.status!=null){
				this.status=options.status
			}
			var that=this;
			uni.$on('refreshPackageOrder', () => {
			 	that.mescroll.resetUpScroll()
			})
			this.getDictByKey("sys_package_order_status");
		},
		onShow() {
		},
		methods: {
			pay(item){
				uni.navigateTo({
					url: '/pages_order/packageOrderPay?orderId='+item.orderId
				})
			},
			getDictByKey(key){
				var data={key:key}
				getDictByKey(data).then(
					res => {
						if(res.code==200){
							if(key=="sys_package_order_status"){
								this.orderStatusOptions=res.data;
							}
						}
					},
					err => {
					}
				);
				
			},
			cancelOrder(item){
				var that=this;
				uni.showModal({
					title:"提示",
					content:"确认取消订单吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							// 用户点击确定
							var data={orderId:item.orderId}
							cancelOrder(data).then(
								res => {
									if(res.code==200){
										uni.$emit('refreshPackageOrder');
										uni.showToast({
										 	icon:'success',
										 	title: "操作成功",
										});
									}else{
										uni.showToast({
											icon:'none',
											title: res.msg,
										});
									}
								},
								rej => {}
							);
														
						}else{
							// 否则点击了取消  
						}
					}
				})
			},
			showDetails(item){
				uni.navigateTo({
					url:"/pages_order/packageOrderDetails?orderId="+item.orderId
				})
			},
			tabChange(item){
				this.status=item.id
				console.log(item)
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					status:this.status,
					pageNum: page.num,
					pageSize: page.size
				};
				getMyPackageOrderList(data).then(res => {
					if(res.code==200){
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list; 
							
						} else {
							that.dataList = that.dataList.concat(res.data.list);
							 
						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);
						
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f6f6f6;
	}
</style>
<style scoped lang="scss">
	.content{
		.top-fixed{
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 10;
			height: 88upx;
			background-color: #fff;
		}
		.order-box{
			padding: 20rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			.order-item{
				width: 100%;
				border-radius: 10rpx;
				background-color: #fff;
				padding: 30rpx;
				margin-bottom: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: flex-start;
				.top-box{
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.num{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1;
					}
					.status-box{
						display: flex;
						align-items: center;
						.text{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							line-height: 1;
							&.success{
								color: #2583EB;
							}
							&.black{
								color: #111111;
							}
							&.info{
								color: #999999;
							}
						}
					}
				}
				.package-box{
					margin-top: 20rpx;
					
					width: 100%;
					display: flex;
					align-items: flex-start;
					justify-content: flex-start;
					.left{
						width:200rpx;
						height:200rpx;
						image{
							border-radius: 15rpx;
							width:100%;
							height:100%;
						}
					}
					.right{
						height: 200rpx;
						display: flex;
						flex-direction: column;
						align-items: flex-start;
						justify-content: flex-start;
						padding-left: 15rpx;
						width:calc(100% - 200rpx);
						.title{
							font-weight: bold;
							font-size: 34upx;
							font-family: PingFang SC;
							color: #111;
						}
						.desc{
							margin-top: 15rpx;
							display: flex;
							align-items: flex-start;
							justify-content: flex-start;
							.cycle{
								background-color: #eee;
								border-radius: 30rpx;
								padding: 5rpx 15rpx;
								font-size: 26upx;
								font-family: PingFang SC;
								color: #2583EB;
							}
							.duration{
								margin-left: 10rpx;
								background-color: #eee;
								border-radius: 30rpx;
								padding: 5rpx 15rpx;
								font-size: 26upx;
								font-family: PingFang SC;
								color: #2583EB;
							}
							
						}
						.price-box{
							flex: 1;
							display: flex;
							align-items: flex-end;
							justify-content: space-between;
							width: 100%;
							.price{
								padding: 5rpx 10rpx;
								background-color: #2583EB;
								border-radius: 30rpx;
								font-size: 20upx;
								font-family: PingFang SC;
								color: #ffffff;
							}
							.count{
								font-size: 24upx;
								font-family: PingFang SC;
								color: #333333;
							}
							
						}
					}
					
					 
				}
				.bottom-box{
					margin-top: 20rpx;
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.amount-paid{
						display: flex;
						align-items: center;
						.label{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
						}
						.price-box{
							display: flex;
							align-items: flex-end;
							.unit{
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #FF6633;
								line-height: 1.2;
								margin-right: 4upx;
							}
							.num{
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #FF6633;
								line-height: 1;
							}
						}
					}
					.btn-box{
						box-sizing: border-box;
						display: flex;
						align-items: center;
						.btn{
							width: 155upx;
							height: 64upx;
							line-height: 64upx;
							font-size: 26upx;
							font-family: PingFang SC;
							font-weight: 500;
							text-align: center;
							border-radius: 32upx;
							margin-left: 15upx;
							&:first-child{
								margin-left: 0;
							}
							&.cancel{
								border: 1px solid #DDDDDD;
								color: #666666;
							}
							&.pay{
								background: #2583EB;
								color: #FFFFFF;
							}
						}
					}
				}
				
			}
			
		}
	}
</style>
