<template>
	<view class="wrap">
		<view class="banner">
			<u-swiper :list="imgList" name='url' mode="number" height="420" indicator-pos="topRight"></u-swiper>
		</view>
		<view class="middle">
			<view class="tabbar">
				<text v-for="(item,index) in tabbar" :key='index' @click="tabbarChange(index)" :class="{'on':active==index}">
					{{item}}
				</text>
			</view>
			<swiper :current="active"  @animationfinish="animationfinish" class="swiper-box">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height:100%;width: 100%;">
					<checkbox-group @change="checkboxChange">
						<view class="merchant" v-for="(item,index) in scenicList" :key='index'>
							<view class="merchant-title">
								<view class="title-left">
									<text >{{item.name}}</text>
									<image src="../../../static/img/scenic/coupon.png" ></image>
									<text class="title-coupon">
										劵惠
									</text>
								</view>
								
								<view v-if="showDistance" class="title-right">
									距您{{item.distance?parseFloat((item.distance/1000).toFixed(2)):0}}km
								</view>							
							</view>
							
								<label v-for="(items,a) in item.productListCopy" :key='a'>
									<view class="product" :class="{'on':items.ifChoose}">
										<view class="product-left">
											<checkbox class="blue" :disabled="items.status==2" :value="JSON.stringify(items)" :checked="items.ifChecked" />
										</view>
										
										<view class="product-right">
											<view class="product-name">
												<text>{{items.name}}</text>
												<text class="product-status" v-if='items.status==2'>{{items.statusName}}</text>
											</view>
											
											<view class="product-price">
												<view style="font-size: 24rpx;">
													<text class="product-rule" v-if="items.productRefundRuleVo">
														<text v-if="items.productRefundRuleVo.refundType==0">不可退</text>
														<text v-if="items.productRefundRuleVo.refundType==1">规定时间退</text>
														<text v-if="items.productRefundRuleVo.refundType==2">随时可退</text>
													</text>
													
													<text class="product-rule">
														无需取票
													</text>
													<text @click="showBuyKnow(items)">购买须知</text> 
													<u-icon name="arrow-right"></u-icon>
												</view>
												<view>
													<text class="product-orange">
														¥{{items.originalPrice}}
													</text>
													<text class="product-sell">
														<text style="font-size: 28rpx;">¥</text>{{items.sellingPrice}}
													</text>
												</view>
											</view>
										</view>
									</view>
								</label>
							
							<view class="product-more" v-if="index>0&&item.productList.length>2" >
								<view v-if="item.productListCopy.length!=item.productList.length" @click="showMoreProduct(item)">
									更多
									<u-icon name='arrow-down'></u-icon>
								</view>
								
								<view v-else @click="retractProduct(item)">
									收起
									<u-icon name='arrow-up'></u-icon>
								</view>
							</view>
						</view>
					</checkbox-group>
					<u-empty text="空空如也..." mode="list" v-if="scenicList.length==0"></u-empty>
					</scroll-view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<u-empty text="空空如也2..." mode="list"></u-empty>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<u-empty text="空空如也3..." mode="list"></u-empty>
				</swiper-item>
				<swiper-item class="swiper-item">
					<u-empty text="空空如也4..." mode="list"></u-empty>
				</swiper-item>
			</swiper>
		</view>
		<view class="bottom">
			<view class="bottom-left" @click="showCustomer()">
				<u-icon name='chat' size='36' style="margin-right: 8rpx;position: relative;top: 2rpx;"></u-icon>
				客服
			</view>
			<view class="bottom-middle">
				<view class="bottom-more" v-show="chooseProduct.length>0">
					组合购更优惠
					<text class="bottom-three"></text>
				</view>
				
				<view class="bottom-detail" @click="showDetail()" :style="{'top':chooseProduct.length>0?'-28rpx':'0'}">
					<text>
						明细
					</text>
					<u-icon name='arrow-up' size='32' style="margin: 0 8rpx;"></u-icon>
					<text class="bottom-number">
						{{chooseProduct.length}}
					</text>
				</view>
			</view>
			<view class="bottom-right">
				<text class="btn">去预定</text>
			</view>
		</view>
		<!-- 客服组件 -->
		<customer :scenicList='scenicList' ref='customer'></customer>
		<!-- 购买须知 -->
		<buyKnow :buyKnowData='buyKnowData' ref='buyKnow'></buyKnow>
		<!-- 模态框 -->
		<u-modal 
			v-model="showModal" 
			@confirm="chooseConfirm" 
			@cancel='chooseCancel' 
			title='提示'
			content='该产品不支持多选'
			show-cancel-button=true >
		</u-modal>
	</view>
</template>

<script>
import customer from '@/components/customer.vue'//客服
import buyKnow from '@/components/buyKnow.vue'//购买须知
export default {
  components:{
    customer,
    buyKnow
  },
  data() {
    return {
      merchantId:'z0015605022691a5945bbe463141668c',//商户Id
      tabbar:['热门景点','一日游','摄影/旅拍','文创DIY'],//tabbar标题列表
      active:0,//导航栏下标
      imgList:[],//图片列表
      scenicList:[],//景区数据
      showDistance:false,//定位成功显示距离
      buyKnowData:'',//购买须知数据
      chooseProduct:[],//选中的产品
      showModal:false,//是否显示模态框
      chooseMerchantProduct:'',//不支持多产品购买时，选中的景区产品
    }
  },
  watch: {
    //选中产品的数量变化
	  chooseProduct: { 
	    handler(newValue, oldValue){
	      // this.originalTotal=0
	      // this.sellTotal=0
	      // this.chooseProduct.forEach((item)=>{//通过选中的产品计算价格
	      //   this.originalTotal+=item.originalPrice
	      //   this.sellTotal+=item.sellingPrice
	      // })
        //当选中产品时
        if(newValue.length>oldValue.length){
          //找到当前选中的产品
          let product=newValue[newValue.length-1]
          //商家一级
          let merchant=''
          //通过产品一级找到商家一级
          this.scenicList.forEach(item=>{
            if(item.id==product.merchantId){
              merchant=item
            }
          })
          //当选中产品的景区有2个以上选中产品时
          let number=0
          this.chooseProduct.forEach(item=>{
            if(merchant.id==item.merchantId){
              number+=1
            }
          })
          if(product.isMoreBuy!==0){
            if(number>1){
              //显示确认弹窗
              this.showModal=true
              //当前选中的景区产品，用于确定以及取消方法取值
              this.chooseMerchantProduct=product
            } 
          }else{
            //支持多产品购买时，删除该景区其他产品不支持多产品购买的产品
            this.chooseProduct.forEach((item,index)=>{
              if(merchant.id==item.merchantId&&item.isMoreBuy!==0){
                //取消选中状态和选中背景颜色
                this.scenicList.forEach(item2=>{
								  item2.productList.forEach(item3=>{
								    if(item.id==item3.id){
								      item3.ifChecked=false
								      item3.ifChoose=false
								    }
								  })
                })
                delete this.chooseProduct[index]
              }
            })
            this.chooseProduct=this.chooseProduct.filter(function (val) {
              	return val
            })
          }
        }
	    },
	    deep:true,
	    immediate: false
	  },
	
  },
  onLoad(option){
    let token = uni.getStorageSync('token')
    if(token){
		  uni.getLocation({
			 type: 'wgs84',
			 success:res=> {
			   let latitude = res.latitude//纬度 
			   let longitude = res.longitude//经度
          this.showDistance=true
			  this.initList(latitude,longitude)
			  },
			  fail:res=>{
			    let latitude=''
			    let longitude=''
			    this.initList(latitude,longitude)
			  }
		  })
    }
  },
  methods:{
    //---导航栏切换
    tabbarChange(index){
      this.active=index
    },
    //---swiper滑动结束，分别设置tabs和swiper的状态
    animationfinish(e) {
		  let current = e.detail.current
		  this.active = current
    },
    //---展示客服组件
    showCustomer(){
      this.$refs.customer.showPop=true
    },
    //---展示购买须知
    showBuyKnow(data){
      this.buyKnowData=data
      this.$refs.buyKnow.showPop=true
    },
    //---展示明细弹窗
    showDetail(){
      this.$refs.detail.showPop=true
    },
    //---展示更多产品
    showMoreProduct(item){
      item.productListCopy=item.productList
      this.$forceUpdate()
    },
    //---收起产品
    retractProduct(item){
      item.productListCopy=item.productList.slice(0,2)
      this.$forceUpdate()
    },
    //---多选框变化
    checkboxChange(e){
      let value=e.detail.value
      let arr=[]
      value.forEach(item=>{
      	arr.push(JSON.parse(item))
      })
      this.chooseProduct=arr
      //判断是否选中产品，选中产品的背景颜色需要变化
      this.scenicList.forEach(item=>{
        item.productList.forEach(item2=>{
          item2.ifChoose=false
          this.chooseProduct.forEach(item3=>{
            if(item2.id==item3.id){
              item2.ifChoose=true
            }
          })
        })
      })
    },
    //---不支持多选产品，选择确定事件
    chooseConfirm(){
		  this.chooseProduct.forEach((item,index)=>{
		    //删除选中产品以外的其他所有产品
		    if(this.chooseMerchantProduct.merchantId==item.merchantId&&JSON.stringify(item)!=JSON.stringify(this.chooseMerchantProduct)){
          //取消选中状态和选中背景颜色
          this.scenicList.forEach(item2=>{
            item2.productList.forEach(item3=>{
              if(item.id==item3.id){
                item3.ifChecked=false
                item3.ifChoose=false
              }
            })
          })
		      delete this.chooseProduct[index]
		    }
		  })
		  this.chooseProduct=this.chooseProduct.filter(function (val) {
		    	return val
		  })
		  this.showModal=false
    },
    //---不支持多选产品，选择取消事件
    chooseCancel(){
		  //删除选中产品
		  this.chooseProduct.forEach((item,index)=>{
		    if(item.id===this.chooseMerchantProduct.id){
          //取消选中状态和选中背景颜色
          this.scenicList.forEach(item2=>{
					  item2.productList.forEach(item3=>{
					    if(item.id==item3.id){
					      item3.ifChecked=false
					      item3.ifChoose=false
					    }
					  })
          })
		      this.chooseProduct.splice(index,1)
		    }
		  })
		  this.showModal=false
    },
    //---商品列表
    initList(latitude,longitude) {
		  let data = {
		    merchantId: this.merchantId, //商户id
        latitude,
        longitude
		  }
		  this.$request('/scenic/groupGood/getGroupProducts', data).then(res => {
		    if (res.code == '00') {
		      this.scenicList = res.data.merchantList
          this.scenicList.forEach((item,index)=>{
            //获取当前景区图片,把当前景区放在第一位
            if(item.id==this.merchantId){
              this.imgList=item.imgList
              this.scenicList.splice(index,1)
              this.scenicList.unshift(item)
              item.productListCopy=item.productList
            }else{
              item.productListCopy=item.productList.slice(0,2)
            }
            //预订时间为当天时，当前时间大于最早预订时间显示未开售，当前时间大于最晚预订时间显示已售罄
            let times = new Date().Format('hh:mm:ss')
            let timeNumber = parseInt(this.$commonjs.changeTime(times)) //将当前时间转化为数字
            item.productList.forEach(item2=>{
						  item2.timeNumber1 = parseInt(this.$commonjs.changeTime(item2.earlyBookTime))
						  item2.timeNumber2 = parseInt(this.$commonjs.changeTime(item2.bookTime))
						  if(item2.aheadBookDays==0){
						    if(item2.status==2){
						      item2.statusName='已售罄'
						    }else{
						      if(timeNumber<item2.timeNumber1){
						        item2.status=2
						        item2.statusName='未开售'
						      }
						      if(timeNumber>item2.timeNumber2){
						        item2.status=2
						        item2.statusName='已售罄'
						      }
						    }
						  }
            })
          })
		    } else {
		      uni.showToast({
		        title: res.message,
		        icon: 'none'
		      })
		    }
		  })
    },
  }
}
</script>

<style lang="scss" scoped>
.wrap{
	height: 100%;
	display: flex;
	flex-direction: column;
	background: #fff;
}
.middle{
	position: relative;
	top: -40rpx;
	padding: 0 24rpx 60rpx 24rpx;
	display: flex;
	flex-direction: column;
	flex: 1;
}
.swiper-box{
	flex: 1;
}
.tabbar{
	background: #fff;
	border-radius: 16rpx;
	display: flex;
	justify-content: space-between;
	font-size: 32rpx;
	text{
		padding:24rpx 32rpx 24rpx 20rpx;
		border-radius: 16rpx;
	}
}
.tabbar .on{
	background: url('../../../static/img/scenic/tabbar.png') no-repeat;
	background-size: 100% 100%;
	color: #fff;
	font-weight: bold;
}
.merchant{
	border-radius: 16rpx;
	background: #FFFFFF;
	box-shadow: 0px 0px 8px 2px rgba(0,0,0,0.08);
	margin-top: 24rpx;
	padding: 24rpx;
}
.merchant-title{
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-bottom: 16rpx;
}
.title-left{
	font-size: 32rpx;
	margin-right: 10rpx;
	font-weight: bold;
	image{
		width: 36rpx;
		height: 36rpx;
		position: relative;
		top: 6rpx;
		margin:0 8rpx;
	}
}
.title-coupon{
	color: $red;
	font-size: 24rpx;
}
.title-right{
	color: $grey;
	font-size: 24rpx;
}
.product{
	padding: 16rpx;
	background: #ffffff;
	border-radius: 8rpx;
	display: flex;
	align-items: center;
}
.product.on{
	background: #E2EEFF;
}
.product-left{
	margin-right: 10rpx;
}
.product-right{
	flex: 1;
}
.product-name{
	font-size: 28rpx;
	font-weight: bold;
	color: #191919;
	display: flex;
	justify-content: space-between;
}
.product-status{
	padding: 6rpx 12rpx;
	border: 2px solid #F40000;
	background: #ffffff;
	color: #F40000;
	font-size: 24rpx;
	border-radius: 4rpx;
	transform: rotate(-25deg);
	position: relative;
	top: 30rpx;
	right: 80rpx;
}
.product-price{
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.product-rule{
	color: $blue;
}
.product-rule::after{
	display: inline-block;
	content: '';
	height: 22rpx;
	margin: 0 10rpx;
	background: #ccc;
	width: 1px;
}
.product-orange{
	font-size: 20rpx;
	color:$grey;
	text-decoration:line-through;
}
.product-sell{
	font-size: 40rpx;
	color: $red;
	font-weight: bold;
	margin-left: 10rpx;
}
.product-more{
	padding: 10rpx;
	text-align: center;
}

.bottom{
	position: fixed;
	bottom: 0;
	width: 100%;
	height: 100rpx;
	background: #fff;
	box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.16);
	z-index: 10;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 32rpx;
}
.bottom-middle{
	position: relative;
	padding: 0 40rpx;
}
.bottom-more{
	position: relative;
	top: -40rpx;
	background: #F40000;
	border-radius: 28px;
	opacity: 0.64;
	padding: 10rpx 20rpx;
	color: #ffffff;
}
.bottom-three{
	width: 14rpx;
	height: 14rpx;
	display: inline-block;
	background: #F40000;
	opacity: 0.64;
	position: absolute;
	right: 42rpx;
	bottom: -7rpx;
	transform:rotate(45deg);
}
.bottom-detail{
	position: relative;
	text-align: center;
}
.bottom-number{
	display: inline-block;
	width: 36rpx;
	height: 36rpx;
	border-radius: 50%;
	text-align: center;
	background: #F40000;
	color: #ffffff;
	font-weight: bold;
}
.btn{
	padding: 16rpx 48rpx;
	border-radius: 20rpx;
	font-size: 32rpx;
}
</style>