let Parse = getApp().Parse;
const company = getApp().globalData.company
const dateF = require('../../../../utils/date')
const compute = require("../../../../utils/compute.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        id: null,
        shopstore: null,
        list: [],
        score: 4,
        comments: null,
        latitude: null,
        longitude: null,
        distance: null,
        activeColor: '#229293'
    },
    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: () => { }
            });
        })

    },
    async getComments() {
        let Order = new Parse.Query('ShopOrder')
        Order.equalTo("shopStore", this.data.id);
        Order.include("user");
        Order.include("goods");
        Order.greaterThanOrEqualTo("status", 800);
        Order.ascending('updatedAt')
        Order.notEqualTo('hiddenComment', true)
        Order.limit(10)
        Order.find().then(res => {
            let comments = []
            res.forEach(item => {
                let activitys = item.toJSON()
                if(activitys.goods) activitys.time = dateF.formatTime("YYYY-mm-dd HH:MM:SS", activitys.goods.updatedAt)
                comments.push(activitys)
            })


            this.setData({
                comments
            }),
                console.log(this.data.comments)
        })
    },
    onevaluation(event) {

        let active = event.detail.name
        console.log(active);
        this.setData({
            active: active
        })
    },
    taxi() {
        let latitude = this.data.shopstore.location.latitude;
        let longitude = this.data.shopstore.location.longitude;
        console.log(latitude, longitude);
        wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success(res) {
                wx.openLocation({
                    latitude,
                    longitude,
                    scale: 18
                })
            },

        })
    },
    phone() {
        let phone = this.data.shopstore.mobile
        console.log(phone);
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    async getShopgoods() {
        let Coupon = new Parse.Query('ShopGoods')
        Coupon.notEqualTo('isDeleted', "true")
        Coupon.equalTo('shopStore', this.data.id)
        Coupon.equalTo('status', 'true')
        Coupon.equalTo('type', 'meal')
        let coupon = await Coupon.find()
        if (coupon && coupon.length > 0) {
            let listJSON = []
            coupon.forEach(c => {
                listJSON.push(c.toJSON())
            })

            this.setData({
                list: listJSON
            })
        }
        console.log(this.data.list.length);
    },
    async getShopStore() {
        let id = this.data.id
        let ShopStore = new Parse.Query('ShopStore')
        let shopstore = await ShopStore.get(id)
        let distances = compute.computeDistance(
            this.data.latitude,
            this.data.longitude,
            shopstore.toJSON().location.latitude,
            shopstore.toJSON().location.longitude
        )
        let distance = distances.toFixed(2)
        this.setData({
            shopstore: shopstore.toJSON(),
            distance: distance
        })
        console.log(this.data.distance);
    },
    async tableList() {
        let TableList = new Parse.Query('TableNumber')
        TableList.equalTo('store', this.data.id)
        TableList.equalTo('company', company)
        TableList.equalTo('status', true)
        TableList.select('name', "objectId", "number")
        let tableList = await TableList.find()
        console.log(tableList);
        if (tableList && tableList.length > 0) {
            let tableArr = []
            console.log(tableList)
            tableList.forEach(li => {
                tableArr.push({
                    id: li.id,
                    name: li.get('name'),
                    number: li.get('number')
                })
            })
            this.setData({
                tableList: tableArr,
                // table: tableArr[0]
            })
            console.log(this.data.tableList)
        }
    },
    comment() {
        let id = this.data.id
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/gourmet/store-package/comment/index?id=' + id
        });

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

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let id = options.id
        console.log(id);
        this.setData({ id: id })
        this.getShopStore()
        this.getShopgoods()
        this.getComments()
        this.getLocation()
        this.tableList()
    },

    toOrder() {
        wx.navigateTo({
            url: '/nova-tourism/pages/gourmet/store-package/meal-order/index?storeid=' + this.data.id
        });
    },

    toWebView(e) {
        console.log(e)
        let path = e.currentTarget.dataset.path
        if (!path) {
            wx.showToast({
                title: 'VR素材收集中',
                duration: 1500,
                icon: "none",
            })
            return
        }
        wx.navigateTo({
            url: '/common-page/pages/web-view/index?path=' + path,

        })
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: `${this.data.shopstore.storeName}`,

        }
    }
})