// commpents/match-achieve/achieve.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        ranking: { type: Array },
        race: {
            type: Array,
            observer: function (val) {
                this.resetTableData(val)
            }
        }

    },

    /**
     * 组件的初始数据
     */
    data: {
        raceList: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 重新整理数据
        resetTableData(data) {
            const arr = [];
            for (const t of data) {
                const { list, team_id, team_name } = t
                const m1 = list.map(i => ({ player_id: i.player_one, player_name: i.player_one_name }))
                const m2 = list.map(i => ({ player_id: i.player_two, player_name: i.player_two_name }))
                const m3 = [...m1, ...m2]
                const player_ids = Array.from(new Set(m3.map(i => i.player_id)))
                // 选手列表
                const playerList = [];
                for (const player_id of player_ids) {
                    const r = m3.find(f => f.player_id === player_id)
                    if (r) playerList.push({ player_name: r.player_name, player_id })
                }
                // 组织列表内容
                const scoreList = [];
                for (const player of playerList) {
                    const { player_id, player_name } = player
                    const arr = [];
                    arr.push(player_name)
                    for (const head of playerList) {
                        const { player_id: head_player_id } = head;
                        if (head_player_id === player_id) arr.push('')
                        else {
                            // 找到左右的分数
                            // 先找到这俩人的比赛
                            const race = list.find(f => (f.player_one === player_id && f.player_two === head_player_id) || (f.player_one === head_player_id && f.player_two === player_id))
                            if (!race) continue;
                            let left, right;
                            if (race.player_one === player_id) {
                                left = race.score_one || 0
                                right = race.score_two || 0
                            } else {
                                left = race.score_two || 0
                                right = race.score_one || 0
                            }
                            arr.push(`${left}:${right}`)
                        }
                    }
                    scoreList.push(arr)
                }
                arr.push({ team_id, team_name, playerList, scoreList })
            }
            // 排序
            if (this.properties.ranking) {
                const afterSortArr = [];
                for (const rank of this.properties.ranking) {
                    const { team_id } = rank
                    const r = arr.find(f => f.team_id === team_id)
                    if (r) afterSortArr.push(r)
                }
                this.setData({ raceList: afterSortArr })
            } else this.setData({ raceList: arr })
        }
    }
})
