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

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        let id = options.id
        let type = options.type
        console.log(id);
        let Category
        let category
        if (id) {
            Category = new Parse.Query("Category")
            category = await Category.get(id)
            console.log(category.toJSON());
            this.setData({
                id: id,
                type: type,
                name: category.toJSON().name,
                desc: category.toJSON().desc,
                number: category.toJSON().index,
            })
        }
        this.setData({ type: type })
        console.log(this.data.type);
    },
    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 Category = new Parse.Query("Category")
        Category.equalTo("store", id)
        Category.equalTo("company", company)
        Category.equalTo("index", this.data.number)
        let table = await Category.first()
        if (table) {
            if (this.data.number = table.toJSON().index) {

            } else {

                wx.showToast({
                    title: '类型排序已存在！',
                    icon: 'none'
                })
                return
            }
        }



        console.log(this.data.name, this.data.number, this.data.desc)
        if (!this.data.name || !this.data.number || !this.data.desc) {
            wx.showToast({
                title: '请填写完整信息',
                icon: 'none'
            })
            return
        }
        let category2
        let Category2
        if (this.data.id) {
            Category2 = new Parse.Query("Category")
            category2 = await Category2.get(this.data.id)
        } else {
            Category2 = Parse.Object.extend("Category")
            category2 = new Category2()
            category2.set("company", {
                __type: "Pointer",
                className: "Company",
                objectId: company
            })
        }
        category2.set("name", this.data.name)
        category2.set("index", Number(this.data.number))
        category2.set("desc", this.data.desc)
        category2.set("status", this.data.checked)
        category2.set("type", this.data.type)
        category2.set("store", {
            __type: "Pointer",
            className: "ShopStore",
            objectId: id
        })
        category2.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 () {

    }
})