<?php

function renderLiveTableHeaderLiveRate()
{
    // Mảng các công ty cá cược
    $betCompanies = [
        ['data-bet' => '100', 'label' => 'K - Sport'],
        ['data-bet' => '1', 'label' => 'Macauslot'],
        ['data-bet' => '3', 'label' => 'Crown'],
        ['data-bet' => '4', 'label' => 'Ladbrokes'],
        ['data-bet' => '8', 'label' => 'Bet365'],
        ['data-bet' => '12', 'label' => 'Easybet'],
        ['data-bet' => '23', 'label' => '188bet'],
        ['data-bet' => '24', 'label' => '12bet'],
        ['data-bet' => '31', 'label' => 'Sbobet'],
        ['data-bet' => '42', 'label' => '18Bet']
    ];

    ob_start();
    ?>

    <tr id="tr_upper-live-title" class="upper-live-title">
        <td class="">
            <span class="upper-live-icon-football"></span>
        </td>
        <td class="upper-live-title-football rs td-bdr" colspan="6">
            <span><?php esc_html_e('Tất cả trận đang live', 'sport') ?></span>
        </td>
        <td colspan="3" class="change-colname hpd-td td-bdr">
            <span>HDP</span>
        </td>
        <td colspan="3" class="td-tx change-colname tx-td td-bdr">
            <span><?php esc_html_e('Tài xỉu', 'sport') ?></span>
        </td>
        <td id="oddsHead" colspan="1" class="oddsHead h2 inclfuded-ht-title table-1"></td>
    </tr>
    <?php
    return ob_get_clean();
}

function renderHeaderLiveTableTitleRate()
{
    ob_start();
    ?>
    <tr id="tr_upper-live-titlez" class="upper-live-titlez">
        <td class="td-bdr w-32">
            <span class="allno trash-live-title remove-league-icon"></span>
        </td>
        <td class="td-bdr w-70 py-1 status-time-sched"><?php esc_html_e('Thời gian', 'sport') ?></td>
        <td class="td-bdr w-70 py-1 status-time-sched"></td>
        <td class="td-bdr w-120"><?php esc_html_e('Đội nhà', 'sport') ?></td>
        <td class="td-bdr w-70"><?php esc_html_e('Tỉ số', 'sport') ?></td>
        <td class="td-tx td-bdr w-33" style="white-space: nowrap;">
            <img src="<?= IMGS3_PLUGINS ?>/assets/images/icon-golf-outline.svg" class="corner-icon light"
                 alt="corner-icon" style="margin: auto;">
            <img src="<?= IMGS3_PLUGINS ?>/assets/images/icon-golf-outline_dark.svg" class="corner-icon dark"
                 alt="corner-icon" style="margin: auto;">
        </td>
        <td class="td-bdr w-120"><?php esc_html_e('Đội khách', 'sport') ?></td>
        <td class="w-47"><?php esc_html_e('Chủ', 'sport') ?></td>
        <td class="w-47"><?php esc_html_e('Tỷ lệ', 'sport') ?></td>
        <td class="td-bdr w-47">
            <span><?php esc_html_e('Khách', 'sport') ?></span>
        </td>
        <td class="w-47"><?php esc_html_e('Chủ', 'sport') ?></td>
        <td class="w-47"><?php esc_html_e('Tỷ lệ', 'sport') ?></td>
        <td class="td-bdr w-47">
            <span><?php esc_html_e('Khách', 'sport') ?></span>
        </td>
        <td id="oddsHead" class="oddsHead w-75">
            <div style="display: flex; padding: 8px; justify-content: center; gap: 12px;">
                <div class=""><?php esc_html_e('Trực tiếp', 'sport') ?></div>
            </div>
        </td>
    </tr>
    <?php
    return ob_get_clean();
}

function fetchdataRate($number_match, $filterL = [])
{
    $url_api_rate = DB_DOMAIN . '/gf/data/bf_vn_nt_live.js';
    $leagues_rate = bongdako_fetch_and_merge_data_faked($url_api_rate);

    if (isset($leagues_rate) && is_array($leagues_rate)) {
        $betMatchCount = 0;
        $maxBetCount = $number_match;
        $isFirstLeague = true; // Flag to check the first league

        foreach ($leagues_rate as $league_item) {
            $league_id = isset($league_item['league_id']) ? $league_item['league_id'] : '';
            if ($filterL && !in_array($league_id, $filterL)) {
                continue;
            }
            $league_name = isset($league_item['leagueName']) ? $league_item['leagueName'] : '';
            $league_flag_url = IMGS3 . $league_item['leagueFlag'];
            $dataMatch = $league_item['matches'];

            // Check if it's the first league and if it has only one match
            if ($isFirstLeague && count($dataMatch) <= 1) {
                $isFirstLeague = false; // Mark the first league as processed
                continue; // Skip the first league if it has only one match
            }
            ?>
            <tr id="ln_<?php echo $league_id; ?>"
                class="Leaguestitle fbHead league-title-head-row league-title-head-mobile" name="Leaguestitle">
                <td>
                    <div class="add-div">
                        <i class="icon iconfont add-info-l icon-font-collect-off l0 favL favL_<?php echo $league_id; ?>"></i>
                    </div>
                </td>
                <td class="league-title league-title-ht-title t" colspan="11" style="line-height: 28px;">
                    <div class="l1" style="width: fit-content;">
                        <img class="cImg lazy flag-img" src="<?php echo $league_flag_url; ?>"
                             alt="<?php echo $league_name; ?>" style="display: inline;">
                        <span class="fbsclass football-team-name"><?php echo $league_name; ?></span>
                    </div>
                </td>
                <td class="df-wrapper df-wrapper-league-item df-wrapper-favorite-item">
                    <div class="df1" style="position: relative;">
                        <div class="hidden-info-match">
                            <span>
                                <div class="swt hide-tmp"></div>
                            </span>
                        </div>
                    </div>
                </td>
            </tr>
            <?php

            $skipFirstMatch = $isFirstLeague;

            foreach ($dataMatch as $item) {
                if ($skipFirstMatch) {
                    $skipFirstMatch = false; // Skip this match and reset the flag
                    continue;
                }
                $match_id = isset($item['matchId']) && $item['matchId'] ? $item['matchId'] : 2607038;
                $match_time = isset($item['startTime']) && $item['startTime'] ? $item['startTime'] : '0';
                $match_status = isset($data['status']) && $data['status'] ? $data['status'] : '';
                $home_team_name = isset($item['teams']['home']) && $item['teams']['home'] ? unicode_to_utf8($item['teams']['home']) : 'Cagliari';
                $away_team_name = isset($item['teams']['away']) && $item['teams']['away'] ? unicode_to_utf8($item['teams']['away']) : 'Inter Milan';
                $home_score = isset($item['score']['home']) ? $item['score']['home'] : null;
                $away_score = isset($item['score']['away']) ? $item['score']['away'] : null;
                $home_rank = isset($item['rank']['home']) && $item['rank']['home'] ? $item['rank']['home'] : 18;
                $away_rank = isset($item['rank']['away']) && $item['rank']['away'] ? $item['rank']['away'] : 3;
                $home_red_card = isset($item['red_card']['home']) && $item['red_card']['home'] ? $item['red_card']['home'] : '';
                $away_red_card = isset($item['red_card']['away']) && $item['red_card']['away'] ? $item['red_card']['away'] : '';
                $home_yellow_card = isset($item['yellow_card']['home']) && $item['yellow_card']['home'] ? $item['yellow_card']['home'] : '';
                $away_yellow_card = isset($item['yellow_card']['away']) && $item['yellow_card']['away'] ? $item['yellow_card']['away'] : '';
                $home_corners = isset($item['corner']['home']) ? $item['corner']['home'] : '';
                $away_corners = isset($item['corner']['away']) ? $item['corner']['away'] : '';

                // Format thời gian trận đấu
                $datetime = new DateTime($match_time, new DateTimeZone('UTC')); // Assuming input time is in UTC
                $datetime->setTimezone(new DateTimeZone('Asia/Bangkok')); // Set timezone to GMT+7
                $match_time_formatted = $datetime->format('H:i');

                $odd_home = number_format(rand(1, 20) / 10, 2); // Giá trị "Chủ"
                $odd_draw = number_format(rand(1, 20) / 10, 2); // Giá trị "Hòa"
                $odd_away = number_format(rand(1, 20) / 10, 2); // Giá trị "Khách"
                $odd_over = number_format(rand(1, 20) / 10, 2); // Giá trị "Trên"
                $odd_equal = number_format(rand(1, 20) / 10, 2); // Giá trị "Tỷ lệ"
                $odd_under = number_format(rand(1, 20) / 10, 2); // Giá trị "Dưới"

                $class_home = getRandomColorClass();
                $class_draw = getRandomColorClass();
                $class_away = getRandomColorClass();
                $class_over = getRandomColorClass();
                $class_equal = getRandomColorClass();
                $class_under = getRandomColorClass();

                ?>
                <tr id="tr1_<?php echo $match_id; ?>MatchOpen" class="tds fst new-home-tr" style="position: relative;">
                    <td height="30" width="34">
                        <div class="add-div 11">
                            <i id="favM_<?php echo $match_id; ?>"
                               class="icon iconfont favM add-info icon-font-collect-off"></i>
                        </div>
                    </td>
                    <td name="timeData" class="time handpoint status-time-sched" id="mt_<?php echo $match_id; ?>"
                        width="50">
                        <span id="got_<?php echo $match_id; ?>"><p><?php echo $match_time_formatted; ?></p></span>
                    </td>
                    <td class="" id="mt_live_<?php echo $match_id; ?>"
                        width="50">
                    </td>
                    <td id="ht_<?php echo $match_id; ?>" style="text-align: center; position: relative;">
                        <span class="hdpstar" style="display: inline;"></span>
                        <div class="team-box tleft">
                            <a data-team-name="<?php echo $home_team_name; ?>"><?php echo $home_team_name; ?></a>
                            <p class="team-more-info">
                                <span id="horder_7416"
                                      class="team-hg">[<?php echo $home_rank; ?>]</span><?php if (!empty($home_yellow_card) && $home_yellow_card > 0) : ?>
                                    <span><span class="yellowcard"><?php echo $home_yellow_card; ?></span>
                                    </span><?php endif; ?><?php if (!empty($home_red_card) && $home_red_card > 0) : ?><a
                                        id="redcard2_4469"><span class="redcard"><?php echo $home_red_card; ?></span>
                                    </a><?php endif; ?>
                            </p>
                        </div>
                    </td>
                    <td class="blue handpoint score-color"><?php if ($home_score > 0 || $away_score > 0): ?><b
                                class="scored-s match-not-finished"><?php echo $home_score; ?>
                            -<?php echo $away_score; ?></b><?php else : ?><b
                                class="scored-s no-score">&nbsp;-&nbsp;</b><?php endif; ?></td>
                    <td class="ht-title" id="corner-title_<?php echo $match_id; ?>">
                        <div style="min-width: 33px;">
                            <div class="score-detail pointer">
                                <div class="secont-half text-center w-100-i"
                                     style="color: rgb(255, 128, 0);"><?php echo $home_corners; ?>
                                    -<?php echo $away_corners; ?></div>
                            </div>
                        </div>
                    </td>
                    <td id="gt_<?php echo $match_id; ?>" style="text-align: center; position: relative;">
                        <div class="team-box">
                            <a data-team-name="<?php echo $away_team_name; ?>"><?php echo $away_team_name; ?></a>
                            <p class="team-more-info">
                                <?php if (!empty($away_red_card) && $away_red_card > 0) : ?><a id="redcard2_4469"><span
                                            class="redcard"><?php echo $away_red_card; ?></span>
                                    </a><?php endif; ?><?php if (!empty($away_yellow_card) && $away_yellow_card > 0) : ?>
                                    <span><span class="yellowcard"><?php echo $away_yellow_card; ?></span>
                                    </span><?php endif; ?><span id="horder_7416"
                                                                class="team-hg">[<?php echo $away_rank; ?>]</span>
                            </p>
                        </div>
                    </td>
                    <td class="oddstd c6" width="50"><p
                                class="<?php echo $class_home; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_home; ?></p>
                    </td>
                    <td class="oddstd c7" width="50"><p
                                class="<?php echo $class_draw; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_draw; ?></p>
                    </td>
                    <td class="oddstd c8" width="50"><p
                                class="<?php echo $class_away; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_away; ?></p>
                    </td>
                    <td class="oddstd td-tx c9" width="50"><p
                                class="<?php echo $class_over; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_over; ?></p>
                    </td>
                    <td class="oddstd td-tx c10" width="50"><p
                                class="<?php echo $class_equal; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_equal; ?></p>
                    </td>
                    <td class="oddstd td-tx c11" width="50"><p
                                class="<?php echo $class_under; ?> odds1 odds-hdp odds-bg odds-normal"><?php echo $odd_under; ?></p>
                    </td>
                    <td colspan="1"
                        class="be-change-ht-title hello toolimg 0-0 action-table action-table-pc d-none d-lg-flex">
                        <div class="btn-live" id="flashSpan_2618967" style="position: relative;">
                            <img src="<?= IMGS3_PLUGINS ?>/images/football-vn/btn-live-v2.svg" alt="btn-live-v2.svg">
                        </div>
                    </td>
                    <td colspan="1"
                        class="be-change-ht-title toolimg 0-0 action-table league-detail-mobile d-flex d-lg-none">
                        <span>Xem chi tiết</span>
                        <div class="btn-live btn-live-mobile" id="flashSpan_2618967">
                            <img src="<?= IMGS3_PLUGINS ?>/images/football-vn/play-button1.svg" alt="btn-live-v2.svg">
                        </div>
                    </td>
                </tr>
                <?php
                $isFirstLeague = false;
                $betMatchCount++;
                if ($betMatchCount == $maxBetCount) {
                    break;
                }
            }
            $isFirstLeague = false; // Mark the first league as processed
            if ($betMatchCount == $maxBetCount) {
                break;
            }
        }
    }
}

function renderMatchItemRate($data)
{
    
    $league_id = isset($data['league_id']) && $data['league_id'] ? $data['league_id'] : '5';
    $league_name = isset($data['league']) && $data['league'] ? unicode_to_utf8($data['league']) : 'VĐQG Ý';
    $league_flag_url = IMGS3 . $data['flag'];
    $match_id = isset($data['matchId']) && $data['matchId'] ? $data['matchId'] : 2607038;
    $match_time = isset($data['startTime']) && $data['startTime'] ? $data['startTime'] : '2024-12-28 17:00:00';
    $home_team_name = isset($data['teams']['home']) && $data['teams']['home'] ? unicode_to_utf8($data['teams']['home']) : 'Cagliari';
    $away_team_name = isset($data['teams']['away']) && $data['teams']['away'] ? unicode_to_utf8($data['teams']['away']) : 'Inter Milan';
    $home_score = isset($data['score']['home']) ? $data['score']['home'] : '';
    $away_score = isset($data['score']['away']) ? $data['score']['away'] : '';
    $home_rank = isset($data['rank']['home']) && $data['rank']['home'] ? $data['rank']['home'] : 18;
    $away_rank = isset($data['rank']['away']) && $data['rank']['away'] ? $data['rank']['away'] : 3;
    $home_red_card = isset($data['red_card']['home']) && $data['red_card']['home'] ? $data['red_card']['home'] : '';
    $away_red_card = isset($data['red_card']['away']) && $data['red_card']['away'] ? $data['red_card']['away'] : '';
    $home_yellow_card = isset($data['yellow_card']['home']) && $data['yellow_card']['home'] ? $data['yellow_card']['home'] : '';
    $away_yellow_card = isset($data['yellow_card']['away']) && $data['yellow_card']['away'] ? $data['yellow_card']['away'] : '';
    $home_corners = isset($data['corner']['home']) ? $data['corner']['home'] : 5;
    $away_corners = isset($data['corner']['away']) ? $data['corner']['away'] : 7;

    // Format thời gian trận đấu
    $match_time_formatted = date('H:i', strtotime($match_time));
    $odd_home = number_format(rand(1, 20) / 10, 2); // Giá trị "Chủ"
    $odd_draw = number_format(rand(1, 20) / 10, 2); // Giá trị "Hòa"
    $odd_away = number_format(rand(1, 20) / 10, 2); // Giá trị "Khách"
    $odd_over = number_format(rand(1, 20) / 10, 2); // Giá trị "Trên"
    $odd_equal = number_format(rand(1, 20) / 10, 2); // Giá trị "Tỷ lệ"
    $odd_under = number_format(rand(1, 20) / 10, 2); // Giá trị "Dưới"

    $class_home = getRandomColorClass();
    $class_draw = getRandomColorClass();
    $class_away = getRandomColorClass();
    $class_over = getRandomColorClass();
    $class_equal = getRandomColorClass();
    $class_under = getRandomColorClass();

    return '<tr id="ln_' . $league_id . '" class="Leaguestitle fbHead league-title-head-row league-title-head-mobile" name="Leaguestitle">
                <td>
                    <div class="add-div">
                        <i class="icon iconfont add-info-l icon-font-collect-off l0 favL favL_' . $league_id . '"></i>
                    </div>
                </td>
                <td class="league-title league-title-ht-title t" colspan="11" style="line-height: 28px;">
                    <div class="l1" style="width: fit-content;">
                        <img class="cImg lazy flag-img" src="' . $league_flag_url . '" alt="' . $league_name . '" style="display: inline;">
                        <span class="fbsclass football-team-name">' . $league_name . '</span>
                    </div>
                </td>
                <td class="df-wrapper df-wrapper-league-item df-wrapper-favorite-item">
                    <div class="df1" style="position: relative;">
                        <div class="hidden-info-match">
                            <span>
                                <div class="swt hide-tmp"></div>
                            </span>
                        </div>
                    </div>
                </td>
            </tr>

            <tr id="tr1_' . $match_id . 'MatchOpen" class="tds fst new-home-tr" style="position: relative;">
                <td height="30" width="34">
                    <div class="add-div 11">
                        <i id="favM_' . $match_id . '" class="icon iconfont favM add-info icon-font-collect-off"></i>
                    </div>
                </td>
                <td name="timeData" class="time handpoint status-time-sched" id="mt_' . $match_id . '" width="50">
                    <span id="got_' . $match_id . '"><p>' . $match_time_formatted . '</p></span>
                </td>
                <td id="ht_' . $match_id . '" style="text-align: center; position: relative;">
                    <span class="hdpstar" style="display: inline;"></span>
                    <div class="team-box tleft">
                        <a data-team-name="' . $home_team_name . '">' . $home_team_name . '</a>
                        <p class="team-more-info">
                            <span id="horder_7416" class="team-hg">[' . $home_rank . ']</span><span id="yellow1_7416"><span class="yellowcard">' . $home_yellow_card . '</span></span>' . ((!empty($home_red_card) && $home_red_card > 0) ? '<a id="redcard2_4469"><span class="redcard">' . $home_red_card . '</span></a>' : '') . '
                        </p>
                    </div>
                </td>
                <td class="blue handpoint score-color">' . (($home_score > 0 || $away_score > 0) ? '<b class="scored-s match-not-finished">' . $home_score . '-' . $away_score . '</b>' : '<b class="scored-s no-score">&nbsp;-&nbsp;</b>') . '</td>
                <td class="ht-title" id="corner-title_' . $match_id . '">
                    <div style="min-width: 33px;">
                        <div class="score-detail pointer">
                            <div class="secont-half text-center w-100-i" style="color: rgb(255, 128, 0);">' . $home_corners . '-' . $away_corners . '</div>
                        </div>
                    </div>
                </td>
                <td id="gt_' . $match_id . '" style="text-align: center; position: relative;">
                    <div class="team-box">
                        <a data-team-name="' . $away_team_name . '">' . $away_team_name . '</a>
                        <p class="team-more-info">
                            ' . ((!empty($away_red_card) && $away_red_card > 0) ? '<a id="redcard2_4469"><span class="redcard">' . $away_red_card . '</span></a>' : '') . '<span id="yellow1_7416"><span class="yellowcard">' . $away_yellow_card . '</span></span><span id="horder_7416" class="team-hg">[' . $away_rank . ']</span>
                        </p>
                    </div>
                </td>
                <td class="oddstd c6" width="50"><p class="' . $class_home . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_home . '</p></td>
                <td class="oddstd c7" width="50"><p class="' . $class_draw . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_draw . '</p></td>
                <td class="oddstd c8" width="50"><p class="' . $class_away . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_away . '</p></td>
                <td class="oddstd td-tx c9" width="50"><p class="' . $class_over . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_over . '</p></td>
                <td class="oddstd td-tx c10" width="50"><p class="' . $class_equal . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_equal . '</p></td>
                <td class="oddstd td-tx c11" width="50"><p class="' . $class_under . ' odds1 odds-hdp odds-bg odds-normal">' . $odd_under . '</p></td>
                <td colspan="1" class="be-change-ht-title hello toolimg 0-0 action-table  action-table-pc d-none d-lg-flex">
                    <div class="btn-live" id="flashSpan_2618967" style="position: relative;">
                        <img src="'.IMGS3_PLUGINS.'/images/football-vn/btn-live-v2.svg" alt="btn-live-v2.svg">
                    </div>
                </td>
                <td colspan="1" class="be-change-ht-title toolimg 0-0 action-table league-detail-mobile d-flex d-lg-none">
                    <span>' . esc_html('Xem chi tiết', 'sport') . '</span>
                    <div class="btn-live btn-live-mobile" id="flashSpan_2618967">
                        <img src="'.IMGS3_PLUGINS.'/images/football-vn/play-button1.svg" alt="btn-live-v2.svg">
                    </div>
                </td>
            </tr>';
}