const Parse = getApp().Parse;
const company = getApp().globalData.company
Page({

    /**
     * 页面的初始数据
     */
    data: {
        storeId: '',
        current: null,
        store: null,
        activeColor:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let storeID = options.store
        await this.getShopStore()
        await this.getBindStore()
    },

    // 获取所有店铺信息
    getShopStore: async function () {
        let Store = new Parse.Query('ShopStore')
        Store.notEqualTo('isDeleted', "true")
        Store.equalTo('company', company)
        Store.equalTo('type', 'shop')
        Store.descending('createdAt')
        let store = await Store.find()
        let list = []
        store.forEach(val => {
            val = val.toJSON()
            list.push(val)
        })

        let current = list.filter(val => {
            return val.objectId == this.data.storeID
        })[0]
        this.setData({
            store: list,
            current: current
        })
    },

    // 获取当前绑定的店铺
    getBindStore: async function () {
        let storeID = wx.getStorageSync('storeID');
        let Store = new Parse.Query('ShopStore')
        let store = await Store.get(storeID)
        let storeJSON = store.toJSON()
        this.setData({
            current: storeJSON
        })
    },

    // 点击店铺绑定
    bindStore(e) {
        let id = e.currentTarget.dataset.id
        wx.setStorageSync('storeID', id);
        wx.navigateBack({
            delta: 1,
        });
        setTimeout(() => {
            wx.showToast({
                title: '选择完毕',
                icon: 'success',
                duration: 1500,
            });
        }, 200)

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})