<template>
	<view>
		<view class="top-fixed">
			<!-- tab切换 -->
			<view class="pub-tab-box">
				<view class="tab-inner">
					<view 
						v-for="(item,index) in orderStatus" 
						:key="index"
						:class="status ==item.value?'item active':'item'"
						@click="orderStatusChange(item)">
						<view class="text">
							{{ item.name }}
							<image v-show="status ==item.value" class="tab-bg" src="https://kuanyitang-1317640934.cos.ap-shanghai.myqcloud.com/shop04/tab_bg.png" mode=""></image>
						</view>
					</view>
				</view>
			</view>
			
			
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="https://kuanyitang-1317640934.cos.ap-shanghai.myqcloud.com/shop04/search.png" mode=""></image>
					<input type="text" value="" placeholder="输入订单号" confirm-type="搜索" @confirm="goSearch"   placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
			</view>
		</view>
		<view class="top-seat"></view>
		
		<!-- 订单列表 -->
		<mescroll-body 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">
							<!-- <view   class="recom-box">推荐订单</view> -->
							<text   class="text success">
								{{utils.getDictLabelName("storeOrderStatus",item.status)}}
							</text>
						</view>
					</view>
					<!-- 药品列表 -->
					<view  class="drug-list"  >
						<view @click="showDetail(item)"  v-if="item.isPackage!=1" v-for="(subItem,subIndex) in item.items" :key="subIndex" class="drug-item" >
							<view class="img-box">
								<image :src="JSON.parse(subItem.jsonInfo).image" mode="aspectFill"></image>
							</view>
							<view class="drug-info"  >
								<view>
									<view class="name-box ellipsis2">
										<view v-if="subItem.isPrescribe==1" class="tag">处方药</view>{{JSON.parse(subItem.jsonInfo).productName}}
									</view>
									<view class="spec ellipsis2">{{JSON.parse(subItem.jsonInfo).sku}}</view>
								</view>
								<view class="num-box">
									<view class="price">
										<text class="unit">¥</text>
										<text class="num">{{JSON.parse(subItem.jsonInfo).price.toFixed(2)}}</text>
									</view>
									<view class="amount">x{{JSON.parse(subItem.jsonInfo).num}}</view>
								</view>
							</view>
						</view>
						<view v-if="item.isPackage==1&&item.packageJson!=null" class="drug-item" @click="showDetail(item)">
							<view class="img-box">
								<image :src="JSON.parse(item.packageJson).imgUrl" mode="aspectFill"></image>
							</view>
							<view class="drug-info"  >
								<view>
									<view class="name-box ellipsis2">
										<view class="tag">套餐</view>{{JSON.parse(item.packageJson).title}}
									</view>
									<view class="spec ellipsis2">{{JSON.parse(item.packageJson).descs}}</view>
								</view>
								<!-- <view class="num-box">
									<view class="price">
										<text class="unit">¥</text>
										<text class="num">{{JSON.parse(item.packageJson).payMoney.toFixed(2)}}</text>
									</view>
									<view class="amount"></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 == 0" class="btn cancel" @click="cancel(item)">取消订单</view>
								<view v-if="item.status == 0" class="btn pay" @click="pay(item)">支付</view>
								<view v-if="item.isAfterSales==1" class="btn cancel" @click="refund(item)">申请售后</view>
								<view v-if="item.status >=2 &&item.deliveryId!=null" class="btn pay" @click.stop="showDelivery(item)">查看物流</view>
								<!-- <view v-if="item.status==4" class="btn pay">再次购买</view> -->
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<ykscreenRecord></ykscreenRecord>
	</view>
</template>

<script>
	import {getMyStoreOrderList,cancelOrder} from '@/api/storeOrder'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import ykscreenRecord from "@/components/yk-screenRecord/yk-screenRecord.vue"
	export default {
		components:{
			ykscreenRecord
		},
		mixins: [MescrollMixin], 
		data() {
			return {
				searchKey:"",
				status:0,
				orderStatus: [
					{name:"全部",value:""},
					{name:"待付款",value:"0"},
					{name:"待发货",value:"1"},
					{name:"待收货",value:"2"},
					{name:"已完成",value:"3"}
				],
				mescroll:null,
				// 上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'https://kuanyitang-1317640934.cos.ap-shanghai.myqcloud.com/shop04/no_data.png',
						tip: '暂无数据'
					}
				},
				// 列表数据
				dataList: [],
			};
		},
		onLoad(option) {
			this.status = option.status
			var that=this;
			uni.$on('refreshOrder', () => {
				that.mescroll.resetUpScroll()
			});
		},
		methods: {
			goSearch(e) {
				this.searchKey=e.detail.value;
				this.mescroll.resetUpScroll()
			},
			refund(item) {
				uni.navigateTo({
					url: './refundOrderProduct?orderId='+item.id
				})	
			},
			// tab切换
			orderStatusChange(item) {
				this.status = item.value
				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,
					page: page.num,
					pageSize: page.size
				};
				getMyStoreOrderList(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();
					}
				});
			},
			// 查看订单详情
			showDetail(item) {
				console.log(item)
				uni.navigateTo({
					url: './storeOrderDetail?id=' + item.id
				})
			},
			cancel(item){
				var that=this;
				uni.showModal({
				    title: '提示',
				    content: '确定取消订单吗',
				    success: function (res) {
				        if (res.confirm) {
							var data = {
								orderId:item.id
							};
							cancelOrder(data).then(res => {
								if(res.code==200){
									uni.showToast({
										icon:'success',
										title: "操作成功",
									});
									 that.mescroll.resetUpScroll()
								}else{
									uni.showToast({
										icon:'none',
										title: res.msg,
									});
								}
							});
				        } 
						else if (res.cancel) {
							
				        }
				    }
				});
			},
			pay(item) {
				if(item.isPrescribe==1 && item.prescribeId==null){
					uni.navigateTo({
						url:"/pages/shopping/prescribe?orderId="+item.id
					});
				}
				else{
					uni.navigateTo({
						url: '/pages/shopping/paymentOrder?orderId='+item.id
					})
				}
			},
			// 查看物流
			showDelivery(item) {
				uni.navigateTo({
					url: './storeOrderDelivery?orderId='+item.id
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.top-fixed{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 10;
	}
	.pub-tab-box{
		box-sizing: border-box;
		width: 100%;
		padding: 0 40upx;
		background-color: #FFFFFF;
		.tab-inner{
			height: 88upx;
			line-height: 88upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			overflow-x: auto;
		}
		.item{
			font-size: 28upx;
			white-space: nowrap;
			line-height: 1;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
			margin-right: 60upx;
			display: flex;
			align-items: center;
			justify-content: center;
			&:last-child{
				margin-right: 0;
			}
			&.active{
				font-weight: bold;
				color: #333333;
			}
			.text{
				position: relative;
				z-index: 1;
			}
			.tab-bg{
				width: 72upx;
				height: 28upx;
				position: absolute;
				top: 17upx;
				left: 50%;
				transform: translateX(-36upx);
				z-index: -1;
			}
		}
	}
	.top-seat{
		width: 100%;
		height: 192upx;
	}
	.search-cont{
		padding: 16upx 30upx;
		background-color: #FFFFFF;
		.inner{
			box-sizing: border-box;
			width: 100%;
			height: 72upx;
			background: #F7F7F7;
			border-radius: 36upx;
			display: flex;
			align-items: center;
			padding: 0 30upx;
			.icon-search{
				width: 28upx;
				height: 28upx;
				margin-right: 20upx;
			}
			input{
				height: 60upx;
				line-height: 60upx;
				flex: 1;
			}
		}
	}
	.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;
					.recom-box{
						width: 108upx;
						height: 30upx;
						line-height: 30upx;
						text-align: left;
						padding-left: 8upx;
						font-size: 22upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						background-image: url(https://kuanyitang-1317640934.cos.ap-shanghai.myqcloud.com/shop04/recom.png);
						background-repeat: no-repeat;
						background-size: 100% 100%;
						margin-right: 8upx;
					}
					.text{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						line-height: 1;
						&.success{
							color: #C39A58;
						}
						&.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, #f95d44 0%, #C39A58 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{
							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: #C39A58;
								color: #FFFFFF;
							}
						}
					}
				}
			}
		}
	}
	
</style>
