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

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '赛事信息', leftArrow: false, useBar: false },
        fileUrl: app.globalData.fileUrl,
        id: '',
        info: {},
        // 选项卡
        tabs: {
            active: 'a',
            list: [
                { title: '规程', name: 'a' },
                { title: '选手', name: 'b' },
                { title: '秩序册', name: 'c' },
                { title: '赛况', name: 'd' },
                { title: '成绩册', name: 'e' },
            ],
        },
        // 场地选项卡
        fieldtabs: {
            active: '0',
            list: [],
        },
        // 选手
        bActive: '0',
        groupList: [],
        // 场次表
        sessionsList: [
            {
                person: [
                    { name: "用户1" },
                    { name: "用户2" }],
                red_members: [
                    { name: "用户3" },
                    { name: "用户4" }],
            },
            {
                person: [
                    { name: "用户1" },
                    { name: "用户2" }],
                red_members: [
                    { name: "用户3" },
                    { name: "用户4" }],
            },
            {
                person: [
                    { name: "用户2" }],
                red_members: [
                    { name: "用户4" }],
            },
            {
                person: [
                    { name: "用户2" }],
                red_members: [
                    { name: "用户4" }],
            },
            {
                person: [
                    { name: "用户2" }],
                red_members: [
                    { name: "用户4" }],
            },
        ],
        // 对阵表
        againstList: [
            { name: '青年一组 A组' }, { name: '青年一组 B组' },
            { name: '青年一组 C组' }, { name: '青年一组 D组' },
            { name: '青年二组 A组' }, { name: '青年二组 B组' },
            { name: '青年二组 C组' }, { name: '青年二组 D组' },
            { name: '青年一组 A组' }, { name: '青年一组 B组' },
            { name: '青年一组 C组' }, { name: '青年一组 D组' },
            { name: '青年二组 A组' }, { name: '青年二组 B组' },
            { name: '青年二组 C组' }, { name: '青年二组 D组' },
        ],
        // 场次/对阵表
        switch: '2',
        //比赛进程安排
        racelist: [
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                player_two: '王子铭(龙伟羽校)',
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            },
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                player_two: '王子铭(龙伟羽校)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            },
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                player_two: '王子铭(龙伟羽校)',
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            },
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                player_two: '王子铭(龙伟羽校)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            }, {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                player_two: '王子铭(龙伟羽校)',
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            },
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                player_two: '王子铭(龙伟羽校)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            }, {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                player_two: '王子铭(龙伟羽校)',
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            },
            {
                match_time: '2022-07-03 16:18',
                ground_name: '大馆场地1',
                player_one: '刘泽皓轩(琦迹羽球)',
                player_two: '王子铭(龙伟羽校)',
                person: [{ name: '张益涵' }, { name: '朱宇航' }, { name: '李帅' }],
                score_one: '6',
                score_two: '21',
                match_name: '青少组小学男单 半决赛 [青少组MS113]'
            }
        ],
        btnList: [],
        btnActive: ['1'],
        active: '',
    },
    // 选项卡
    tabsChange: function (e) {
        const that = this;
        const { name } = e.detail;
        that.setData({ 'tabs.active': name });
    },
    // 场地选项卡
    fieldtabsChange: async function (e) {
        const that = this;
        const { name } = e.detail;
        that.setData({ 'fieldtabs.active': name });
        let group = that.data.fieldtabs.list.find((i) => i.name == name);
        if (group && group.name != 0) {
            that.setData({ active: group.name });
            // const arr = await app.$get(`/newCourt/api/race`, { ground_id: group._id });
            // if (arr.errcode == '0') {
            //     that.setData({ racelist: arr.data })
            // }
        }
    },
    toCommon: function (e) {
        const { route } = e.currentTarget.dataset;
        if (route) { wx.navigateTo({ url: `/pages/${route}` }) };
    },
    // 选择手风琴
    bChange: function (e) {
        const that = this;
        that.setData({ btnActive: e.detail })
    },
    // 更多
    toCommon: function (e) {
        const { route } = e.currentTarget.dataset;
        wx.redirectTo({ url: `/pages/${route}/index` })
    },
    // 报名
    toSign: function (e) {
        const { item } = e.currentTarget.dataset;
        wx.navigateTo({ url: `/pages/matchInfo/sign?id=${item._id}` })
    },
    // 选择手风琴
    bChange: function (e) {
        const that = this;
        that.setData({ bActive: e.detail })
    },
    // 查看场次/对阵表
    toSwitch: function (e) {
        const that = this;
        that.setData({ switch: e.currentTarget.dataset.switch })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        that.setData({ id: options.id || '62e388d4fd61d871861b80af' });
        // 监听用户是否登录
        that.watchLogin();
    },
    watchLogin: async function () {
        const that = this;
        if (that.data.id) {
            let arr;
            arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
            if (arr.errcode == '0') {
                arr.data.statusZh = that.getStatusZh(arr.data.status);
                that.setData({ info: arr.data })
                const ground = await app.$get(`/newCourt/api/ground`);
                if (ground.errcode == '0') {
                    let groundList = []
                    ground.data.unshift({ _id: '', title: '全部场地', name: '0' });
                    for (const [index, val] of ground.data.entries()) {
                        groundList.push({ _id: val._id, title: val.title || val.name, name: index })
                    }
                    that.setData({ 'fieldtabs.list': groundList })
                    const matchProject = await app.$get(`/newCourt/api/matchProject`);
                    if (matchProject.errcode == '0') {
                        that.setData({ btnList: matchProject.data })
                    }
                }
            } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
            arr = await app.$get(`/newCourt/api/view/groupProjectUser?match_id=${that.data.id}`);
            if (arr.errcode == '0') { that.setData({ groupList: arr.data }) }
        }
    },
    getStatusZh(status) {
        let word = "未知"
        let data = match_status.find((i) => i.label == status);
        if (data) word = data.value;
        return word;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})