// index.js
let myPro = require("../../utils/wxRequest.js");
let util = require("../../utils/util.js");

Page({
    data: {
		interval: '3000', // 自动切换时间间隔
		duration: '500', // 滑动动画时长
		adList: [],
		current: 0,
		imgUrl: getApp().globalData.imgUrl,
		userInfo: null,
		token: null,
		setBgHeight: getApp().globalData.setBgHeight,
		topBannerList: [],
		intervalTopBanner: '3000', // 自动切换时间间隔
		durationTopBanner: '500', // 滑动动画时长
		currentTopBanner: 0
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 顶部banner
		that.getTopBanner();
		// 一元加购 1加购
		that.getAdList(1);
		// 广告位 3底部滚动
		that.getAdList(3);
		// that.setData({
		// 	token: getApp().globalData.token,
		// 	userInfo: getApp().globalData.userInfo
		// });
		if(wx.getStorageSync('token')){
			that.setData({
				token: wx.getStorageSync('token')
			})
		};
		// console.log('onshow',getApp().globalData);
		// 清掉
		wx.removeStorageSync('fare_type');
		wx.removeStorageSync('checkedAddress');
		// wx.removeStorageSync('store');
		// console.log('token',that.data.token)
		if(that.data.token){
			that.getUserInfo();
		}
    },
    onReady: function () {
        // Do something when page ready.
		let that = this;
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
	onShareAppMessage: function () {
        // return custom share data when user share.
    },
    onPageScroll: function () {
        // Do something when page scroll
    },
    onResize: function () {
        // Do something when page resize
    },
	// 广告banner
	getAdList(cateId){
		let that = this;
		let params = {
			cate_id: cateId
		};
		myPro.wxRequest("user/v3/article/list","GET",params).then(res=>{
			// 底部滚动
			if(cateId == 3){
				that.setData({
					adList: res.result
				});
			};
			// 加购
			if(cateId == 1){
				that.setData({
					activityFirst: res.result[0]
				});
			};
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 广告轮播图切换
	changeAd(event){
		let that = this;
		that.setData({
			current: event.detail.current,
		});
	},
	// 广告跳转(单页)
	goAdUrl(event){
		let that = this;
		let item = event.currentTarget.dataset.item;
		wx.navigateTo({
			url: '/pages/singlepage/index?id='+item.id
		});
	},
	// 页面跳转
	goPage(event){
		let that = this;
		console.log(event)
		let url = event.currentTarget.dataset.url;
		wx.navigateTo({
			url: url
		});
	},
	// 自取流程
	goStore(){
		let that = this;
		// 配送方式 0外卖 1自提
		wx.setStorage({
			key: 'fare_type',
			data: '1'
		});
		// 因为是单店(所以直接去商品页)
		// wx.navigateTo({
		// 	url: '/pages/goods/index'
		// });
		wx.navigateTo({
			url: '/pages/tables/index'
		});
	},
	// 外卖流程
	goGoods(){
		let that = this;
		// 配送方式 0外卖 1自提
		wx.setStorage({
			key: 'fare_type',
			data: '0'
		});
		wx.navigateTo({
			url: '/pages/user/address/list/index'
		});
	},
	// 用户信息
	getUserInfo(){
		let that = this;
		myPro.wxRequest("user/v2/userinfo","GET",{}).then(res=>{
			that.setData({
				userInfo: res.result
			})
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: 'none'
			});
		});
	},
	// 去登录
	toLogin(){
		let that = this;
		wx.reLaunch({
			url: '/pages/login/index/index'
		})
	},
	// 一元购 (单页)
	goActivityPage(){
		let that = this;
		let id = that.data.activityFirst.id;
		wx.navigateTo({
			url: '/pages/singlepage/index?id='+id
		});
	},
	// 顶部banner
	getTopBanner(){
		let that = this;
		myPro.wxRequest("user/v2/banner","GET",{}).then(res=>{
			that.setData({
				topBannerList: res.result
			});
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		});
	},
	// 顶部banner切换
	changeTopBanner(event){
		let that = this;
		that.setData({
			currentTopBanner: event.detail.current,
		});
	}
});
