<template>
	<view class="drawer">
		<mynavbar class="mynavbarV" :title="Btitle" :titledetl="Btitledetl" :interestBT="0" :mycartBT="1"
			v-on:Enavbarsel="navbarsel"></mynavbar>
		<view :style="{marginTop: vMtop+'rpx'}">
			<swiperImgs v-if="cuxiaoImge.length>0" style="margin-top: 20rpx;" width="750" height="380"
				:imageList="cuxiaoImge"></swiperImgs>
			<fenleiList ref="fwfenlei" v-on:Efwflxuanze="fwflxuanze"></fenleiList>
			<listItemD v-on:Emendianxuanze="mendianxuanze" :mendianList="mendianList" :title="$t('index.remenct')">
			</listItemD>
			<!-- <listItemC ref="flList" v-on:Efenleixuanze="fenleixuanze"></listItemC> -->
			<listitemB ref="flList" v-on:Efenleixuanze="fenleixuanze"></listitemB>
		</view>
		<view class="contentColumnC" style="margin-top: 10rpx;">
			<view class="contentInRowC" style="width: 90%;">
				<text class="tipyLBt" :style="BtState==-1?'background-color:dodgerblue;color:white':''"
					@click="BtStatesl(-1)">{{$t('mend.fujinrem')}}</text>
				<text class="tipyMBt" :style="BtState==0?'background-color:dodgerblue;color:white':''"
					@click="BtStatesl(0)">{{$t('mend.haoping')}}</text>
				<text class="tipyRBt" :style="BtState==2?'background-color:dodgerblue;color:white':''"
					@click="BtStatesl(2)">{{$t('mend.xindian')}}</text>
			</view>
			<view class="contentColumnC" style="margin-top: 16rpx;">
				<view class="item_content" v-for="(item,index) in fjmendian" @click="mendianxuanze(item.id)">
					<imageconver :isremen="true" :isyouhui="false" :youhuinote="''" class="mainimage"
						:imagSrc="item.image"></imageconver>
					<view class="titletext textfontB3">
						<text>{{item.posName}}</text>
					</view>
					<view class="contentInRowS price textfontB5">
						<text>{{$t('index.manX')}} {{$formPr(item.posPrice)}}{{$t('locale.huobidw')}}</text>
						<text class="textfontB5" style="color:#00A6FF;">{{$formPr(item.juli)}}Km</text>
					</view>
				</view>
			</view>
		</view>
		<view v-if="notLongin" class="loninnotev">
			<view class="contentColumnC" style="margin-top: 20rpx;" @tap="gotologin">
				<text class="dengluzhuc">{{$t("index.dengluzhuce")}}</text>
			</view>
			<view style="height: 20rpx;"></view>
		</view>

		<popViewGG :tips="$t('user.xiazaijindu')" :show="show" :maskClosable="false" :isCancel="false">
			<view class="words">
				<text>{{$t('user.xiazaizhong')}} {{percentVal}}%</text>
			</view>
		</popViewGG>
		<popViewGG :tips="$t('index.dizhixuanz')" :show="showaddrs" :maskClosable="true" :isCancel="true"
			:acttips="$t('index.qitadizhi')" @chooseCancel="chooseCancel" @ClickCancel="ClickCancel">
			<view class="contentColumnC" style="width: 90%;margin-left: 5%;">
				<view class="itemView" v-for="(item,index) in addrList" @click="addrselct(item)">
					<text class="addrV textfontB5">{{item.address}}</text>
				</view>
			</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 listItemC from "@/pages/component/listItemC/listItemC.vue";
	import listItemD from "@/pages/component/listItemD/listItemD.vue";
	import listitemB from "@/pages/component/listitemB/listitemB.vue";

	import api from "@/pages/api/api.js"
	export default {
		components: {
			mynavbar,
			listItemC,
			listItemD,
			listitemB
		},
		data() {
			return {
				cuxiaoImge: [],
				systemLocale: '',
				applicationLocale: '',
				Btitle: '',
				Btitledetl: '',
				latitude: 10.721347,
				longitude: 106.713463,
				addrList: [],
				Btype: 0,
				open: false,
				isActive: false,
				goods: [],
				mendianList: [],
				readonly: true,
				versionMSG: [],
				appVersionCode: 0,
				updateUrl: '',
				tishigx: true,
				show: false,
				showaddrs: false,
				percentVal: '',
				juli: 0,
				searchBrH: 120,
				vMtop: 0,
				BtState: -1,
				fjmendian: [],
				notLongin: true
			}
		},
		onLoad() {
			//this.ringoffImCall();
			var tempjuli = uni.getStorageSync('FuWujuli');
			if ('' == tempjuli || null == tempjuli || undefined == tempjuli) {
				this.juli = 10;
			} else {
				this.juli = tempjuli;
			}
			var info = uni.getSystemInfoSync();
			//console.log(info);
			this.appVersionCode = info.appVersionCode;
			this.statusBarHeight = info.statusBarHeight * 4 + 60;
			this.vMtop = this.statusBarHeight + this.searchBrH;

			var yszche = uni.getStorageSync('yszche');
			if (1 != yszche) {} else {
				var tempjuli = uni.getStorageSync('FuWujuli');
				if ('' == tempjuli || null == tempjuli || undefined == tempjuli) {
					this.juli = 10;
				} else {
					this.juli = tempjuli;
				}
				uni.setStorageSync("latitude", this.latitude);
				uni.setStorageSync("longitude", this.longitude);
				this.gethostmendianlist(1);
				// this.MYgetLocation();
			}
		},

		onReady() {
			var yszche = uni.getStorageSync('yszche');
			if (1 != yszche) {
				this.$refs.popYsxy.open('center');
				uni.hideTabBar();
			} else {}
			this.$refs.fwfenlei.getfenleiList();
		},

		onShow() {
			var yszche = uni.getStorageSync('yszche');
			if (1 != yszche) {
				return;
			}
			this.getVersion();
			this.cuxiaoList();
			var tempjuli = uni.getStorageSync('FuWujuli');
			if ('' == tempjuli || null == tempjuli || undefined == tempjuli) {
				this.juli = 10;
			} else {
				this.juli = tempjuli;
			}
			console.log('this.juli', this.juli)
			this.chongxindenglu();
			if (this.tishigx) {
				this.tishigx = false;
			} else {
				this.gettempLocate();
				this.$refs.flList.getmendianfenlei();
				this.$refs.fwfenlei.getfenleiList();
			}
		},

		methods: {
			checkdingdan() {
				//getApp().globalData.iosfl(1);
			},
			gotologin() {
				if (this.notLongin) {
					uni.navigateTo({
						url: '/pages/UserCenter/LoginView'
					})
				}

			},
			cuxiaoList() {
				api('cuxiaoList', {
					mdId: '',
					price: ''
				}, r => {
					console.log('-----------', r)
					if (r.data.code == 200) {
						this.cuxiaoImge = [];
						var objs = r.data.data;
						for (var i = 0; i < objs.length; i++) {
							var obj = objs[i];
							if (obj.poster && obj.poster.length > 0) {
								this.cuxiaoImge.push(obj.poster);
							}
						}
					}
				}, failc => {
					//console.log('getadvertis----',failc)
				})
			},
			//获取自己最近收货地址
			getzuijinaddress(latitude, longitude) {
				api('getzuijinaddress', {
					latitude: latitude,
					longitude: longitude
				}, res => {
					if (res.data.code != 200) {
						return;
					}
					this.addrList = res.data.data;
					console.log('11113');
				}, failc => {
					//console.log('getadvertis----',failc)
				})
			},
			//获取当前定位
			MYgetLocation() {
				console.log('MYgetLocation');
				var that = this;
				uni.getLocation({
					type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标，其他地图使用 gcj02 坐标
					geocode: "true",
					isHighAccuracy: "true",
					accuracy: "best", // 精度值为20m
					success: function(res) {
						console.log("定位获取:", res);
						that.latitude = res.latitude;
						that.longitude = res.longitude;
						var latlon = that.latitude + ',' + that.longitude;
						// that.Btitle=latlon;
						uni.setStorageSync("latitude", that.latitude);
						uni.setStorageSync("longitude", that.longitude);
						that.gethostmendianlist(1);
						that.getzuijinaddress(that.latitude, that.longitude);
						that.getAreaCode(latlon);
					},
					fail(err) {
						console.log(err);
						if (that.Btitledetl == '') {
							// uni.navigateTo({
							// 	url:'/pages/UserCenter/map/map?state=0'
							// })
							// uni.navigateTo({
							// 	url:'/pages/UserCenter/map/mapN?state=0'
							// })
							uni.showToast({
								title: that.$t('api.dingweishibai'),
								icon: 'none',
								duration: 3000
							})
						}
					}
				});
			},
			//获取地图选点返回
			gettempLocate() {
				var that = this;
				try { //读取
					var data = uni.getStorageSync("userAddrLocation");
					if ('' == data || undefined == data || null == data) {
						that.gethostmendianlist(1);
						that.getzuijinaddress(that.latitude, that.longitude);
						return;
					} else {

						that.latitude = data.latitude;
						that.longitude = data.longitude;
						uni.setStorageSync("latitude", that.latitude);
						uni.setStorageSync("longitude", that.longitude);
						that.Btitle = data.name;
						that.Btitledetl = data.address;
						that.gethostmendianlist(1);
						uni.setStorageSync('userAddrLocation', "");
						uni.setStorageSync('tempLocation', '');
						that.getzuijinaddress(that.latitude, that.longitude);
						return;
					}
				} catch (e) { //错误
					return;
				}
			},
			//获取附近地址
			getAreaCode(latlng) {
				var userId = uni.getStorageSync('userId');
				var that = this;
				api('getAddr', {
					latlng: latlng,
					id: userId
				}, res => {
					//console.log('getAreaCode',res);
					if (res.data.code != 200) {
						return;
					}
					this.Btitle = res.data.data.name;
					this.Btitledetl = res.data.data.address;
				}, failc => {
					//console.log('getadvertis----',failc)
				})

			},
			dowithAddr(r, type) {
				console.log(r);
				if (type == 0) {
					if (r.result.pois.length > 0) {
						this.Btitledetl = r.result.formatted_address;
					}
				} else if (type == 1) {
					if (r.results.length > 0) {
						this.Btitledetl = r.results[1].vicinity;
					}
				}
			},
			//
			navbarsel(item) {
				console.log('navbarsel')
				if (item == 1) {
					if (this.addrList.length > 0) {
						this.showaddrs = true;
					} else {
						if (this.notLongin) {
							this.gotologin();
							return;
						}
						uni.navigateTo({
							url: '/pages/UserCenter/map/Chazhaodizhi'
						})

					}
				}
				if (item == 2) {
					if (this.notLongin) {
						this.gotologin();
						return;
					}
					uni.navigateTo({
						url: '/pages/shanpinxungou/shangpin/shangpinList/shangpinList'
					})
				}
			},
			addrselct(item) {
				this.latitude = item.latitude;
				this.longitude = item.longitude;
				uni.setStorageSync("latitude", this.latitude);
				uni.setStorageSync("longitude", this.longitude);
				this.Btitle = '';
				this.Btitledetl = item.address;
				this.gethostmendianlist(1);
				this.showaddrs = false;
			},
			chooseCancel() {
				this.showaddrs = false;
			},
			ClickCancel() {
				if (this.notLongin) {
					this.gotologin();
					return;
				}
				this.showaddrs = false;
				console.log('ClickCancel');
				uni.navigateTo({
					url: '/pages/UserCenter/map/Chazhaodizhi'
				})
				// uni.navigateTo({
				// 	url:'/pages/UserCenter/map/mapN?state=0'
				// })
			},

			//获取附近热门门店
			gethostmendianlist(type) { //0按照评分查询，1按照热度查询，2按照新开店时间查询
				console.log(this.longitude, this.latitude)
				var that = this;
				api('getPfStore', {
					longitude: this.longitude,
					latitude: this.latitude,
					page: 1,
					juli: this.juli,
					type: type
				}, res => {
					console.log('getPfStore-----', res);
					if (res.data.code == 200) {
						this.mendianList = res.data.data;
					}
				}, failc => {
					uni.showToast({
						title: that.$t('api.wangluoshibai'),
						icon: 'none',
						duration: 2500
					})
				})
				this.BtStatesl(this.BtState);
			},

			//获取附近门店
			getmendianlist(latitude, longitude) {
				var that = this;
				that.fjmendian = [];
				api('getmendianlist', {
					longitude: longitude,
					latitude: latitude,
					page: 1,
					area: '',
					sort: '',
					juli: that.juli
				}, res => {
					// console.log('11',res);
					if (res.data.data) {
						that.fjmendian = res.data.data;
					} else {
						console.log('数据空')
					}
				}, failc => {
					uni.showToast({
						title: that.$t('api.wangluoshibai'),
						icon: 'none',
						duration: 2500
					})
				})

			},

			fenleixuanze(e) {
				if (this.notLongin) {
					this.gotologin();
					return;
				}
				uni.navigateTo({
					url: '/pages/shanpinxungou/shangjia/shangjia?id=' + e + '&latitude=' + this.latitude +
						'&longitude=' + this.longitude
				})
				return;
			},
			fwflxuanze(e) {
				// uni.navigateTo({
				// 	url:'/pages/imcall/audioPush?caller=0&&playUrl='
				// })
				// return;
				// var token = uni.getStorageSync("token");
				// console.log('2222',token)
				// if(token == '' || token == undefined || token == null){
				// 	uni.navigateTo({
				// 		url:'/pages/UserCenter/LoginView'
				// 	})
				// 	return;
				// }
				// uni.navigateTo({
				// 	url:'/pages/dache/DdcheshouyeN?type=3'
				// })
				// return;
				if (this.notLongin) {
					this.gotologin();
					return;
				}
				uni.navigateTo({
					url: '/pages/index/fuwuleixingList?serverType=' + e + '&latitude=' + this.latitude +
						'&longitude=' + this.longitude
				})
				return;
			},
			mendianxuanze(id) {
				// if(this.notLongin){
				// 	this.gotologin();
				// 	return;
				// }
				uni.navigateTo({
					url: '/pages/shanpinxungou/shangjia/shangjiaView?id=' + id + '&isShangjia=0'
				})
			},
			chongxindenglu() {
				var token = getApp().globalData.token;
				//console.log('chongxindenglu111',token);
				if (token == '' || token == undefined || token == null) {
					//console.log('chongxindenglu1112222');
					return;
				} else {
					//console.log('chongxindenglu111333');
					this.gengxintoken(token);
					return;
				}
			},
			gengxintoken(token) {

				api('refreshToken', {
					token: getApp().globalData.token
				}, res => {
					if (res.data.code == 200) {
						//console.log('refreshToken',res)
						this.notLongin = false;
						getApp().globalData.token = res.data.data;
						uni.setStorageSync("token", res.data.data);
						this.getCID();
					} else {
						getApp().globalData.token = '';
						uni.setStorageSync("token", '');
						console.log('refreshToken', res)
					}
				}, failc => {
					//console.log('getadvertis----',failc)
				})
			},
			getCID() {
				var cid = uni.getStorageSync("cid");
				//console.log('客户端推送标识:',cid)
				if (cid == '' || cid == null || cid == undefined) {
					var that = this;
					uni.getPushClientId({
						success: (res) => {
							console.log('客户端推送标识:', res.cid)
							// uni.showToast({
							// 	title:'客户端推送标识1:'+res.cid,
							// 	icon: 'none',
							// 	duration: 4000
							// })
							uni.setStorageSync("cid", res.cid);
							that.updataUserMsg(res.cid);
						},
						fail(err) {
							uni.showToast({
								title: '获取推送标识失败',
								icon: 'none',
								duration: 4000
							})
							console.log(err)
						}
					});
				} else {
					this.updataUserMsg(cid)
				}

			},
			updataUserMsg(cid) {
				api('updataUserMsg', { //159b0921b7ab3eb4f93a184d973b9fcb
					"cid": cid,
				}, res => {
					console.log('updataUserMsg：', cid, res.data)
				}, 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');
						// const threadClass = plus.ios.importClass("NSThread");
						// const mainThread = plus.ios.invoke(threadClass, "mainThread");
						// plus.ios.invoke(mainThread, "exit");
						plus.ios.import("UIApplication").sharedApplication().performSelector("exit")
					}
				}
				if (state == 1) {
					uni.showTabBar();
					this.$refs.popYsxy.close();
					uni.setStorageSync('yszche', 1);
					var yszche = uni.getStorageSync('yszche');
					console.log(yszche);
					this.MYgetLocation();
					this.$refs.flList.getmendianfenlei();
				}
				if (state == 2) {
					if (this.notLongin) {
						this.gotologin();
						return;
					}
					uni.navigateTo({
						url: '/pages/index/mindex/yinszhengc'
					})
				}
			},
			ringoffImCall() {
				api('ringoff', {}, res => {
					console.log(res)
				}, failc => {
					//console.log('getadvertis----',failc)
				})
			},
			//-------------------------------------------------
			BtStatesl(index) { //附近 好评  新店
				this.BtState = index;
				if (index == -1) {
					this.getmendianlist(this.latitude, this.longitude);
					return;
				}
				this.getPfStore(this.BtState);
			},
			//查询附近高评分门店等----------------------------------------------------------
			getPfStore(type) { //0按照评分查询，1按照热度查询，2按照新开店时间查询
				this.fjmendian = [];
				var that = this;
				api('getPfStore', {
					longitude: this.longitude,
					latitude: this.latitude,
					page: 1,
					juli: this.juli,
					type: type
				}, res => {
					//console.log('getPfStore-----',res);
					if (res.data.code == 200) {
						this.fjmendian = res.data.data;
					}
				}, failc => {
					uni.showToast({
						title: that.$t('api.wangluoshibai'),
						icon: 'none',
						duration: 2500
					})
				})
			},
			//自动更新
			getVersion() {
				return;
				var info = uni.getSystemInfoSync();
				if (info.platform.toLowerCase() != 'android') {}
				var appVersionCode = info.appVersionCode;
				var that = this;
				api('getversion', {
					platform: '0',
					name: 'CTE'
				}, res => {
					console.log(res);
					if (res.data.data) {
						var versionMSG = res.data.data;
						if (appVersionCode < versionMSG.version) {
							that.gengxincaozuo(versionMSG.renew);
						}
					}
				}, failc => {
					//console.log('getadvertis----',failc)
				});
			},

			gengxincaozuo(state) {
				var that = this;
				var content = "";

				var info = uni.getSystemInfoSync();

				if (state == 0) {
					content = that.$t('index.qiangzhiyiyouxinban');
				} else {
					content = that.$t('index.yiyouxinban');
				}

				uni.showModal({
					cancelText: that.$t('order.quxiao'), // 取消按钮的文字
					confirmText: that.$t('index.shengji'), // 确认按钮的文字 
					title: that.$t('api.message'),
					content: content,
					success: function(res) {
						if (res.confirm) { //这里是点击了确定以后
							var updateUrl = 'http://backend.cityexpress168.com.vn/androidApk/CTE.apk';
							that.gengxinApp(updateUrl);
						} else { //这里是点击了取消以后
							console.log('用户点击取消')
							if (state == 0) {
								plus.runtime.quit(); // 强制更新，App端退出应用
							}
						}

					}
				})
			},


			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: 240rpx;
	}

	.mynavbarV {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		z-index: 9998;
		background-color: #00A6FF;
	}

	.drawer {
		width: 100%;
		padding-bottom: 100rpx;
		position: absolute;
		background-color: white;
	}

	.words {
		width: 100%;
		text-align: center;
		font-size: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
	}

	.itemView {
		background-color: white;
		height: 80rpx;
		width: 100%;
		margin: 10rpx;
		padding: 16rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		border-radius: 20rpx;
		box-shadow: 0rpx 0rpx 10rpx 0rpx darkgray;
	}

	.addrV {
		color: black;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.tipyLBt {
		font-size: 28rpx;
		width: 33%;
		border-radius: 6px 0px 0px 6px;
		border: 2rpx solid lightgray;
		line-height: 64rpx;
		text-align: center;
		color: darkgray;
	}

	.tipyMBt {
		font-size: 28rpx;
		width: 33%;
		border: 2rpx solid lightgray;
		line-height: 64rpx;
		text-align: center;
		color: darkgray;
	}

	.tipyRBt {
		font-size: 28rpx;
		width: 33%;
		border-radius: 0px 6px 6px 0px;
		border: 2rpx solid lightgray;
		line-height: 64rpx;
		text-align: center;
		color: darkgray;
	}

	.item_content {
		background: white;
		width: 660rpx;
		height: 510rpx;
	}

	.mainimage {
		width: 660rpx;
		height: 330rpx;
		display: block;
		border-radius: 16rpx;
		background-color: floralwhite;
		border: 1rpx solid lightgray;
	}

	.Dishes {
		width: 100%;
		text-align: left;
		color: darkgray;
		font-size: 26rpx;
	}

	.price {
		width: 100%;
		text-align: left;
		//line-height: 28rpx;
		color: black;
	}

	.titletext {
		padding-top: 8rpx;
		width: 100%;
		text-align: left;
	}

	.loninnotev {
		position: fixed;
		bottom: 0rpx;
		width: 100%;
		background-color: whitesmoke;
		z-index: 9999;
	}

	.dengluzhuc {
		width: 50%;
		height: 60rpx;
		line-height: 60rpx;
		padding: 10rpx;
		border-radius: 20rpx;
		border: solid #00A6FF;
		border-width: 0px 0px 6rpx 0px;
		text-align: center;
		justify-content: center;
		background-color: #00A6FF;
		color: white;
		box-shadow: 0rpx 0rpx 10rpx 0rpx #00A6FF;
	}
</style>