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

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        name: null,
        checked: true,
        number: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        let id = options.id
        console.log(id);
        let TableNumber
        let tableNumber
        if (id) {
            TableNumber = new Parse.Query("TableNumber")
            tableNumber = await TableNumber.get(id)
            console.log(tableNumber.toJSON());
            this.setData({
                id: id,
                name: tableNumber.toJSON().name,
                checked: tableNumber.toJSON().status,
                number: tableNumber.toJSON().number,
            })
        }



    },
    blur(e) {
        let name = e.currentTarget.dataset.name
        this.setData({
            [name]: e.detail.value
        })
    },
    onChange({ detail }) {
        // 需要手动对 checked 状态进行更新
        this.setData({ checked: detail });
    },
    async submit() {
        const _this = this
        let merchant = wx.getStorageSync('merchant'); //用户
        let ShopStore = new Parse.Query('ShopStore')
        ShopStore.equalTo('user', merchant.objectId)
        let res = await ShopStore.first()
        let id = res.toJSON().objectId
        let Table = new Parse.Query("TableNumber")
        Table.equalTo("store", id)
        Table.equalTo("company", company)
        Table.equalTo("number", this.data.number)
        let table = await Table.first()
        if (table) {
            wx.showToast({
                title: '桌台编号已存在！',
                icon: 'none'
            })
            return
        }
        console.log(this.data.name, this.data.number)
        if (!this.data.name || !this.data.number) {
            wx.showToast({
                title: '请填写完整信息',
                icon: 'none'
            })
            return
        }
        let tableNumber
        let TableNumber
        if (this.data.id) {
            TableNumber = new Parse.Query("TableNumber")
            tableNumber = await TableNumber.get(this.data.id)
        } else {
            TableNumber = Parse.Object.extend("TableNumber")
            tableNumber = new TableNumber()
            tableNumber.set("company", {
                __type: "Pointer",
                className: "Company",
                objectId: company
            })
        }
        tableNumber.set("name", this.data.name)
        tableNumber.set("number", this.data.number)
        tableNumber.set("status", this.data.checked)
        tableNumber.set("store", {
            __type: "Pointer",
            className: "ShopStore",
            objectId: id
        })
        tableNumber.save().then(res => {
            console.log(res)
            if (this.data.id) {
                wx.showToast({

                    title: '修改成功',
                    icon: 'none'
                })
            } else {
                wx.showToast({

                    title: '创建成功',
                    icon: 'none'
                })
            }
            _this.setData({
                name: '',

            })

            wx.navigateBack({
                delta: 1

            });

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})