const app = getApp()
import { race_status } from '../../utils/dict';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '淘汰赛管理', leftArrow: true, useBar: false },
        list: [],
        dialog: { title: '赛程信息', show: false, type: '1' },
        form: {},
        // 赛事信息
        matchList: [],
        // 组别
        groupingList: [],
        //组内项目
        projectList: [],
        statusList: race_status,
    },
    // 跳转菜单
    back(e) {
        wx.navigateBack({ delta: 1 })
    },
    // 赛事管理
    toScore: async function (e) {
        const that = this;
        const { item } = e.currentTarget.dataset;
        const arr = await app.$get(`/newCourt/api/eliminateRace/${item._id}`);
        if (arr.errcode == '0') { that.setData({ form: arr.data }) }
        that.setData({ dialog: { title: '赛事管理', show: true, type: '2' } });
    },
    // 选择比赛状态
    statusChange: function (e) {
        const that = this;
        let data = that.data.statusList[e.detail.value];
        if (data) { that.setData({ 'form.status': data.value }) }
    },
    // 提交保存
    scoreSubmit: async function (e) {
        const that = this;
        const params = e.detail.value;
        const arr = await app.$post(`/newCourt/api/eliminateRace/${params._id}`, params);
        if (arr.errcode == '0') {
            wx.showToast({ title: `信息维护成功`, icon: 'error', duration: 2000 })
            that.toClose()
        } else {
            wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
        }
    },
    // 删除
    toDel: function (e) {
        const that = this;
        const { item } = e.currentTarget.dataset;
        wx.showModal({
            title: '提示',
            content: '是否确认删除该条数据?',
            async success(res) {
                if (res.confirm) {
                    const arr = await app.$delete(`/newCourt/api/eliminateRace/${item._id}`);
                    if (arr.errcode == '0') {
                        wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 })
                        that.watchLogin()
                    } else {
                        wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
                    }
                }
            }
        })
    },
    // 添加
    toAdd: function () {
        const that = this;
        that.setData({ dialog: { title: '赛程信息', show: true, type: '1' } })
    },
    // 关闭弹框
    toClose: function () {
        const that = this;
        that.setData({ form: {} })
        that.setData({ dialog: { title: '赛程信息', show: false, type: '1' } })
        that.watchLogin()
    },
    // 选择赛事
    matchChange: function (e) {
        const that = this;
        let data = that.data.matchList[e.detail.value];
        if (data) {
            that.setData({ 'form.match_id': data._id });
            that.setData({ 'form.match_name': data.name });
            if (data.grouping && data.grouping.length > 0) {
                that.setData({ groupingList: data.grouping })
            }
        }
    },
    // 选择赛事组别
    grpupChange: async function (e) {
        const that = this;
        let data = that.data.groupingList[e.detail.value];
        if (data) {
            that.setData({ 'form.grouping_id': data._id });
            that.setData({ 'form.grouping_name': data.name });
            if (data.project && data.project.length > 0) {
                let projectList = [];
                for (const val of data.project) {
                    const arr = await app.$get(`/newCourt/api/matchProject/${val}`)
                    if (arr.errcode == '0') projectList.push(arr.data);
                    that.setData({ projectList: projectList })
                }
            }
        }
    },
    // 选择组内项目
    projectChange: function (e) {
        const that = this;
        let data = that.data.projectList[e.detail.value];
        if (data) {
            that.setData({ 'form.project_id': data._id });
            that.setData({ 'form.project_name': data.name });
        }
    },
    // 赛程信息
    onSubmit: function (e) {
        const that = this;
        const params = e.detail.value;
        if (params.match_id && params.grouping_id && params.project_id) {
            wx.navigateTo({ url: `/pages/eliminate/add?match_id=${params.match_id}&grouping_id=${params.grouping_id}&project_id=${params.project_id}` })
        } else {
            wx.showToast({ title: `缺少必要信息`, icon: 'error', duration: 2000 })
        }

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this;
        that.watchLogin();
    },
    // 监听用户是否登录
    watchLogin: async function () {
        const that = this;
        wx.getStorage({
            key: 'user',
            success: async res => {
                let arr;
                // 比赛信息
                arr = await app.$get(`/newCourt/api/match`, { status: '0' });
                if (arr.errcode == '0') { that.setData({ matchList: arr.data }) };
                // 淘汰赛信息
                arr = await app.$get(`/newCourt/api/eliminateRace`);
                if (arr.errcode == '0') { that.setData({ list: arr.data }) };
            },
            fail: res => {
                wx.redirectTo({ url: '/pages/index/index', })
            }
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})