{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    {if $isAdmin}
    {* ============= DASHBOARD ADMINA ============= *}
    <div class="row g-4 mb-4">
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-primary">
                <div class="stat-icon">
                    <i class="bi bi-people"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$userStats.total}</div>
                    <div class="stat-label">Użytkownicy</div>
                </div>
                <div class="stat-footer">
                    <span><i class="bi bi-person-check me-1"></i>{$userStats.users} userów, {$userStats.resellers} resellerów</span>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-success">
                <div class="stat-icon">
                    <i class="bi bi-tv"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$deviceStats.total}</div>
                    <div class="stat-label">Urządzenia</div>
                </div>
                <div class="stat-footer">
                    <span class="text-warning"><i class="bi bi-shield-x me-1"></i>{$deviceStats.blocked} zablokowanych</span>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-warning">
                <div class="stat-icon">
                    <i class="bi bi-list-ul"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$playlistStats.total}</div>
                    <div class="stat-label">Playlisty</div>
                </div>
                <div class="stat-footer">
                    <span>
                        {$playlistStats.by_type.xtream} Xtr | {$playlistStats.by_type.m3u} M3U | {$playlistStats.by_type.stalker} Stk
                    </span>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-info">
                <div class="stat-icon">
                    <i class="bi bi-person-check"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$userStats.today_logins}</div>
                    <div class="stat-label">Logowań dziś</div>
                </div>
                <div class="stat-footer">
                    <span><i class="bi bi-shield-check me-1"></i>{$userStats.admins} administratorów</span>
                </div>
            </div>
        </div>
    </div>

    {* Resellerzy - przegląd *}
    {if $resellerStats && count($resellerStats) > 0}
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title mb-0"><i class="bi bi-shop me-2"></i>Resellerzy - wykorzystanie limitów</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Reseller</th>
                            <th>Użytkownicy</th>
                            <th>Urządzenia</th>
                            <th>Playlisty</th>
                            <th>Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $resellerStats as $reseller}
                        <tr>
                            <td><strong>{$reseller.username}</strong></td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="progress flex-grow-1 me-2" style="height: 6px; width: 80px;">
                                        <div class="progress-bar bg-primary" style="width: {if $reseller.max_users > 0}{($reseller.users_count / $reseller.max_users * 100)|round}{else}0{/if}%"></div>
                                    </div>
                                    <span class="small">{$reseller.users_count}/{$reseller.max_users}</span>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="progress flex-grow-1 me-2" style="height: 6px; width: 80px;">
                                        <div class="progress-bar bg-success" style="width: {if $reseller.max_devices > 0}{($reseller.devices_used / $reseller.max_devices * 100)|round}{else}0{/if}%"></div>
                                    </div>
                                    <span class="small" title="własne: {$reseller.devices_own}, przydzielone: {$reseller.devices_allocated}">{$reseller.devices_used}/{$reseller.max_devices}</span>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="progress flex-grow-1 me-2" style="height: 6px; width: 80px;">
                                        <div class="progress-bar bg-info" style="width: {if $reseller.max_playlists > 0}{($reseller.playlists_used / $reseller.max_playlists * 100)|round}{else}0{/if}%"></div>
                                    </div>
                                    <span class="small" title="własne: {$reseller.playlists_own}, przydzielone: {$reseller.playlists_allocated}">{$reseller.playlists_used}/{$reseller.max_playlists}</span>
                                </div>
                            </td>
                            <td>
                                <a href="{$baseUrl}/users/edit?id={$reseller.id}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-pencil"></i>
                                </a>
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {/if}

    <div class="row g-4">
        <!-- Recent Users -->
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-people me-2"></i>Ostatni użytkownicy
                    </h5>
                    <a href="{$baseUrl}/users" class="btn btn-sm btn-outline-primary">Zobacz wszystkich</a>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>Użytkownik</th>
                                    <th>Rola</th>
                                    <th>Urządzenia</th>
                                    <th>Ostatnie logowanie</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $recentUsers as $user}
                                <tr>
                                    <td>
                                        <strong>{$user.username}</strong>
                                        {if !$user.is_active}<span class="badge bg-danger ms-1">Nieaktywny</span>{/if}
                                    </td>
                                    <td>
                                        {if $user.role == 'admin'}
                                        <span class="badge bg-danger">Admin</span>
                                        {elseif $user.role == 'reseller'}
                                        <span class="badge bg-primary">Reseller</span>
                                        {else}
                                        <span class="badge bg-secondary">User</span>
                                        {/if}
                                    </td>
                                    <td>
                                        {if $user.role == 'admin'}
                                        <span class="text-muted">∞</span>
                                        {elseif $user.role == 'reseller'}
                                        <span class="text-info">{$user.devices_count + $user.devices_allocated}</span><span class="text-muted">/{$user.max_devices}</span>
                                        {else}
                                        {$user.devices_count}/{$user.device_limit}
                                        {/if}
                                    </td>
                                    <td>
                                        {if $user.last_login}
                                        {$user.last_login|date_format:"%d.%m.%Y %H:%M"}
                                        {else}
                                        <span class="text-muted">Nigdy</span>
                                        {/if}
                                    </td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">Brak użytkowników</td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Recent Logs -->
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-journal-text me-2"></i>Ostatnie logi
                    </h5>
                    <a href="{$baseUrl}/admin/logs" class="btn btn-sm btn-outline-primary">Zobacz wszystkie</a>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>Akcja</th>
                                    <th>Użytkownik</th>
                                    <th>Data</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $recentLogs as $log}
                                <tr>
                                    <td>
                                        <span class="badge bg-{$log.badge_class|default:'info'}">
                                            {$log.action}
                                        </span>
                                    </td>
                                    <td>{$log.username|default:'System'}</td>
                                    <td>{$log.created_at|date_format:"%d.%m %H:%M"}</td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="3" class="text-center text-muted py-4">Brak logów</td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {elseif $isReseller}
    {* ============= DASHBOARD RESELLERA ============= *}
    
    {* Kafelki z limitami *}
    <div class="row g-4 mb-4">
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-primary">
                <div class="stat-icon">
                    <i class="bi bi-people"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$resellerUsage.users.used}/{$resellerUsage.users.max}</div>
                    <div class="stat-label">Moi użytkownicy</div>
                </div>
                <div class="stat-footer">
                    <a href="{$baseUrl}/users" class="text-primary">
                        <i class="bi bi-arrow-right me-1"></i>Zarządzaj
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-success">
                <div class="stat-icon">
                    <i class="bi bi-tv"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$resellerUsage.devices.used}/{$resellerUsage.devices.max}</div>
                    <div class="stat-label">Urządzenia (pula)</div>
                </div>
                <div class="stat-footer">
                    <span class="small">własne: {$resellerUsage.devices.own}, rozdane: {$resellerUsage.devices.allocated}</span>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-warning">
                <div class="stat-icon">
                    <i class="bi bi-list-ul"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$resellerUsage.playlists.used}/{$resellerUsage.playlists.max}</div>
                    <div class="stat-label">Playlisty (pula)</div>
                </div>
                <div class="stat-footer">
                    <span class="small">własne: {$resellerUsage.playlists.own}, rozdane: {$resellerUsage.playlists.allocated}</span>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-3">
            <div class="stat-card stat-card-info">
                <div class="stat-icon">
                    <i class="bi bi-hdd-stack"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$totalManagedDevices}</div>
                    <div class="stat-label">Urządzeń w systemie</div>
                </div>
                <div class="stat-footer">
                    <span class="small">{$totalManagedPlaylists} playlist łącznie</span>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        {* Moi użytkownicy *}
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-people me-2"></i>Moi użytkownicy
                    </h5>
                    <a href="{$baseUrl}/users" class="btn btn-sm btn-outline-primary">Zobacz wszystkich</a>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>Użytkownik</th>
                                    <th>Urządzenia</th>
                                    <th>Playlisty</th>
                                    <th>Ostatnie logowanie</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $myUsers as $user}
                                <tr>
                                    <td><strong>{$user.username}</strong></td>
                                    <td>{$user.devices_count}/{$user.device_limit}</td>
                                    <td>{$user.playlists_count|default:0}/{$user.playlist_limit}</td>
                                    <td>
                                        {if $user.last_login}
                                        {$user.last_login|date_format:"%d.%m %H:%M"}
                                        {else}
                                        <span class="text-muted">Nigdy</span>
                                        {/if}
                                    </td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="4" class="text-center py-4">
                                        <p class="text-muted mb-2">Nie masz jeszcze użytkowników</p>
                                        <a href="{$baseUrl}/users/add" class="btn btn-sm btn-primary">
                                            <i class="bi bi-plus-lg me-1"></i>Dodaj użytkownika
                                        </a>
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        {* Moje urządzenia *}
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-tv me-2"></i>Moje urządzenia
                    </h5>
                    <a href="{$baseUrl}/devices" class="btn btn-sm btn-outline-primary">Zobacz wszystkie</a>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>Nazwa</th>
                                    <th>MAC</th>
                                    <th>Playlisty</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $myDevices as $device}
                                <tr>
                                    <td><strong>{$device.name|default:'Bez nazwy'}</strong></td>
                                    <td><code class="small">{$device.mac}</code></td>
                                    <td><span class="badge bg-info">{$device.playlists_count}</span></td>
                                    <td>
                                        {if $device.is_blocked}
                                        <span class="badge bg-danger">Zablokowane</span>
                                        {else}
                                        <span class="badge bg-success">Aktywne</span>
                                        {/if}
                                    </td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="4" class="text-center py-4">
                                        <p class="text-muted mb-2">Brak własnych urządzeń</p>
                                        {if $resellerUsage.devices.available_for_self > 0}
                                        <a href="{$baseUrl}/devices/add" class="btn btn-sm btn-primary">
                                            <i class="bi bi-plus-lg me-1"></i>Dodaj urządzenie
                                        </a>
                                        {/if}
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {else}
    {* ============= DASHBOARD UŻYTKOWNIKA ============= *}
    <div class="row g-4 mb-4">
        <div class="col-sm-6 col-xl-4">
            <div class="stat-card stat-card-primary">
                <div class="stat-icon">
                    <i class="bi bi-tv"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$myDevicesCount}/{$deviceLimit}</div>
                    <div class="stat-label">Moje urządzenia</div>
                </div>
                <div class="stat-footer">
                    <a href="{$baseUrl}/devices" class="text-primary">
                        <i class="bi bi-arrow-right me-1"></i>Zarządzaj urządzeniami
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-sm-6 col-xl-4">
            <div class="stat-card stat-card-success">
                <div class="stat-icon">
                    <i class="bi bi-list-ul"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$myPlaylistsCount}/{$playlistLimit}</div>
                    <div class="stat-label">Moje playlisty</div>
                </div>
                <div class="stat-footer">
                    <a href="{$baseUrl}/playlists" class="text-success">
                        <i class="bi bi-arrow-right me-1"></i>Zarządzaj playlistami
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-sm-12 col-xl-4">
            <div class="stat-card stat-card-info">
                <div class="stat-icon">
                    <i class="bi bi-person-badge"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$currentUser.username}</div>
                    <div class="stat-label">Twoje konto</div>
                </div>
                <div class="stat-footer">
                    <span>
                        <i class="bi bi-calendar3 me-1"></i>
                        Utworzone: {$currentUser.created_at|date_format:"%d.%m.%Y"}
                    </span>
                </div>
            </div>
        </div>
    </div>

    {* Moje urządzenia *}
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="card-title mb-0">
                <i class="bi bi-tv me-2"></i>Moje urządzenia
            </h5>
            <div>
                {if $myDevicesCount < $deviceLimit}
                <a href="{$baseUrl}/devices/add" class="btn btn-sm btn-primary">
                    <i class="bi bi-plus-lg me-1"></i>Dodaj urządzenie
                </a>
                {/if}
                <a href="{$baseUrl}/devices" class="btn btn-sm btn-outline-secondary ms-1">
                    Zobacz wszystkie
                </a>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Nazwa</th>
                            <th>MAC</th>
                            <th>Kod</th>
                            <th>Playlisty</th>
                            <th>Status</th>
                            <th>Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $myDevices as $device}
                        <tr>
                            <td><strong>{$device.name|default:'Bez nazwy'}</strong></td>
                            <td><code>{$device.mac}</code></td>
                            <td><code>{$device.code}</code></td>
                            <td>
                                <a href="{$baseUrl}/playlists?device_id={$device.id}" class="badge bg-info text-decoration-none">
                                    {$device.playlists_count} playlist
                                </a>
                            </td>
                            <td>
                                {if $device.is_blocked}
                                <span class="badge bg-danger">Zablokowane</span>
                                {else}
                                <span class="badge bg-success">Aktywne</span>
                                {/if}
                            </td>
                            <td>
                                <a href="{$baseUrl}/playlists/add?device_id={$device.id}" class="btn btn-sm btn-outline-success" title="Dodaj playlistę">
                                    <i class="bi bi-plus-lg"></i>
                                </a>
                                <a href="{$baseUrl}/devices/edit?id={$device.id}" class="btn btn-sm btn-outline-primary" title="Edytuj">
                                    <i class="bi bi-pencil"></i>
                                </a>
                            </td>
                        </tr>
                        {foreachelse}
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="text-muted mb-3">
                                    <i class="bi bi-tv" style="font-size: 3rem;"></i>
                                </div>
                                <p class="mb-3">Nie masz jeszcze żadnych urządzeń</p>
                                <a href="{$baseUrl}/devices/add" class="btn btn-primary">
                                    <i class="bi bi-plus-lg me-1"></i>Dodaj pierwsze urządzenie
                                </a>
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {* Ostatnie playlisty *}
    {if $recentPlaylists && count($recentPlaylists) > 0}
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="card-title mb-0">
                <i class="bi bi-list-ul me-2"></i>Ostatnie playlisty
            </h5>
            <a href="{$baseUrl}/playlists" class="btn btn-sm btn-outline-primary">Zobacz wszystkie</a>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Nazwa</th>
                            <th>Typ</th>
                            <th>Urządzenie</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $recentPlaylists as $playlist}
                        <tr>
                            <td><strong>{$playlist.name}</strong></td>
                            <td>
                                {if $playlist.type == 'xtream'}
                                <span class="badge bg-primary">Xtream</span>
                                {elseif $playlist.type == 'm3u'}
                                <span class="badge bg-success">M3U</span>
                                {else}
                                <span class="badge bg-warning">Stalker</span>
                                {/if}
                            </td>
                            <td><code class="small">{$playlist.device_name|default:$playlist.mac}</code></td>
                            <td>
                                {if $playlist.is_active}
                                <span class="badge bg-success">Aktywna</span>
                                {else}
                                <span class="badge bg-secondary">Nieaktywna</span>
                                {/if}
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {/if}
    {/if}
</div>
{/block}
