<?php

// Daten abrufen und deserialisieren
$url     = 'https://www.schachbund.de/php/dewis/verein.php?zps=25318&format=array';
$data    = @file_get_contents($url);
$players = $data ? @unserialize($data) : [];

function safe($val)
{
    return $val === null || $val === false || $val === '' || $val === 'N'
        ? '-' : htmlspecialchars($val);
}

// Nach DWZ absteigend sortieren
if ($players && is_array($players)) {
    usort($players, function ($a, $b) {
        $dwzA = isset($a['dwz']) && is_numeric($a['dwz']) ? (int)$a['dwz']
            : 0;
        $dwzB = isset($b['dwz']) && is_numeric($b['dwz']) ? (int)$b['dwz']
            : 0;

        return $dwzB <=> $dwzA;
    });
}
?>

<section class="py-24 bg-sf_grau-50">
    <div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
        <table class="min-w-full border border-sf_blau-200 rounded-xl overflow-hidden">
            <thead class="bg-sf_blau-600 text-white font-bold">
            <tr>
                <th class="px-3 py-2">Platz</th>
                <th class="px-3 py-2">Mitgl.</th>
                <th class="px-3 py-2">Titel</th>
                <th class="px-3 py-2">Name</th>
                <th class="px-3 py-2">DWZ</th>
                <th class="px-3 py-2">ELO</th>
            </tr>
            </thead>
            <tbody>
            <?php
            if ($players && is_array($players)): ?>
                <?php
                $platz = 1;
                foreach ($players as $p): ?>
                    <tr class="even:bg-sf_grau-100 odd:bg-white">
                        <td class="px-3 py-2 text-center"><?php
                            echo $platz++; ?></td>
                        <td class="px-3 py-2 text-center"><?php
                            echo safe($p['mglnr']); ?></td>
                        <td class="px-3 py-2 text-center"><?php
                            echo safe($p['titel']); ?></td>
                        <td class="px-3 py-2 font-semibold text-xl text-sf_blau-500 hover:text-sf_gelb-300">
                            <a href="http://www.schachbund.de/spieler.html?pkz=<?php echo safe($p['id']); ?>" target="_blank">
                            <?php
                            echo safe(
                                $p['nachname'].', '
                                .$p['vorname'],
                            ); ?></td>
                        </a>
                        <td class="px-3 py-2 text-right text-xl"><?php
                            echo safe($p['dwz']); ?>
                            -<?php
                            echo safe($p['dwzindex']); ?></td>
                        <td class="px-3 py-2 text-right text-xl"><?php
                            echo safe($p['fideelo']); ?></td>
                    </tr>
                <?php
                endforeach; ?>
            <?php
            else: ?>
                <tr>
                    <td colspan="6" class="px-3 py-4 text-center text-red-500">
                        Keine Daten verfügbar.
                    </td>
                </tr>
            <?php
            endif; ?>
            </tbody>
        </table>
    </div>
</section>