let Parse = getApp().Parse;
const company = getApp().globalData.company
const compute = require("../../../utils/compute.js");
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        latitude: 0,
        longitude: 0,
        active: 0,
        list: [],
        value: null,
        showTab: false,
        stickytop: navigationBarHeight,
    },
    navigateComments(e) {
        let id = e.currentTarget.dataset.item.objectId
        wx.navigateTo({
            url: `/nova-tourism/pages/gourmet/store-package/comment/index?id=` + id
        })
    },
    async getData() {
        if (this.data.latitude == 0 && this.data.longitude == 0) {
            let { latitude, longitude } = await this.getLocation()
            this.setData({
                latitude,
                longitude
            })
            console.log(this.latitude, this.longitude);

        }
        this.getShopStore()
    },
    storepackage(e) {
        let id = e.currentTarget.dataset.item.objectId
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/gourmet/store-package/index?id=' + id
        });

    },
    getLocation() {
        return new Promise((resolve, reject) => {
            wx.getLocation({
                type: 'gcj02',
                success: (res) => {
                    console.log(res.latitude, res.longitude);
                    resolve({
                        latitude: res.latitude,
                        longitude: res.longitude
                    })
                },
                fail: () => { },
                complete: () => { }
            });
        })


    },



    onSearch() {
        wx.showToast({
            title: `搜索 ${this.data.value}`,
            icon: 'none',
        });

    },
    onClick() {
        wx.showToast({
            title: `搜索 ${this.data.value}`,
            icon: 'none',
        });
    },
    onChan(e) {
        this.setData({
            value: e.detail,
        });
        this.getShopStore()
    },
    onChange(event) {
        let active = event.detail.name
        console.log(active);
        this.setData({
            active: active
        })
        this.getShopStore()

    },
    async getShopStore() {
        let ShopStore = new Parse.Query('ShopStore')
        ShopStore.equalTo('company', company)
        ShopStore.equalTo('type', "catering")
        ShopStore.notEqualTo("isDeleted",true)
        if (this.data.active == 0) {
            ShopStore.descending('isShow')
        }
        if (this.data.active == 1) {
            ShopStore.descending('score')
        }
        if (this.data.active == 3) {
            ShopStore.ascending('perCapita')
        }
        if (this.data.value) {
            ShopStore.contains('storeName', this.data.value)
        }
        let shopStores = await ShopStore.find()
        if (shopStores) {
            let listJSON = []
            for (let i = 0; i < shopStores.length; i++) {
                listJSON.push(shopStores[i].toJSON())
            }

            for (let i = 0; i < listJSON.length; i++) {
                let item = listJSON[i]
                if (item.location) {
                    console.log(this.data.latitude, this.data.longitude);
                    let distance = compute.computeDistance(
                        this.data.latitude,
                        this.data.longitude,
                        item.location.latitude,
                        item.location.longitude
                    )
                    item.distance = distance.toFixed(2)

                    listJSON[i] = item
                }
            }
            //按距离排序
            if (this.data.active == 2) {
                listJSON = listJSON.sort((a, b) => {
                    return a.distance - b.distance
                })
            }

            this.setData({
                list: listJSON
            })
        }
        console.log(this.data.list);
    },
    onPageScroll: function (e) {
        var that = this;
        var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
        //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
        var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
        // 只有处于吸顶的临界值才会不相等
        if (that.data.isFixedTop === isSatisfy) {
            return false;
        }

        that.setData({
            isFixedTop: isSatisfy
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        this.getData()
        this.getShopStore()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (that.data.navbarInitTop == 0) {
            //获取节点距离顶部的距离
            wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
                if (rect && rect.top > 0) {
                    var navbarInitTop = parseInt(rect.top);
                    that.setData({
                        navbarInitTop: navbarInitTop
                    });
                }
            }).exec();

        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '美食预定',

        }
    }
})