<template>
	<view class="content">
		<view class="top-fixed">
			<!-- tab切换 -->
			<view class="pub-tab-box">
				<view class="tab-inner">
					<view 
						v-for="(item,index) in orderStatus" 
						:key="index"
						:class="item.val == orderStatusValue?'item active':'item'"
						@click="orderStatusChange(item)"
					>
						<view class="text">
							{{ item.name }}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="consu-list">
			<mescroll-body top="88upx" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<view v-if="orderStatusValue==0">
				<view v-for="(item,index) in dataList" :key="index" class="item" >
					<view class="item-top">
						<view class="item-title">
							<view class="ask-text">{{item.productName}}</view>
							<view class="fs28">X {{item.purchaseQuantity}}</view>
						</view>
						<view class="ask-time">购买次数：{{item.purchaseTimes}}</view>
						<view class="ask-time">购买时间：{{utils.timeFormat(item.createTime,'yyyy-mm-dd hh:MM')}}</view>
						<view class="ask-time">备注：{{item.remark||''}}</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.amount.toFixed(2)}}</view>
							</view>
						</view>
						<view class="btn-box">
							<view class="btn cancel" @click="deleteService(item)">删除</view>
							<view class="btn pay" @click="showDetail(orderStatusValue,item)">编辑</view>
						</view>
					</view>
				</view>
				</view>
				<view v-if="orderStatusValue==1">
				<view v-for="(item,index) in dataList" :key="index" class="item" >
					<view class="item-top">
						<view class="item-title">
							<view class="ask-text">{{item.product}}</view>
							<!-- <view class="fs28">X {{item.freeServiceTimes}}</view> -->
						</view>
						<view class="ask-time">服务次数：{{item.freeServiceTimes}}</view>
						<view class="ask-time">购买时间：{{utils.timeFormat(item.createTime,'yyyy-mm-dd hh:MM')}}</view>
						<view class="ask-time">备注：{{item.remark||''}}</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.price.toFixed(2)}}</view>
							</view>
						</view>
						<view class="btn-box">
							<view class="btn cancel" @click="deleteCompetitor(item)">删除</view>
							<view class="btn pay" @click="showDetail(orderStatusValue,item)">编辑</view>
						</view>
					</view>
				</view>
				</view>
				<view class="h160"></view>
			</mescroll-body>
		</view>
		<view class="btn-box2">
			<view class="sub-btn" @click="navgetTo(orderStatusValue)">
				<image src="/static/images/health/nav_add_icon24.png" class="w48 h48"></image>
				<text>新增</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {getServiceList,getCompetitorList,deleteCompetitor,deleteService} from '@/api/companyUser.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				orderStatus:[
					{name:"购买服务信息",val:0},
					{name:"购买竞品信息",val:1},
				],
				orderStatusValue:0,
				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://user.test.ylrztop.com/images/empty_icon.png',
						tip: '暂无数据'
					}
				},
				// 列表数据
				dataList: [],
			}
		},
		onLoad(options) {
			this.userId=options.userId;
			this.companyUserId=options.companyUserId;
			uni.$on('refreshBuyOrder', () => {
				this.mescroll.resetUpScroll()
			})
		},
		methods: {
			navgetTo(index){
				uni.navigateTo({
					url: index==0?'/pages_company/addServe?type=add&userId='+this.userId+"&companyUserId="+this.companyUserId:'/pages_company/addCompetitors?type=add&userId='+this.userId+"&companyUserId="+this.companyUserId
				})
			},
			// tab切换
			orderStatusChange(item) {
				this.orderStatusValue = item.val
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				if(this.orderStatusValue==0){
					this.getServiceList(page)
				}else if(this.orderStatusValue==1){
					this.getCompetitorList(page)
				}
			},
			getServiceList(page){
				//联网加载数据
				var that = this;
				var data = {
					userId:this.userId,
					// status:this.orderStatusValue,
					pageNum: page.num,
					pageSize: page.size
				};
				getServiceList(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();
					}
				});
			},
			getCompetitorList(page){
				//联网加载数据
				var that = this;
				var data = {
					// status:this.orderStatusValue,
					userId:this.userId,
					// companyUserId:this.companyUserId,
					pageNum: page.num,
					pageSize: page.size
				};
				getCompetitorList(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();
					}
				});
			},
			//删除商品
			deleteService(item){
				var that=this;
				uni.showModal({
				    title: '提示',
				    content: '确定删除商品订单吗',
				    success: function (res) {
				        if (res.confirm) {
							var data = {
								id:item.id
							};
							deleteService(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) {
				        }
				    }
				});
			},
			//删除竞品
			deleteCompetitor(item){
				var that=this;
				uni.showModal({
				    title: '提示',
				    content: '确定删除竞品订单吗',
				    success: function (res) {
				        if (res.confirm) {
							var data = {
								id:item.id
							};
							deleteCompetitor(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) {
				        }
				    }
				});
			},
			showDetail(index,item) {
				uni.navigateTo({
					url: index==0?'/pages_company/addServe?type=edit&id='+item.id+'&userId='+this.userId+"&companyUserId="+this.companyUserId:'/pages_company/addCompetitors?type=edit&id='+item.id+'&userId='+this.userId+"&companyUserId="+this.companyUserId
				})
			}
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 20upx;
		.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: center;
				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;
					&::after {
						content: "";
						width: 48rpx;
						height: 8rpx;
						background: linear-gradient(120deg, #31A1FE  0%, #008FD3 100%);
						position: absolute;
						bottom: 0;
						border-radius: 6upx 6upx 0upx 0;
					}
				}
				.text{
					position: relative;
					z-index: 1;
				}
				.tab-bg{
					width: 72upx;
					height: 28upx;
					position: absolute;
					top: 17upx;
					left: 50%;
					transform: translateX(-36upx);
					z-index: -1;
				}
			}
		}
		.btn-box2{
			z-index: 9999;
			width: 100%;
			padding: 30upx;
			position: fixed;
			bottom: 0;
			left: 0;
			box-sizing: border-box;
			// background: #FFFFFF;
			
			 
			.sub-btn{
				width: 100%;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background: #008FD3;
				border-radius: 44upx;
				display: flex;
				align-items: center;
				justify-content: center;
				image{
					margin-right: 16rpx;
				}
			}
		}
		.consu-list{
			.item{
				background: #FFFFFF;
				border-radius: 16upx;
				padding: 32rpx;
				position: relative;
				margin-bottom: 20upx;
				.item-top{
					padding-bottom:40rpx ;
					border-bottom: 1px solid #ECECEC;
					.item-title{
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 12rpx;
						
					}
					.ask-text{
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 36rpx;
						color: #222426;
						text-align: left;
					}
					.ask-time{
						font-size: 24rpx;
						color: #898E91;
						text-align: left;
						line-height: 40rpx;
					}
				}
				.ordersn-box{
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0upx 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(../../static/images/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: #0bb3f2;
							}
							&.black{
								color: #111111;
							}
							&.info{
								color: #999999;
							}
						}
					}
				}
				.doctor-info{
					display: flex;
					align-items: center;
					.img-box{
						width: 80upx;
						height: 80upx;
						background: #E0FFF8;
						border-radius: 50%;
						overflow: hidden;
						margin-right: 20upx;
						image{
							width: 100%;
							height: 100%;
						}
					}
					.name{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #666666;
						line-height: 48upx;
						text{
							margin-right: 10upx;
						}
					}
				}
				
				.patient-text{
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					margin-bottom: 24upx;
				}
			
				.bottom-box{
					height: 80upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;
					.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: 600;
								color: #FF5030;
								line-height: 1.2;
								margin-right: 4upx;
							}
							.num{
								font-size: 36upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #FF5030;
								line-height: 1;
							}
						}
					}
					.btn-box{
						box-sizing: border-box;
						display: flex;
						align-items: center;
						.btn{
						   width: 128rpx;
						    height: 60rpx;
							line-height: 60upx;
							font-size: 24upx;
							font-family: PingFang SC;
							text-align: center;
							border-radius: 32upx;
							margin-left: 15upx;
							&:first-child{
								margin-left: 0;
							}
							&.cancel{
								border: 1px solid #B2B2B2;
								color: #666666;
							}
							&.pay{
								border: 1px solid #008FD3;
								color: #008FD3;
							}
						}
					}
				}
			}
		}
		
	}
</style>
