<template>
	<view class="container" :style="{paddingTop:82+'px'}">
		<view class="order-section-top">
			<scroll-view scroll-x scroll-with-animation class="tab-view" :scroll-left="scrollLeft">
				<view v-for="(item,index) in orderTabBar" :key="index" class="tab-bar-item" :class="[currentTab==index ? 'active' : '']"
				 :data-current="index" @tap.stop="onClickTab">
					<text class="tab-bar-title">{{item.text}}</text>
				</view>
			</scroll-view>
			<view class="tab-screen">
				<view v-for="(item,index) in screenTabBar" :key="index" class="tab-screen-item" :class="[screenTab == index ? 'active' : '']"
				 :data-current="index" @tap.stop="onClickScreenTab(index)">{{item.text}}</view>
			</view>
		</view>
		<swiper class="tab-content" :current="currentTab" duration="80" @animationfinish="onChange" :style="{height:winHeight+'px'}" >
			<swiper-item v-for="(tabItem,index) in orderTabBar" :key="index">
				<tui-skeleton v-if="skeletonShow" backgroundColor="#fafafa" borderRadius="10rpx" :isLoading	="false" :loadingType="5"></tui-skeleton>
				<scroll-view scroll-y class="scoll-y tui-skeleton" @scrolltolower="scrolltolower" :style="{height:winHeight+'px'}" >
					<view  :class="{'tui-order-list':scrollTop >= 0}" class="tui-skeleton clearfix">
						<!-- 空白页 -->
						<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0" :typeIndex="currentTab" :navbarHeight="navbarHeight"></empty>
						<!-- 列表 -->
						<view v-else class="tui-order-content">
							<view  class="tui-order-item" v-for="(order,orderIndex) in tabItem.orderList" :key="orderIndex" @click.stop="detail(order.orderID)">
								<view class="order-title">
									<view class="order-title-name">{{order.clubName}}</view>
									<view class="order-title-t">
										<text class="bage-buss tui-skeleton-fillet" v-if="order.orderSubmitType == 3 || order.orderSubmitType == 4">协销</text>
										<text class="bage-auto tui-skeleton-fillet" v-if="order.orderSubmitType == 0 || order.orderSubmitType == 1 || order.orderSubmitType == 2">自主</text>
										<text class="bage-text tui-skeleton-fillet">订单号：{{order.orderNo}}({{order.orderID}})</text>
										<image class="bage-icon" src="../../../static/temp/icon-type@3x.png" mode="widthFix" v-if="order.secondHandOrderFlag == 1"></image>
									</view>
									<view class="order-title-b">
										<view class="order-title-btxt tui-skeleton-fillet">下单时间：{{order.orderTime}}</view>
										<view class="order-title-tip tui-skeleton-fillet">{{StateExpFormat(order.status)}}</view>
									</view>
								</view>
								<block v-for="(shop,index) in order.shopOrderList" :key="index">
									<view class="goods-title">
										<view v-if="shop.shopPromotion" class="floor-item-act">
											<view class="floor-tags">{{shop.shopPromotion.name}}</view>	
										</view>
										<view class="title-text tui-skeleton-fillet">{{shop.shopName}}</view>
									</view>
									<view class="goods-item" v-for="(pros,prosIndex) in shop.orderProductList" :key="prosIndex">
										<view class="goods-pros-t">
											<view class="pros-img tui-skeleton-fillet">
												<image :src="pros.productImage" alt="" />
												<text class="tips" v-if="pros.productType ==2 || pros.productType ==1">赠品</text>
											</view>
											<view class="pros-product clearfix">
												<view class="producttitle tui-skeleton-fillet">{{pros.name}}</view>
												<view class="productspec tui-skeleton-fillet" v-if="pros.productCategory != 2">规格:{{pros.productUnit ? pros.productUnit : ''}}</view>
												<view class="productprice">
													<view class="price tui-skeleton-fillet">
														<text>￥{{ pros.price | NumFormat }}</text>
													</view>
													<view class="count tui-skeleton-fillet">
														<text class="small">x</text>{{pros.num}}
													</view>
												</view>
												<view class="floor-item-act" v-if="pros.productPromotion!=null">
													<view v-if="PromotionsFormat(pros.productPromotion)"  class="floor-tags">
														{{pros.productPromotion.name}}
														<text v-if ="pros.productPromotion!=null && pros.productPromotion.type !=3 ">
															:￥{{ pros.productPromotion == null ? '0.00' : pros.productPromotion.touchPrice | NumFormat}}
														</text>
													</view>
													<view v-else-if="pros.productPromotion.type !=3" class="floor-tags">{{pros.productPromotion.name}}</view>	
												</view>
											</view>	
										</view>
									</view>	
								</block>
								<view class="order-footer">
									<view class="order-footer-top" v-if="order.discountFee!=0">经理折扣：¥{{ order.discountFee | NumFormat }}</view>
									<view class="order-footer-bot">
										<view class="count tui-skeleton-fillet">共{{order.productCount}}件商品</view>
										<view class="money tui-skeleton-fillet">应付总额：¥{{ order.payableAmount | NumFormat }}</view>
									</view>
								</view>
								<!-- 底部button -->
								<order-button ref="orderButton" 
											  :status="order.status" 
											  :orderID="order.orderID" 
											  :userID = "order.userID"
											  @buttonConfirm="handButtonConfirm">
								</order-button>
							</view>
							<!--加载loadding-->
							<tui-loadmore :visible="loadding" :index="3" type="black"></tui-loadmore>
							<tui-nomore :visible="!pullUpOn" bgcolor="#F7F7F7" :text='nomoreText'></tui-nomore>
							<!--加载loadding-->
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
		<!-- 分享弹窗 -->
		<share-alert   v-if="isShareModal"
					   :orderID="btnoRderID" 
					   @shareConfirm ='onShareAppMessage'>
		</share-alert>
		<!-- 透明模态层 -->
		<modal-layer v-if='isModalLayer'></modal-layer>
	</view>
</template>

<script>
	import headerBack from '@/components/cm-module/headerNavbar/header-back' 		//自定义顶部导航
	import btSearch from '@/components/uni-search/bt-search.vue'				//搜索
	import tuiSkeleton from "@/components/tui-skeleton/tui-skeleton"
	import tuiListCell from "@/components/tui-components/list-cell/list-cell"
	import tuiLoadmore from "@/components/tui-components/loadmore/loadmore"
	import tuiNomore from "@/components/tui-components/nomore/nomore"
	import orderButton from '@/components/cm-module/orderDetails/sellerOrderButton' //操作按钮
	import modalLayer from "@/components/modal-layer"
	import empty from "@/components/empty";
	import shareAlert from '@/components/cm-module/modelAlert/shareAlert'			 //分享弹窗
	import { cancelOrder,deleteOrder, affirmOrder } from "@/api/order.js"
	import { getSellerClubOrderList } from "@/api/seller.js" 
	
	export default {
		components: {
			headerBack,
			empty,
			btSearch,
			tuiListCell,
			tuiLoadmore,
			tuiNomore,
			orderButton,
			tuiSkeleton,
			modalLayer,
			shareAlert
		},
		data() {
			return {
				orderTabBar: [{listType: 0,text: '全部订单',orderList: []},
							  {listType: 1,text: '待确认',orderList: []},
							  {listType: 2,text: '待付款',orderList: []},
							  {listType: 3,text: '待发货',orderList: []},
							  {listType: 4,text: '已发货',orderList: []},
							  {listType: 5,text: '退货/款',orderList: []},
							 ],
				screenTabBar:[
								{type:0,text:'全部订单'},
								{type:1,text:'机构自主订单'},
								{type:2,text:'协销订单'}
							],			 
				winHeight: "", //窗口高度
				clubID:'',		//机构ID
				currentTab: 0, //预设当前项的值
				screenTab:0,   //筛选预设当前项的值
				scrollLeft: 0 ,//tab标题的滚动条位置
				serviceProviderId:0,//协销用户ID
				orderData: [],
				btnClubUserID:0,
				btnoRderID: 0, //点击按钮传入的的订单ID
				pageNum: 1,	  //页数
				pageSize: 10,  //条数
				scrollTop: 0,
				deteleType:'',
				skeletonShow: true,
				isClickChange: false,
				isShareModal: false,//控制分享弹窗
				isSeller:false,
				isModalLayer: false,
				loadding: false,
				pullUpOn: true,
				hasNextPage: false,
				pullFlag: true,
				navbarHeight:'',
				nomoreText: '上拉显示更多',
			}
		},
		onLoad(option) {
			let self = this
			this.clubID = option.clubID
			this.currentTab = option.listType
			//  高度自适应
			uni.getSystemInfo({
				success: function(res) {
					let calc = res.windowHeight;
					self.winHeight = calc-82;
				}
			});
		},
		filters:{
			NumFormat(value) {//处理金额
				return Number(value).toFixed(2);
			},
		},
		methods: {
			// 滚动切换标签样式
			onChange (e) {
				let index = e.target.current || e.detail.current;
				if (this.isClickChange) {
					this.currentTab = index;
					this.isClickChange = false;
					return;
				}
				this.isClickChange = false;
				this.currentTab = index; 
				this.checkCor();
				this.pageNum = 1
				this.pullUpOn = true //切换时隐藏
				this.loadding =  false //切换时隐藏
				this.nomoreText = ''
				this.getOrderDatainit(this.currentTab,'tabChange')
			},
			// 点击标题切换当前页时改变样式
			onClickTab (e) {
				let tabIndex = e.target.dataset.current || e.currentTarget.dataset.current;
				if (this.currentTab === tabIndex) {
					return false;
				} else {
					this.isClickChange = true;
					this.currentTab = tabIndex
					this.pageNum = 1
					this.pullUpOn = true //切换时隐藏
					this.loadding =  false //切换时隐藏
					this.getOrderDatainit(this.currentTab)
				}
			},
			onClickScreenTab(index){
				console.log(index)
				this.screenTab = index
				this.pageNum = 1
				this.pullUpOn = true //切换时隐藏
				this.loadding =  false //切换时隐藏
				this.nomoreText = ''
				this.getOrderDatainit(this.currentTab)
			},
			//判断当前滚动超过一屏时，设置tab标题滚动条。
			checkCor: function() {
				if (this.currentTab > 3) {
					//这里距离按实际计算
					this.scrollLeft = 300
				} else {
					this.scrollLeft = 0
				}
			},
			getOrderDatainit(index,source){
				setTimeout(()=>{this.skeletonShow = false},1500)
				let orderItem = this.orderTabBar[index];
				let listType = orderItem.listType;
				if(source === 'tabChange' && orderItem.loaded === true){//tab切换只有第一次需要加载数据
					return;
				}
				setTimeout(()=>{
					this.$api.getComStorage('orderUserInfo').then((resolve) =>{
						let params = {listType:index,clubID:resolve.clubID,orderSubmitType:this.screenTab,serviceProviderId:this.serviceProviderId,pageNum:1,pageSize:this.pageSize};
						getSellerClubOrderList(params).then(response =>{
							let orderList = response.data.results.filter(item=>{
								//添加不同状态下订单的表现形式
								item = Object.assign(item, this.StateExpFormat(item.listType));
								return item;
							});
							orderItem.orderList =[];
							orderList.forEach(item=>{
								orderItem.orderList.push(item);
							})
							//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
							this.$set(orderItem, 'loaded', true);
							this.hasNextPage = response.data.hasNextPage;
							if(this.hasNextPage){
								this.pullUpOn = false
								this.nomoreText = '上拉显示更多'
							}else{
								if(orderItem.orderList.length < 2){
									this.pullUpOn = true
								}else{
									this.pullUpOn = false
									this.nomoreText = '已至底部'
								}
							}
						}).catch(error =>{
							this.$util.msg(error.msg,2000);
						})
					})
				}, 600);	
			}, 
			getOnReachBottomData(index){//上拉加载
				let params = {listType:index,orderSubmitType:this.screenTab,serviceProviderId:this.serviceProviderId,pageNum:this.pageNum+=1,pageSize:this.pageSize};
				getSellerOrderList(params).then(response =>{
					let orderItem = this.orderTabBar[index];
					let resData = response.data.results
					this.hasNextPage = response.data.hasNextPage;
					orderItem.orderList = orderItem.orderList.concat(resData)
					this.pullFlag = false;// 防上拉暴滑
					setTimeout(()=>{this.pullFlag = true;},500)
					if(this.hasNextPage){
						this.pullUpOn = false
						this.nomoreText = '上拉显示更多'
					}else{
						this.loadding = false
						this.pullUpOn = false
						this.nomoreText = '已至底部'
					}
				}).catch(error =>{
					this.$util.msg(error.msg,2000)
				})
			},
			scrolltolower() {
				if(this.hasNextPage){
					this.loadding = true
					this.pullUpOn = true
					this.getOnReachBottomData(this.currentTab);
				}	
			},
			detail(id) {//订单详情跳转
				this.isModalLayer = true;	
				this.$api.navigateTo(`/seller/pages/order/order-details?listType=${this.currentTab}&orderID=${id}`)
			},
			handButtonConfirm(data) {//获取点击
				this.handShowAlert(data)
				this.btnoRderID = data.orderId
			},
			handShowAlert(data) {//执行
				switch(data.type){
					case 'delete':
						this.handOrderDetele(data.orderId);
						break
					case 'cancel':
						this.handCenceConfirm(data.orderId)
						break
					case 'confirm':
						this.handOrderConfirm(data.orderId);
						break
				}
			},
			handOrderConfirm (id){//确认订单
				this.$util.modal('提示','确认此订单？','确定','取消',true,() =>{
					affirmOrder({orderID:id}).then(response =>{
						this.$util.msg(response.msg,2000,true,'success');
						setTimeout(() => {
							this.getOrderDatainit(this.currentTab)
						},2000)
					}).catch(error =>{
						this.$util.msg(error.msg,2000)
					})
				})
			},
			handOrderDetele(id){//删除订单
				this.$util.modal('提示','确认删除该订单吗？','确定','取消',true,() =>{
					deleteOrder({orderID:id}).then(response =>{
						this.$util.msg(response.msg,2000,true,'success');
						setTimeout(() => {
							this.getOrderDatainit(this.currentTab)
						},2000)
					}).catch(error =>{
						this.$util.msg(error.msg,2000)
					})
				})
			},
			handCenceConfirm(id){//取消订单
				this.$util.modal('提示','确认取消该订单吗？','确定','取消',true,() =>{
					cancelOrder({orderID:id}).then(response =>{
						this.$util.msg(response.msg,2000,true,'success');
						setTimeout(() => {
							this.getOrderDatainit(this.currentTab)
						},2000)
					}).catch(error =>{
						this.$util.msg(error.msg,2000)
					})
				})
			},
			handlSearchPath(){
				this.$api.navigateTo('/seller/pages/search/search-order')
			},
			onShareAppMessage (res){//分享转发
				this.isShareModal = false
				if (res.from === 'button') {// 来自页面内转发按钮
					// console.log(res.target)
			    }
				return {
					title: '您有新的分享订单，快来查看吧~',
					path: `/pages/user/order/order-sharelogin?orderID=${this.btnoRderID}&userID=${this.btnClubUserID}&serviceProviderId=${this.serviceProviderId}`,
					imageUrl:'https://img.caimei365.com/group1/M00/03/95/Cmis216Sk_SABnOFABZCgCzFV_g063.png'
				}
			},
			PromotionsFormat(promo){//促销活动类型数据处理
				if(promo!=null){
					if(promo.type == 1 && promo.mode == 1){
						return true
					}else{
						return false
					}
				}
				return false
			},
			//订单状态文字和颜色
			StateExpFormat (state){
				let stateText = '',
					stateTextObject={
						0:'待确认',
						4:'交易完成',
						5:'订单完成',
						6:'已关闭',
						7:'交易全退',
						77:'交易全退',
						11:'待付款待发货',
						12:'待付款部分发货',
						13:'待付款已发货',
						21:'部分付款待发货',
						22:'部分付款部分发货',
						23:'部分付款已发货',
						31:'已付款待发货',
						32:'已付款部分发货',
						33:'已付款已发货',
						111:'待付款待发货',
					}
				Object.keys(stateTextObject).forEach(key => {
					if(key == state){
						stateText = stateTextObject[key]
					}
				})
				return stateText;
			},
			orderPriceToFixed (n){
				let price ='';
				price = n.toFixed(2);
				return price
			},
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onShow() {
			this.isModalLayer = false;
			this.getOrderDatainit(this.currentTab)
		}
	}
</script>

<style lang="scss">
	/*tabbar start*/
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}
	.order-section-top{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		background: #FFFFFF;
		.tab-screen{
			height: 60rpx;
			width: 702rpx;
			padding: 10rpx 24rpx;
			border-top: 1px solid #F7F7F7;
			border-bottom: 1px solid #F7F7F7;
			display: flex;
			justify-content: center;
			justify-items: center;
			.tab-screen-item{
				flex: 1;
				height: 60rpx;
				border-radius: 10rpx;
				background: #F7F7F7;
				margin-right: 22rpx;
				line-height: 66rpx;
				font-size: $font-size-28;
				color: #333333;
				text-align: center;
				&.active{
					color: $color-system;
				}
				&:last-child{
					margin-right: 0;
				}
			}
		}
	}
	.tab-view::before {
		content: '';
		position: absolute;
		border-bottom: 1rpx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		bottom: 0;
		right: 0;
		left: 0;
	}
	.tab-view {
		width: 100%;
		height: 80rpx;
		overflow: hidden;
		box-sizing: border-box;
		background: #fff;
		white-space: nowrap;
		border-top: 1px solid #F7F7F7;
	}
	.tab-bar-item {
		padding: 0;
		height: 80rpx;
		min-width: 80rpx;
		line-height: 80rpx;
		margin: 0 28rpx;
		display: inline-block;
		text-align: center;
		box-sizing: border-box;
		&.active {
			border-bottom: 6rpx solid $color-system;
		}
		.tab-bar-title {
			height: 80rpx;
			line-height: 80rpx;
			font-size:$font-size-28;
			color: $text-color;
		}
		&.active .tab-bar-title {
			color: $color-system !important;
		}
	}
	/*tabbar end*/
	page{
		background: #F7F7F7;
	}
	.container {
		padding-bottom: env(safe-area-inset-bottom);
		height: auto;
		position: relative;
	}
	.tui-order-content{
		width: 100%;
		height: auto;
	}
	.tui-order-list {
		width: 100%;
		position: relative;
	}
	.tui-order-item {
		display: flex;
		flex-direction: column;
		width: 702rpx;
		padding:0 24rpx;
		background: #fff;
		border-bottom: 20rpx solid #F7F7F7;
	}
	.order-title{
		width: 100%;
		height: auto;
		.order-title-name{
			width: 100%;
			height: 72rpx;
			border-bottom: 1px solid #F7F7F7;
			line-height: 72rpx;
			text-align: left;
			font-size: $font-size-28;
			color: #333333;
		}
		.order-title-t{
			width: 100%;
			height: 68rpx;
			float: left;
			line-height: 68rpx;
			position: relative;
			.bage-icon{
				width: 50rpx;
				height: 50rpx;
				display: block;
				position: absolute;
				right: 0;
				top: 9rpx;
			}
			.bage-buss{
				display: inline-block;
				width: 72rpx;
				height: 30rpx;
				background:radial-gradient(circle,rgba(255,39,180,1) 0%,rgba(193,77,245,1) 100%);
				border-radius: 4rpx;
				line-height: 30rpx;
				font-size: $font-size-22;
				text-align: center;
				color: #FFFFFF;
				margin-top: 8rpx;
			}
			.bage-auto{
				display: inline-block;
				width: 72rpx;
				height: 30rpx;
				background:radial-gradient(circle,rgba(255,180,39,1) 0%,rgba(245,142,77,1) 100%);
				border-radius: 4rpx;
				line-height: 30rpx;
				font-size: $font-size-22;
				text-align: center;
				color: #FFFFFF;
				margin-top: 8rpx;
			}
			.bage-text{
				display: inline-block;
				font-size: $font-size-28;
				line-height: 68rpx;
				text-align: left;
				color: $color-system;
				margin-left: 15rpx;
			}
		}
		.order-title-b{
			width: 100%;
			height: 40rpx;
			float: left;
			margin-top: 8rpx;
			.order-title-btxt{
				float: left;
				font-size: $font-size-28;
				line-height: 40rpx;
				color: #999999;
				text-align: lef
			}
			.order-title-tip{
				float: right;
				font-size: $font-size-28;
				line-height: 40rpx;
				text-align: right;
				color: #FF2A2A;
			}
		}
	}
	.goods-title{
		width: 100%;
		height: 56rpx;
		float: left;
		margin-top:10rpx;
		.floor-item-act{
			height: 56rpx;
			text-align: center;
			box-sizing: border-box;
			float: left;
			padding: 10rpx 0;
			margin-right: 12rpx;
			.floor-tags{
				float: left;
				height: 36rpx;
				border-radius: 4rpx;
				background-color: rgba(225, 86, 22, 0.1);
				line-height: 36rpx;
				color: $color-system;
				text-align: center;
				display: inline-block;
				padding:0 16rpx;
				font-size: $font-size-20;
			}
		}
		.title-text{
			width: 400rpx;
			overflow: hidden;
			text-overflow:ellipsis;
			white-space: nowrap;
			float: left;
			font-size: $font-size-28;
			color: $text-color;
			text-align: left;
			line-height: 56rpx;
			font-weight: bold;
		}
	}
	.goods-item{
		width: 100%;
		height: auto;
	}	
	.goods-pros-t{
		width: 100%;
		height: auto;
		padding:24rpx 0;
		.pros-img{
			float: left;
			width: 210rpx;
			height: 100%;
			border-radius: 10rpx;
			margin:0 26rpx 0 0;
			position: relative;
			.tips{
				display: inline-block;
				width: 80rpx;
				height: 40rpx;
				background-image: linear-gradient(214deg, #ff4500 0%, #ff5800 53%, #ff4367 100%);
				line-height: 40rpx;
				text-align: center;
				font-size: $font-size-24;
				color: #FFFFFF;
				border-radius:10rpx 0 10rpx 0 ;
				position: absolute;
				top:0;
				left: 0;
			}
			image{
				width: 210rpx;
				height: 210rpx;
				border-radius: 10rpx;
				border:1px solid #f3f3f3;
			}
		}
	}
	.pros-product{
		width: 460rpx;
		height: 100%;
		line-height: 36rpx;
		font-size: $font-size-26;	
		position: relative;
		float: left;
		.producttitle{
			width: 100%;
			display: inline-block;
			height: auto;							
			text-overflow:ellipsis;
			display: -webkit-box;
			word-break: break-all;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
			margin-bottom: 8rpx;
		}
		.productspec{
			height: 36rpx;
			color: #999999;
		}
		.productprice{
			height: 48rpx;
			width: 100%;
			float: left;
			.price{
				line-height: 48rpx;
				font-size: $font-size-28;
				width: 48%;
				color: #FF2A2A;
				float: left;
				font-weight: bold;
			}
			.count{
				height: 100%;
				float: right;
				position: relative;
				.small{
					color: #666666;
				}
			}
		}
		.floor-item-act{
			width: 100%;
			height: 56rpx;
			text-align: center;
			box-sizing: border-box;
			float: left;
			padding:0 0 10rpx 0;
			.floor-tags{
				float: left;
				height: 36rpx;
				border-radius: 4rpx;
				background-color: rgba(225, 86, 22, 0.1);
				line-height: 36rpx;
				color: $color-system;
				text-align: center;
				display: inline-block;
				padding:0 16rpx;
				font-size: $font-size-20;
			}
		}
	}
	.order-footer{
		width: 100%;
		height: 78rpx;
		float: left;
		.order-footer-top{
			width: 100%;
			height: 34rpx;
			line-height: 34rpx;
			font-size: $font-size-24;
			color: #999999;
			text-align: right;
		}
		.order-footer-bot{
			width: 100%;
			float: left;
			height: 48rpx;
			line-height: 48rpx;
			font-size: $font-size-28;
			font-weight: bold;
			color: $text-color;
			.count{
				width: 50%;
				float: left;
				text-align: left;
			}
			.money{
				width: 50%;
				float: right;
				text-align: right;
			}
		}
	}
</style>
