<template>
	<view class="wrap">
		<view>
			<view class="first">
				<input placeholder="请输入购票预留手机号/身份证号" v-model="credentialNumber" />
			</view>
			<view class="query">
				<text class="button" @click="clickQuery()">查询订单</text>
			</view>
		</view>

		<view class="mark-box" v-if="showMark">
			<view style="width: 100%;">
				<view class="mark-content">
					<view class="mark-title"> 获取失败 </view>
					<view class="mark-tip"> 查无该订单，请咨询购票渠道！</view>
					<!-- <view class="mark-btn">
						<text class="btn">重试</text>
					</view> -->
				</view>
				
				<view class="mark-close" @click="showMark=false">
					<u-icon name="close-circle" size='60' color='#ffffff'></u-icon>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
export default {
  data() {
    return {
      showMark: false ,//是否显示遮罩层
      credentialNumber:'',//输入框的值
      noNumberList:[],//未排号站点列表
      orderId:'',//订单Id
      merchantCode:'',//商户码
      thirdOrderId:'',//三方Id
      channelName:'',//渠道名称
      productName:'',//产品名称
    }
  },
  onLoad(option) {
	  this.merchantCode=this.$commonjs.getKey(option,'merchantCode')
  },
  methods: {
    //---点击查询
    clickQuery(){
      uni.$u.throttle(this.queryList,3000)
    },
    //---查询订单
    queryList(){
      if(!this.credentialNumber){
        uni.showToast({
				  title:'请输入手机号或者身份证号',
				  icon: 'none'
        })
        return
      }
      let data={
        credentialNumber:this.credentialNumber,//输入框的值
        userId:uni.getStorageSync('openid')||'',
        merchantCode:this.merchantCode,
        startPlayDate:new Date().Format('yyyy-MM-dd')+' 00:00:00',//开始日期
        endPlayDate:new Date().Format('yyyy-MM-dd')+' 23:59:59',//结束日期
      }
      this.$request('order/userOrder/findOrderPageListByCode',data).then((res)=>{
			  if(res.code=='00'){
			    let list=res.data||[]
          if(list.length==0){
            this.showMark=true
          }else{
            //下面参数需要传给下一个页面
            this.orderId=list[0].id||''
            this.thirdOrderId=list[0].thirdOrderId||''
            this.merchantCode=list[0].merchantCode||''
            this.channelName=list[0].channelName||''
            this.productName=list[0].productName||''
            this.getFetchInfo()
          }
			  }else{
			    uni.showToast({
			      title: res.message,
			      icon: 'none'
			    })
			  }
      })
    },
    //---获取排号信息
    getFetchInfo(){
      let data={
			  thirdId:this.thirdOrderId,
        merchantCode:this.merchantCode
      }
      this.noNumberList=[]
      this.$request('distribution/distribution/getFetchInfoOta',data).then((res)=>{
			  if(res.code=='00'){
          //如果排号的数组里面有未使用的直接跳转电子门票页面
          if(res.data&&res.data.length>0){
            let arr1=res.data[0].sorts||[]
            let arr2=res.data[0].uses||[]
            let number=0
            arr1.forEach(item=>{
              arr2.forEach(item2=>{
                if(item.projectId==item2.projectId){
                  number+=1
                }
              })
            })
            if(arr1.length>number){
              uni.navigateTo({
                url:`/pages/my/order/electronicTicket/electronicTicket?orderId=${this.orderId}`
              })
            }
          }
          //sortFair=3时可重排，排号状态:0-等待中,1-可进入,2-已过号,３-可重排,4-可返程取号(仅胖丁显示),5-第一次取号(仅胖丁显示),6-已过闸
          //计算时去除可重排的
          let sorts=[]
          if(res.data[0].sorts&&res.data[0].sorts.length>0){
            res.data[0].sorts.forEach(item=>{
              if(item.sortFair!=3){
                sorts.push(item)
              }
            })
          }
          let uses=res.data[0].uses||[]
          let list=res.data[0].timeAreas||[]
          //获取所有站点列表
          let totalList=[]
          list.forEach(item=>{
            if(!totalList.find(item2=>item2.projectIds==item.projectIds)){
              //去重，并且为排号区的才放入数组
              if(item.areaId==1){
                totalList.push({
								  projectIds:item.projectIds,
								  projectName:item.projectName
                })
              }
            }
          })
          //获取已使用和已排号站点信息之和
          let usedList=sorts.concat(uses)
          let usedNewArr=[]
			    usedList.forEach(item=>{
			      if(!usedNewArr.find(item2=>item2.projectId==item.projectId)){
			        usedNewArr.push({
			          projectId:item.projectId,
			          projectName:item.projectName
			        })
			      }
			    })
          //获取未排号的站点列表
          this.noNumberList=JSON.parse(JSON.stringify(totalList)) 
          this.noNumberList.forEach((item,index)=>{
            usedNewArr.forEach(item2=>{
              if(item.projectIds==item2.projectId){
                delete this.noNumberList[index]
              }
            })
          })
          this.noNumberList=this.noNumberList.filter(function (val) {
        	return val
          })
          if(this.noNumberList.length==0){
            uni.navigateTo({
            	url:`/pages/my/order/electronicTicket/electronicTicket?orderId=${this.orderId}`
            })
          }
          if(this.noNumberList.length==1){
            let chooseArea=JSON.stringify(this.noNumberList[0])
            uni.navigateTo({
              url:`/pages/my/takeNumber/rowNumber/rowNumber?chooseArea=${chooseArea}&orderId=${this.orderId}&thirdOrderId=${this.thirdOrderId}&merchantCode=${this.merchantCode}&channelName=${this.channelName}&productName=${this.productName}`
            })
          }
          if(this.noNumberList.length>1){
            let list=JSON.stringify(this.noNumberList)
            uni.navigateTo({
						  url:`/pages/my/takeNumber/numberChoose/numberChoose?list=${list}&orderId=${this.orderId}&thirdOrderId=${this.thirdOrderId}&merchantCode=${this.merchantCode}&channelName=${this.channelName}&productName=${this.productName}`
            })
          }
			  }else{
			    uni.showToast({
			      title: res.message,
			      icon: 'none',
            duration:4000
			    })
			  }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	background: linear-gradient(180deg, #3688ff 0%, #2abbd7 100%);
}
.first input {
	width: 600rpx;
	height: 88rpx;
	background: #fff;
	border-radius: 8rpx;
	padding: 0 24rpx;
}
.query {
	margin-top: 48rpx;
	display: flex;
	justify-content: center;
}
.button {
	display: inline-block;
	width: 440rpx;
	height: 80rpx;
	line-height: 80rpx;
	background: #3688ff;
	border-radius: 40rpx;
	text-align: center;
	color: #fff;
	font-size: 32rpx;
	font-weight: bold;
}
// 遮罩层
.mark-box {
	position: fixed;
	z-index: 100;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.6);
	display: flex;
	align-items: center;
	justify-content: center;
}
.mark-content{
	background: #ffffff;
	margin: 0 48rpx;
	border-radius: 16rpx;
	padding: 64rpx 0;
	text-align: center;
}
.mark-title{
	font-size: 40rpx;
	font-weight: bold;
	color: #191919;
}
.mark-tip{
	font-size: 32rpx;
	margin-top: 24rpx;
}
.mark-btn{
	margin-top: 48rpx;
	.btn{
		width: 212rpx;
		height: 80rpx;
		line-height: 60rpx;
		font-size: 32rpx;
		background: #3688FF;
		border-radius: 40rpx;
	}
}
.mark-close{
	margin-top: 60rpx;
	display: flex;
	justify-content: center;
}
</style>
