<template>
	<view class="content">
		<view class="top-fixed">
			 <u-tabs
			:current="current"
			  :scrollable="true"
			  :list="tabs"  
			  lineColor="#FF233C"
			 @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-list">
				<view  v-for="(item,index) in dataList" :key="index" class="item" >
					<!-- 订单号，状态 -->
					<view class="ordersn-box" @click="showDetail(item)">
						<view class="num">订单号：{{item.orderCode}}</view>
						<view class="status-box">
							<text   class="text success" v-if="item.status>1&&item.status<5">
							{{$getDictLabel2Name(orderStatusOptions,item.status)}}
							</text>
							<text   class="text info" v-else>
							{{$getDictLabel2Name(orderStatusOptions,item.status)}}
							</text>
						</view>
					</view>
					<view  class="drug-list"  >
						<view @click="showDetail(item)" v-for="(subItem,subIndex) in item.items" :key="subIndex" class="drug-item" >
							<view class="img-box">
								<image :src="subItem.imgUrl==''?'https://hdtobs.obs.cn-north-4.myhuaweicloud.com/miniapp/drug.svg':subItem.imgUrl" mode="aspectFill"></image>
							</view>
							<view class="drug-info"  >
								<view>
									<view class="name-box ellipsis2">
										{{subItem.goodsName}}
									</view>
								</view>
								<view class="num-box">
									<view class="price">
										<text class="num" >{{subItem.integral}}</text>
										<text class="unit">积分</text>
										<text class="num" v-show="subItem.cash>0">+{{subItem.cash}}</text>
										<text class="unit" v-show="subItem.cash>0">元</text>
									</view>
									<view class="amount"  >x{{subItem.num}}</view>
								</view>
							</view>
						</view>
						<!-- 实付金额、按钮 -->
						<view class="bottom-box">
							<view class="amount-paid">
								<!-- <text class="label">使用积分：</text> -->
								<view class="price-box">
									<view class="num" >{{item.integral}}</view>
									<view class="unit">积分</view>
									<view class="unit" v-show="item.payMoney>0">+</view>
									<view class="num" v-show="item.payMoney>0">{{item.payMoney}}</view>
									<view class="unit" v-show="item.payMoney>0">元</view>
								</view>
							</view>
							<view class="btn-box">
								<view class="btn pay" v-if="item.status==4" @click.stop="pay(item)">去支付</view>
								<view class="btn cancel" v-if="item.status==4" @click.stop="cancelPay(item)">取消订单</view>
								<!-- <view class="btn cancel">
									联系客服
									<button class='contact-btn' open-type="contact">
									</button>
								</view> -->
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
 import {getStoreDictByKey} from '@/api/common.js'
 import {getIntegralOrderList,cannelOrder} from '@/api/integral.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:"4"},
				{name:"待发货",id:"1"},
				{name:"待收货",id:"2"},
				{name:"已完成",id:"3"},
				{name:"已取消",id:"5"}
			],
			mescroll:null,
			// 上拉加载的配置
			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://hdtobs.obs.cn-north-4.myhuaweicloud.com/hdt/empty_icon.png',
					tip: '暂无数据'
				}
			},
			// 列表数据
			dataList: [],
			current: 0
 		}
 	},
	onLoad(option) {
		this.current = option.current || 0
		this.status = this.tabs[this.current].id
		this.getDictByKey("sys_integral_order_status");
	},
	onShow() {
		if(this.mescroll) {
			this.mescroll.resetUpScroll()
		}
	},
 	methods: {
		getDictByKey(key){
			var data={key:key}
			getStoreDictByKey(data).then(
				res => {
					if(res.code==200){
						if(key=="sys_integral_order_status"){
							this.orderStatusOptions=res.data;
						}
					}
				},
				err => {
				}
			);
			
		},
		tabChange(item) {
			this.status = item.id
			this.mescroll.resetUpScroll()
		},
		mescrollInit(mescroll) {
			this.mescroll = mescroll;
		},
		/*下拉刷新的回调 */
		downCallback(mescroll) {
			mescroll.resetUpScroll()
		},
		upCallback(page) {
			//联网加载数据
			var that = this;
			var data = {
				keyword:this.searchKey,
				status:this.status,
				pageNum: page.num,
				pageSize: page.size
			};
			getIntegralOrderList(data).then(res => {
				if(res.code==200){
					res.data.list = res.data.list.map(v => ({
					  ...v,
					  items: [].concat(JSON.parse(v.itemJson || '[]')).filter(Boolean)
					}));
					//设置列表数据
					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();
				}
			});
		},
		// 查看订单详情
		showDetail(item) {
			uni.navigateTo({
				url: './integralOrderDetails?orderId=' + item.orderId
			})
		},
		pay(item) {
			 uni.navigateTo({
			 	url: '/pages_points/integralPayment?orderId='+item.orderId
			 })
		},
		cancelPay(item) {
			cannelOrder({orderId:item.orderId}).then(res=>{
				if(res.code ==200) {
					uni.showToast({
						title: '取消成功',
						icon: 'none'
					})
					this.mescroll.resetUpScroll()
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		}
 	}
 }
 
 
</script>


<style scoped lang="scss">
	::v-deep  .u-tabs__wrapper__nav__item{
		padding: 0 26rpx !important;
	}


.top-fixed{
		width: 100%;
		position: fixed;
		top: 0;
		left: 50%;
		transform: translateX(-50%);
		z-index: 10;
		height: 88upx;
		background-color: #fff;
	}
	.order-list{
		padding: 20upx;
		.item{
			background: #FFFFFF;
			border-radius: 16upx;
			padding: 0 30upx;
			margin-bottom: 20upx;
			.ordersn-box{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 34upx 0 20upx;
				.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: #FF233C;
						}
						&.black{
							color: #111111;
						}
						&.info{
							color: #999999;
						}
					}
				}
			}
			.drug-list{
				.drug-item{
					padding: 30upx 0;
					border-bottom: 1px soli #F0F0F0;
					display: flex;
					align-items: center;
					.img-box{
						width: 160upx;
						height: 160upx;
						margin-right: 30upx;
						flex-shrink: 0;
						image{
							width: 100%;
							height: 100%;
						}
					}
					.drug-info{
						width: calc(100% - 190upx);
						height: 160upx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.name-box{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 40upx;
							.tag{
								display: inline-block;
								padding: 0 6upx;
								height: 30upx;
								background: linear-gradient(90deg, #FF233C 0%, #E2C99E 100%);
								border-radius: 4upx;
								margin-right: 10upx;
								font-size: 22upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #FFFFFF;
								line-height: 30upx;
								float: left;
								margin-top: 7upx;
							}
						}
						.spec{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
							margin-top: 10upx;
						}
						.num-box{
							display: flex;
							align-items: center;
							justify-content: space-between;
							.price{
								display: flex;
								align-items: flex-end;
								.unit{
									font-size: 24upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #111111;
									line-height: 1.2;
									margin-right: 4upx;
								}
								.num{
									font-size: 32upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #111111;
									line-height: 1;
								}
							}
							.amount{
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #999999;
								line-height: 1;
							}
						}
					}
				}
				.bottom-box{
					height: 110upx;
					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{
							position: relative;
							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: #FF233C;
								color: #FFFFFF;
							}
							.contact-btn {
								top: 0;
								position: absolute;
								width:100%;
								height:100%;
								opacity: 0;
							}
						}
					}
				}
			}
		}
	}
</style>
