const Parse = getApp().Parse;
const app = getApp();
const company = getApp().globalData.company
Page({
    /**
     * 页面的初始数据
     */
    data: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        rightCode: "",
        pageType: "login", //register,login ,forget
        time: 60000,
        waitStatus: false,

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            activeColor: getApp().globalData.activeColor || '#229293'
          })
    },
    onClick(e) {
        let {
            type,
            key,
            value
        } = e.currentTarget.dataset;
        switch (type) {
            case "register":
                this.setData({
                    [key]: value,
                });
                break;
            case "forget":
                this.setData({
                    [key]: value,
                });
                break;
            case "login":
                this.setData({
                    [key]: value,
                });
                break;
        }
    },

    /* 用户登录 */
    async userLogin() {
        if (this.data.username.trim() == '' || this.data.password.trim() == "") {
            wx.showToast({
                icon: "none",
                title: "请填写用户名/密码",
            });
            return;
        }
        wx.request({
            url: "https://test.fmode.cn/api/auth/store_user",
            method: "post", //请求方式
            data: {
                username: this.data.username,
                password: this.data.password,
                company: company,
            },
            success(res) {
                console.log(res)
                if(res && res.data &&  res.data.code == 200) {
                    wx.showToast({
                        icon: "none",
                        title: "登录成功，页面跳转中",
                    });
                    let store = res.data.data.store
                    let merchant = res.data.data.merchant
                    wx.setStorageSync('merchant', merchant)
                    wx.setStorageSync('store', store)
                    wx.reLaunch({
                        url: `/nova-tourism/pages/my/merchant/merchant-home/index?type=${store.type}`,
                    });
                }else {
                    wx.showToast({
                        icon: "none",
                        title: res.data.mess
                    });
                }
            },
            fail(error) {
                wx.showToast({
                    icon: "none",
                    title: error.data.mess
                });
                return
            }
        })
        // let User = new Parse.Query('User')
        // User.equalTo('username', this.data.username)
        // User.equalTo('company', company)
        // let user = await User.first()
        // if (user && user.id) {
        //     let store = await this.checkAdmin(user)
        //     if (!store) {
        //         wx.showToast({
        //             icon: "none",
        //             title: "非商户用户，登录失败",
        //         });
        //         return
        //     }
        //     wx.showToast({
        //         icon: "none",
        //         title: "登录成功，页面跳转中",
        //     });
        //     wx.setStorageSync('merchant', user)
        //     wx.setStorageSync('store', store)
        //     wx.reLaunch({
        //         url: `/nova-tourism/pages/my/merchant/merchant-home/index?type=${store.type}`,
        //     });
        // } else {
        //     wx.showToast({
        //         title: "该商户不存在",
        //         icon: "none",
        //     });
        // }

    },
    async checkAdmin(user) {
        let ShopStore = new Parse.Query('ShopStore')
        ShopStore.notEqualTo('isDeleted', "true")
        ShopStore.equalTo('user', user.id)
        ShopStore.equalTo('company', company)
        let store = await ShopStore.first()
        if (store && store.id) {
            return store
        }
        return false
    },

    inputChange(event) {
        if (event.currentTarget.dataset.type == "username") {
            this.setData({
                username: event.detail.value,
            });
            return;
        }
        if (event.currentTarget.dataset.type == "password") {
            this.setData({
                password: event.detail.value,
            });
            return;
        }
        if (event.currentTarget.dataset.type == "code") {
            this.setData({
                code: event.detail.value,
            });
            return;
        }
        if (event.currentTarget.dataset.type == "confirmPassword") {
            this.setData({
                confirmPassword: event.detail.value,
            });
            return;
        }
    },

    async goBack() {
        wx.navigateBack({
            delta: 1,
        });
    },
})