let Parse = getApp().Parse;
const company = getApp().globalData.company
const login = require('../../../../../utils/login')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        storeid: "", // 店铺id
        tableid: "",// 桌号
        list: [
            { value: 1, childe: false },
            { value: 2, childe: false },
            { value: 3, childe: false },
            { value: 4, childe: true },
            { value: 5, childe: false },
            { value: 6, childe: false },
            { value: 7, childe: false },
            { value: 8, childe: false },
            { value: 9, childe: false },
            { value: 10, childe: false },
            { value: 11, childe: false },
            { value: '更多', childe: false },
        ],
        desc: "",
        tableList: [],
        currentIndex: 0,
        table: null,
        active: 4,  // 用餐人数
        activeColor:"#229293"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let storeid = options.storeid
        let tableid = options.tableid
        this.setData({
            storeid: storeid,
            tableid: tableid
        })
        let userLogin = wx.getStorageSync("userLogin");
        if (userLogin == "") {
            login.loginNow();
            return;
        }
        if (!storeid) {
            return
        }
        this.getStore(storeid)
        this.getTable(tableid)
        this.tableList()
    },

    async getStore(id) {
        let ShopStore = new Parse.Query('ShopStore')
        let store = await ShopStore.get(id)
        if (store && store.id) {
            this.setData({
                store: store.toJSON()
            })
        } else {
            wx.showToast({
                title: '未找到该店铺',
                duration: 1600,
                icon: icon,
            })
            wx.navigateBack({
                delta: 1,
            })
        }
    },

    async getTable(id) {
        if (id) {
            let Table = new Parse.Query("TableNumber")
            let table = await Table.get(id)
            if (table && table.id) {
                this.setData({
                    table: {
                        id: table.id,
                        name: table.get('name'),
                        number: table.get('number')
                    }
                })
            }
        }
    },

    async tableList() {
        let TableList = new Parse.Query('TableNumber')
        TableList.equalTo('store', this.data.storeid)
        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)
        }
    },

    onChang(e) {
        let list = this.data.list
        let value = e.currentTarget.dataset.value
        let show = false
        let active = list[value].value
        list.forEach((item, index) => {
            list[index].childe = false
        });
        if (value == 11) {
            show = true
            active = 0
        }
        list[value].childe = true
        this.setData({
            list,
            show,
            active
        })
    },


    bindPickerChange(e) {
        console.log(e)
        this.setData({
            currentIndex: Number(e.detail.value),
            tableid: this.data.tableList[e.detail.value].id,
            table: this.data.tableList[Number(e.detail.value)]
        })
    },
    startOrder() {
        console.log(this.data.storeid)
        console.log(this.data.table)
        console.log(this.data.active)
        if (!this.data.storeid) {
            wx.showToast({
                title: '店铺信息有误，请重新进入',
                duration: 1500,
                icon: "none"
            })
            return
        }
        if (!this.data.table) {
            wx.showToast({
                title: '请先确认桌号信息',
                duration: 1500,
                icon: "none"
            })
            return
        }
        if (!this.data.active) {
            wx.showToast({
                title: '请先确认用餐人数',
                duration: 1500,
                icon: "none"
            })
            return
        }
        let storeid = this.data.storeid
        let tableid = this.data.table.id
        let active = this.data.active
        let desc = this.data.desc
        wx.navigateTo({
            url: `/nova-tourism/pages/gourmet/store-package/meal-order/order-detail/index?storeid=${storeid}&tableid=${tableid}&active=${active}&desc=${desc}`
        })
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getStore(this.data.storeid)
        this.getTable(this.data.tableid)
        this.tableList()
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})