const app = getApp()
import WxValidate from '../../utils/wxValidate';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '话题', leftArrow: true, useBar: false },
        id: '',
        info: {},
        projectList: [],
        dialog: { title: '赛事组别', show: false, type: '1' },
        form: { project: [] }
    },
    initValidate() {
        const rules = { name: { required: true }, age: { required: true }, gender: { required: true } }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = { name: { required: '请输入名称' }, age: { required: '请输入年龄限制' }, gender: { required: '请输入性别限制' } };
        this.WxValidate = new WxValidate(rules, messages)
    },
    // 跳转菜单
    back(e) {
        wx.navigateBack({ delta: 1 })
    },
    // 添加
    toAdd: function () {
        const that = this;
        that.setData({ dialog: { title: '赛事组别', show: true, type: '1' } });
    },
    // 信息删除
    toDel: async function (e) {
        const that = this;
        const info = that.data.info;
        const { item } = e.currentTarget.dataset;
        wx.showModal({
            title: '提示',
            content: '您确定要删除该条数据吗？',
            async success(res) {
                if (res.confirm) {
                    let grouping = info.grouping.filter((i) => i._id != item._id);
                    const arr = await app.$post(`/newCourt/api/match/${info.id}`, { grouping: grouping })
                    if (arr.errcode == '0') {
                        wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 });
                        that.watchLogin()
                    } else {
                        wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
                    }
                }
            }
        })
    },
    checkboxChange: function (e) {
        const that = this;
        that.setData({ 'form.project': e.detail.value });
    },
    // 提交
    onSubmit: async function (e) {
        const that = this;
        const params = e.detail.value;
        const info = that.data.info;
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0];
            wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
            return false
        } else {
            let grouping = [...info.grouping, params];
            const arr = await app.$post(`/newCourt/api/match/${that.data.id}`, { grouping: grouping })
            if (arr.errcode == '0') {
                that.watchLogin();
                that.toClose();
            } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }

        }
    },
    // 关闭弹框
    toClose: function () {
        const that = this;
        that.setData({ form: {} })
        that.setData({ dialog: { title: '赛事组别', show: false, type: '1' } })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        that.setData({ id: options.id || '62e23cd3bb22e03d0b49d73b' });
        //验证规则函数
        that.initValidate();
        that.watchLogin()
    },
    watchLogin: function () {
        const that = this;
        wx.getStorage({
            key: 'user',
            success: async (res) => {
                if (that.data.id) {
                    let arr;
                    arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
                    if (arr.errcode == '0') {
                        that.setData({ info: arr.data })
                    } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
                    arr = await app.$get(`/newCourt/api/matchProject`, { skip: 0, limit: 1000 });
                    if (arr.errcode == '0') that.setData({ projectList: arr.data })
                }
            },
            fail: async (res) => {
                wx.redirectTo({ url: '/pages/index/index' });
            },
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})