//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        hasPhone: true,
        imgUrls: [
            {'url': '../../images/banner.jpg'},
            {'url': '../../images/banner.jpg'},
            {'url': '../../images/banner.jpg'},],
        autoplay: true, //是否自动轮播
        interval: 2000, //间隔时间
        duration: 500, //滑动时间
        this_week_test_info: {
            is_this_week: true,
            ordered: false,
            is_past: false,
        },
        past_test_info: {
            info: [
                {time: '10月01号-10月07号', url: '../../images/test_img.jpg',},
                {time: '10月08号-10月14号', url: '../../images/test_img.jpg'},
                {time: '10月15号-10月21号', url: '../../images/test_img.jpg'},
            ],
            is_this_week: false,
            ordered: false,
            is_past: true,
        },
    },
    swiperChange: function (e) { //切换轮播图
        this.setData({
            currentSwiper: e.detail.current
        })
    },
    //首次登录小程序,跳转到认证手机页面
    goPhoneCode() {
        wx.navigateTo({url: '/pages/index/phone-code/phone-code'})
    },
    //点击购买畅玩卡
    goBuyCard() {
        wx.navigateTo({url: './activity-detail/activity-detail'})
    },
    //点击进入活动详情
    goTestDetail(e) {
        // console.log(e);
        const current = e.currentTarget.dataset.index;
        wx.navigateTo({url: './activity-detail/activity-detail'})
    },
    //前往预约
    goAppointment(e) {
        const current = e.currentTarget.dataset.index;
        wx.navigateTo({url: './go-appointment/go-appointment'})
    },
    //获取轮播图
    getSlideImage() {
        let url = '/portal/Index/index';
        let params = {
            city: this.data.city,
        };
        app.post(url, params).then((ret) => {
            wx.setStorageSync('token', ret.token)
            app.globalData.userInfo = ret.user_type
            // console.log('userInfo',app.globalData.userInfo,ret);
            if (ret.user_type == 2) {
                wx.switchTab({
                    url: '/pages/index/index',
                })
            }
        })
    },
    //获取本周实验
    getTheWeekTest() {
        let url = '/portal/Index/now';
        app.post(url, {}).then((res) => {
            console.log(res);
            this.setData({this_week_test_info: res})
            console.log(this.data.this_week_test_info);
        })
    },
    //查看本周实验详情
    goDetail() {
        wx.navigateTo({url: '/pages/index/activity-detail/activity-detail?test_id=' + this.data.this_week_test_info.id})
    },
    //获取往期实验
    getPastWeekTest() {
        let url = '/portal/Index/history';
        let params = {
            page: 1,
        }
        app.post(url, params,{}).then((res) => {
            console.log(res);
            this.setData({past_test_info: res})
            // console.log(this.data.this_week_test_info);
        })
    },
    //查看往期实验详情
    goPastDetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({url: '/pages/index/activity-detail/activity-detail?test_id=' + id})
    },
    onLoad: function () {
        this.getTheWeekTest();
        this.getPastWeekTest();
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                console.log(res);
                let url = '/public/getAddressInfoByCoordinate';
                let params = {
                    latitude: res.latitude,//纬度
                    longitude: res.longitude//经度
                };
                // app.post(url,params).then((res) => {
                //     // console.log('获取城市',res);
                //     self.setData({city: res.city})
                // })
            }
        });
    },
    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
})