<template>
	<view class="">
		<view class="drawer">
			<view class="topContentV">
				<mynavbar class="mynavbarV" :title="Btitle" titledetl="" :interestBT="0" :mycartBT="1" v-on:Enavbarsel="navbarsel"></mynavbar>
				<view class="toptalbar">
					<view v-for="(item,index) in tableList">
						<text :class="toptableindex==index?'tableLab tableselect':'tableLab'" @click="tableselct(item.value,index)">{{item.text}}</text>
					</view>
				</view>
			</view>
		</view>
		<view v-if="isLogin" class="content4 listView">
			<foodlistItem :orderlist="orderlist" v-on:ERefresh="Refresh"></foodlistItem>
		</view>
		
		<view v-if="!isLogin" class="contentColumnC" style="margin-top: 260rpx;">
			<image class="weidenglu" src="/static/msdshj/qishouIM.png" mode="aspectFit"></image>
			<text class="xuacyuA"> </text>
			<view class="contentColumnC" style="width: 100%;">
				<view class="texing">
					<view class="contentInRowL" style="align-items: center;">
						<uni-icons custom-prefix="custom-icon" color="#386EF5" type="checkbox" size="22"></uni-icons>
						<text class="textfontB3">{{$t('user.hailiangdd')}}</text>
					</view>
					<view class="contentInRowL" style="align-items: center;margin-top: 20rpx;">
						<uni-icons custom-prefix="custom-icon" color="#386EF5" type="checkbox" size="22"></uni-icons>
						<text class="textfontB3">{{$t('user.caozuojid')}}</text>
					</view>
					<view class="contentInRowL" style="align-items: center;margin-top: 20rpx;">
						<uni-icons custom-prefix="custom-icon" color="#386EF5" type="checkbox" size="22"></uni-icons>
						<text class="textfontB3">{{$t('user.shirkek')}}</text>
					</view>
				</view>
			</view>
			
			<view class="contentColumnC texing" style="width: 100%;">
				<text class="textfontB3">美食達</text>
				<text class="textfontB3">{{$t('user.chengyaojm')}}</text>
			</view>
			
		</view>
		<view v-if="isLogin">
			<view v-if="orderlist.length==0&&isLogin" class="contentColumnC">
				<image class="weishimingImage" src="/static/images/dingdan1@3x.png" mode="widthFix"></image>
				<text class="meiyouText">{{$t('order.dangqmydd')}}</text>
			</view>
		</view>
		
		<view style="height: 20rpx;"></view>
		<popViewGG :tips="$t('user.xiazaijindu')" :show="show" :maskClosable="false" :isCancel="false">
			<view class="words">
				<text>{{$t('user.xiazaizhong')}} {{percentVal}}%</text>
			</view>
		</popViewGG>
		<uni-popup style="z-index: 999;" ref="popYsxy" type="center" :isMaskClick="false" :safeArea="true">
			<ysxyView v-on:EagrResult="agrResult"></ysxyView>
		</uni-popup>
	</view>
</template>

<script>
  import mynavbar from "@/pages/component/mynavbar/mynavbar.vue";
  
  import api from "@/pages/api/api.js"
  
  export default {
	  components:{
		  mynavbar
	  },
    data() {
      return {
		  formData: {
		  	status: 'loading', // 加载状态
		  },
		  tipShow: false ,// 是否显示顶部提示框
		Btitle:"",
		isLogin:false,
		token:'',
		mendid:'',
		mendianList:[],
		versionMSG:[],
		appVersionCode:0,
		updateUrl:'',
		tishigx:true,
		show:false,
		percentVal:'',
		orderlist:[],
		tableList:[],
		toptableindex:0,
		state:'z01',
		page:1,
		timer:null
      }
    },
    onLoad() {
		var info = uni.getSystemInfoSync();
		//console.log(info);
		this.appVersionCode=info.appVersionCode;	
    },
	onReady() {
		var yszche=uni.getStorageSync('yszche');
		if(1!=yszche){
			this.$refs.popYsxy.open('center');
			uni.hideTabBar();
		}
	},
	onShow() {
		this.tableList=[
			{ value: 1, text: this.$t('order.daishouli') },
			{ value: 2, text: this.$t('order.daichuchan') },
			{ value: 5, text: this.$t('order.yichuchan') },
			{ value: 6, text: this.$t('order.shouhou') },
		];
		
		var yszche=uni.getStorageSync('yszche');
		if(1!=yszche){
			return;
		}
		this.getVersion();
		this.token = uni.getStorageSync("token");
		console.log('------11this.token',this.token)
		if(this.token == '' || this.token == undefined || this.token == null){
			// uni.navigateTo({
			// 	url:'/pages/UserCenter/LoginView'
			// })
			this.isLogin=false;
			return;
		}
		else{
			this.isLogin=true;
			this.chongxindenglu();
		}
		
		
		this.mendid = uni.getStorageSync("mendid");
		this.getMymendianlist();
		if(this.tishigx){
			this.tishigx=false;
		}
		else{
			this.getstoreorderlist(1,this.state);
		}
		this.timerAct();
	},
	onHide() {
		this.clearTimer();
	},
    methods: {
	getMymendianlist(){
		//this.formData.status = 'more'
		this.tipShow = true
		
		api('getMymendianlist',{},
		r=>{
			this.mendianList=r.data.data;
			console.log(this.mendianList);
			if(this.mendid==''||undefined==this.mendid||null==this.mendid){
				if(this.mendianList&&this.mendianList.length>0){
					this.mendid=this.mendianList[0].id;
					this.Btitle=this.mendianList[0].posName;
				}
			}
			else{
				this.chonsemendian();
			}
			this.getstoreorderlist(1,this.state);
		},failc=>{
			//console.log('getadvertis----',failc)
			uni.stopPullDownRefresh()
			this.tipShow = false
		});
	},
	chonsemendian(){
		for(var i=0;i<this.mendianList.length;i++){
			var mend = this.mendianList[i];
			if(mend.id==this.mendid){
				this.Btitle=mend.posName;
				return;
			}
		}
		this.mendid=this.mendianList[0].id;
		this.Btitle=this.mendianList[0].posName;
	},
	tableselct(id,index){
		
		if(!this.isLogin){
			uni.navigateTo({
				url:'/pages/UserCenter/LoginView'
			})
			return;
		}
		console.log('tableselct',index)
		this.toptableindex=index;
		this.state = id;
		this.page = 1;
		if(id==1){
			this.state='z01';
		}
		if(id==2){
			this.state='z23';
		}
		if(id==6){
			this.state='z678911';
		}
		this.getstoreorderlist(1,this.state);
	},
	timerAct(){
		this.clearTimer();
		var that = this;
		that.timer = setInterval(() => {
			//console.log('setInterval',that.toptableindex)
			if(that.toptableindex==0||that.toptableindex==1){
				that.Refresh();
			}
		}, 20000);
	},
	clearTimer() {
		// clearTime
		if (this.timer) {
			clearInterval(this.timer);
			this.timer = null;
		}
	},
	Refresh(){
		this.getstoreorderlist(1,this.state);
	},
	navbarsel(item){
		console.log(item);
		if(item==1){
			uni.switchTab({
			  url: '/pages/UserCenter/UserCenter'
			});
			return
		}
		if(item==2){
			if(!this.isLogin){
				uni.navigateTo({
					url:'/pages/UserCenter/LoginView'
				})
				return;
			}
			uni.navigateTo({
				url:'/pages/UserCenter/mendian/xuanzemendian'
			})
			return
		}
	},
	
	getstoreorderlist(page,state){
		 console.log('-----------',state,this.mendid)
	 	if(state<0){
	 		state = '';
	 	}
	 	if(state=='z23'){
	 		this.getdaichucan(page,state,0);
	 		return;
	 	}
		if(state==5){
			this.getdaichucan(page,'',1);
			return;
		}
	 	this.formData.status = 'more'
	 	this.tipShow = true
		uni.showLoading({
			title:'Loading...',
			mask:true
		})
		console.log(state,this.mendid)
		api('getstoreorderlist',{
			page:page,
			size:10,
			state:state,
			mdId:this.mendid
		},
		r=>{
			console.log(r);
			uni.hideLoading();
			if(page!=1){
				if(r.data.data){
					if(r.data.data.length>0){
						var temp = this.orderlist;
						for(var i=0;i<r.data.data.length;i++){
							temp.push(r.data.data[i]);
						}
						// this.orderlist=[];
						// this.orderlist.push(temp);
						this.orderlist=temp;
					}
					else{
						this.status = 'noMore';
					}
				}
				else{
					this.status = 'noMore';
				}
			}
			else{
				// this.orderlist=[];
				// this.orderlist.push(r.data.data);
				this.orderlist=r.data.data;
				this.status = 'more';
			}
			uni.stopPullDownRefresh();
			this.tipShow = false;
		},failc=>{
			uni.hideLoading();
			
			uni.stopPullDownRefresh()
			this.tipShow = false
		});
	},
	 
	getdaichucan(page,state,diningStatus){
	 	uni.showLoading({
	 		title:'Loading...',
	 		mask:true
	 	})
	 	this.formData.status = 'more'
	 	this.tipShow = true;
		
		api('getstoreorderlist',{
			page:page,
			size:10,
			state:state,//空或z23
			diningStatus:diningStatus,//0h或1
			mdId:this.mendid
		},
		r=>{
			console.log(r);
			uni.hideLoading();
			if(page!=1){
				if(r.data.data){
					if(r.data.data.length>0){
						var temp = this.orderlist;
						for(var i=0;i<r.data.data.length;i++){
							temp.push(r.data.data[i]);
						}
						// this.orderlist=[];
					// 	this.orderlist.push(temp);
						this.orderlist=temp;
					}
					else{
						this.status = 'noMore';
					}
				}
				else{
					this.status = 'noMore';
				}
			}
			else{
				// this.orderlist=[];
				// this.orderlist.push(r.data.data);
				this.orderlist=r.data.data;
				this.status = 'more';
			}
			uni.stopPullDownRefresh();
			this.tipShow = false;
		},
		failc=>{
			uni.hideLoading();
			uni.stopPullDownRefresh()
			this.tipShow = false
		});
	 },
	 /**
	  * 下拉刷新回调函数
	  */
	 onPullDownRefresh() {
	 	this.getstoreorderlist(1,this.state);
	 	this.page=1;
	 },
	 /**
	  * 上拉加载回调函数
	  */
	 onReachBottom() {
	 	console.log('onReachBottom')
	 	this.page=this.page+1;
	 	this.getstoreorderlist(this.page,this.state);
	 },
	 
	 chongxindenglu(){
	   var that =this;
	 	try {//读取
	 		that.token = uni.getStorageSync("token");
	 		getApp().globalData.token = that.token;
	 		if(this.token == '' || this.token == undefined || this.token == null){
				// uni.navigateTo({
				// 	url:'/pages/UserCenter/LoginView'
				// })
	 			return;
	 		}
	 		else{
	 			this.gengxintoken(that.token);
	 			return;
	 		}
	 	} catch(e){//错误
	 		return;
	 	}
	 },
	 gengxintoken(token){
		api('refreshToken',{
		},
		r=>{
			console.log('gengxintoken',r)
			if(r.data.code==200){
				this.isLogin=true;
				uni.setStorageSync("token",r.data.data);
				getApp().globalData.token = r.data.data;
				this.getUserMsg(r.data.data);
			}
			else{
				uni.navigateTo({
					url:'/pages/UserCenter/LoginView'
				})
			}
		},failc=>{
			uni.hideLoading();
			uni.stopPullDownRefresh()
			this.tipShow = false
		});
		
	 },
	 getUserMsg(token){
	 	//console.log('getUserMsg')
		api('getUserMsg',{
		},
		r=>{
			console.log(r.data)
			if(r.data.code==200){
				let items = JSON.stringify(r.data.data);
				uni.setStorageSync('userInfo',items);
				this.getCID();
			}
		},failc=>{
			console.log(failc)
		});
	 },
	 
	 getCID(){
	 	 var cid = uni.getStorageSync("cid");
	 	 if(cid==''||cid==null||cid==undefined){
	 		 var that = this;
	 		 uni.getPushClientId({
	 			success: (res) => {
	 				//console.log('客户端推送标识:',res.cid)
	 				uni.setStorageSync("cid",res.cid);
	 				var usdata = {
	 				 	"cid":res.cid,
	 				};
	 				that.updataUserMsg(usdata);
	 			 },
	 			 fail(err) {
	 			  console.log(err)
	 			 }
	 		 });
	 	 }
	 	 else{
	 		var usdata = {
	 		 	"cid":cid,
	 		};
	 		 this.updataUserMsg(usdata)
	 	 }
	 		 
	 },
	updataUserMsg(data){
		
		api('updataUserMsg',data,
		res=>{
			console.log(res)
			uni.navigateBack({
				delta:1,//返回层数，2则上上页
			})
		},failc=>{
			//console.log('getadvertis----',failc)
			
		});
		
	 },
	 
	 agrResult(state){//隐私协议
		 console.log(state);
		 if(state==0){
			if (plus.os.name.toLowerCase() === 'android') {
				console.log('ios');
				plus.runtime.quit();
			}
			else{
				console.log('ios');
				 plus.ios.import("UIApplication").sharedApplication().performSelector("exit")
			}
		 }
		 if(state==1){
			 uni.showTabBar(); 
			 this.$refs.popYsxy.close();
			 uni.setStorageSync('yszche',1);
			 this.chongxindenglu();
		 }
		 if(state==2){
			uni.navigateTo({
				url:'/pages/index/mindex/yinszhengc'
			})	 
		 }
	 },
	 //自动更新
	 getVersion(){
		 return;
	 	var info = uni.getSystemInfoSync();
	 	console.log(info);
	 	if(info.platform.toLowerCase() != 'android'){
	 		return;
	 	}
	 	var appVersionCode = info.appVersionCode;
	 	var that = this;
		
		api('getversion',{
	 		platform:'0',
	 		name:'CTE.cooperate'
	 	},
		res=>{
			console.log(res);
			if(res.data.data){
				var versionMSG=res.data.data;
				if(appVersionCode<versionMSG.version){
					uni.showModal({
						cancelText: that.$t('order.quxiao'), // 取消按钮的文字
						confirmText: that.$t('index.shengji'), // 确认按钮的文字 
					    title: that.$t('api.message'),
					    content: that.$t('index.yiyouxinban'),
					    success: function (res) {
							if (res.confirm) { //这里是点击了确定以后
								var updateUrl='http://backend.cityexpress168.com.vn/androidApk/CTE.cooperate.apk';
								that.gengxinApp(updateUrl);
							} else { //这里是点击了取消以后
							  console.log('用户点击取消')
							}
							
					    }
					})
				}
			}
		},failc=>{
			//console.log('getadvertis----',failc)
			
		});
	 },
	 gengxinApp(url){
	 	console.log(url);
	 	var that = this;					
	 	var dtask = plus.downloader.createDownload(
	 		url,
	 		{
	 			method: "GET"
	 		}, (d, status) => {
	 			console.log(status,d);
	 			if (status == 200) {
	 				plus.runtime.install(d.filename)
	 			} else {
	 				plus.nativeUI.alert('fail！' + status)
	 			}
	 	});
	 	dtask.start();
	 	var prg = 0;//进度数字化
	 	var show= plus.nativeUI.showWaiting("Loading...");
	 	var percentVal;
	 	dtask.addEventListener('statechanged',function(task,status){
	 		//console.log(task);
	 		switch(task.state){//根据下载状态调整其显示内容
	 			case 1:
	 				percentVal = "Loading……";
	 				show.setTitle("Loading……");
	 				break;
	 			case 2:
	 				percentVal = "Connected";
	 				show.setTitle("Connected");
	 				break;
	 			case 3:
	 				percentVal = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
	 				show.setTitle("Loading：" + percentVal + "%");
	 				break;
	 			case 4:
	 				plus.nativeUI.closeWaiting();
	 				
	 				break;
	 		}
	 	});
	 },
    }
  }
</script>

<style scoped lang="scss">
  .contentbg{  
	  margin-top: 140rpx;
  }
	.mynavbarV{
		z-index: 999;
		background-color: #40AE36;
	}
	.topContentV{
		position: fixed;
		padding-top: 40rpx;
		width: 750upx;
		left: 0;
		right: 0;
		top: 0;
		z-index: 999;
		background-color: #40AE36;
	}
	.toptalbar{
		display: flex;
		flex-direction: row;
		justify-content:space-between;
		align-items: center;
		background-color: #40AE36;
		padding-top: 10rpx;
		height: 90rpx;
		margin-left: 6rpx;
		margin-right: 6rpx;
	}
	.tableLab{
		width: 24%;
		text-align: center;
		margin-left: 14rpx;
		height: 90rpx;
		line-height: 28rpx;
		font-size: 24rpx;
		color: white;
	}
	.tableselect{
		border-bottom-width: 6rpx;
		border-bottom-style:solid;
		border-bottom-color:white;
		color: white;
		font-size: 28rpx;
	}
	
	.weidenglu{
		width: 600rpx;
		height: 600rpx;
	}
	.xuacyuA{
		font-size: 26rpx;
		font-family: 'zrht';
		font-weight: bold;
		letter-spacing: 4rpx;
		margin-left: 160rpx;
		margin-top: 80rpx;
	}
	.texing{
		font-size: 24rpx;
		font-weight: bold;
		font-family: 'zrht';
		//width: 600rpx;
		letter-spacing: 2rpx;
		margin-top: 40rpx;
		color: #40AE36;
		align-items: center;
		justify-content: flex-start;
	}
  //抽屉实现
  .drawer{
	  width: 100%;
	  padding-left: 0;
	  position: absolute;
	  background-color: white;
  }
  
  .words{
  	width: 100%;
  	text-align: center;
  	font-size: 30rpx;
  	height: 80rpx;
  	line-height: 80rpx;
  }
  
  .listView{
	  margin-top: 290rpx;
	  margin-bottom: 20rpx;
  }
  
  .weishimingImage{
  	width: 30%;
  	margin-top: 340rpx;
  }
  .meiyouText{
  	font-size: 28rpx;
  	font-weight: bold;
  	color: black;
  }
  
</style>
