{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1">Mój profil</h4>
            <p class="text-muted mb-0">Informacje o Twoim koncie</p>
        </div>
        <a href="{$baseUrl}/profile/edit" class="btn btn-primary">
            <i class="bi bi-pencil me-1"></i>Edytuj profil
        </a>
    </div>

    <div class="row g-4">
        <!-- Dane profilu -->
        <div class="col-lg-4">
            <div class="card">
                <div class="card-body text-center py-4">
                    <div class="mb-3">
                        {if $profile.avatar}
                        <img src="/uploads/avatars/{$profile.avatar}" class="rounded-circle" style="width: 120px; height: 120px; object-fit: cover;">
                        {else}
                        <div class="rounded-circle bg-primary bg-opacity-10 d-inline-flex align-items-center justify-content-center" style="width: 120px; height: 120px;">
                            <i class="bi bi-person text-primary" style="font-size: 4rem;"></i>
                        </div>
                        {/if}
                    </div>
                    <h4 class="mb-1">{$currentUser.username}</h4>
                    <p class="text-muted mb-2">
                        {if $isAdmin}Administrator
                        {elseif $isReseller}Reseller
                        {else}Użytkownik{/if}
                    </p>
                    {if $profile.full_name}
                    <p class="mb-0">{$profile.full_name}</p>
                    {/if}
                </div>
            </div>

            <!-- Dane kontaktowe -->
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="card-title mb-0"><i class="bi bi-person-lines-fill me-2"></i>Dane kontaktowe</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <small class="text-muted">Email</small>
                        <p class="mb-0">{$profile.email|default:'<span class="text-muted">Nie podano</span>'}</p>
                    </div>
                    <div class="mb-3">
                        <small class="text-muted">Telefon</small>
                        <p class="mb-0">{$profile.phone|default:'<span class="text-muted">Nie podano</span>'}</p>
                    </div>
                    <div class="mb-0">
                        <small class="text-muted">Firma</small>
                        <p class="mb-0">{$profile.company|default:'<span class="text-muted">Nie podano</span>'}</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Statystyki i informacje -->
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0"><i class="bi bi-graph-up me-2"></i>Statystyki konta</h5>
                </div>
                <div class="card-body">
                    <div class="row g-4">
                        <div class="col-md-4">
                            <div class="text-center p-3 bg-primary bg-opacity-10 rounded">
                                <h3 class="text-primary mb-1">{$currentUser.devices_count|default:0}</h3>
                                <small class="text-muted">Urządzeń</small>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center p-3 bg-success bg-opacity-10 rounded">
                                <h3 class="text-success mb-1">{$currentUser.login_count|default:0}</h3>
                                <small class="text-muted">Logowań</small>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center p-3 bg-info bg-opacity-10 rounded">
                                <h3 class="text-info mb-1">{$currentUser.api_calls_count|default:0}</h3>
                                <small class="text-muted">Wywołań API</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Informacje o koncie -->
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="card-title mb-0"><i class="bi bi-info-circle me-2"></i>Informacje o koncie</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <p><strong>Data utworzenia:</strong><br>{$currentUser.created_at|date_format:"%d.%m.%Y %H:%M"}</p>
                            <p><strong>Ostatnie logowanie:</strong><br>
                                {if $currentUser.last_login}
                                {$currentUser.last_login|date_format:"%d.%m.%Y %H:%M"}
                                {else}
                                <span class="text-muted">Brak</span>
                                {/if}
                            </p>
                        </div>
                        <div class="col-md-6">
                            <p><strong>Strefa czasowa:</strong><br>{$profile.timezone|default:'Europe/Warsaw'}</p>
                            <p><strong>Język:</strong><br>{if $profile.language == 'pl'}Polski{else}English{/if}</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Ostatnie logowania -->
            <div class="card mt-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0"><i class="bi bi-clock-history me-2"></i>Ostatnie logowania</h5>
                    <a href="{$baseUrl}/profile/login-history" class="btn btn-sm btn-outline-secondary">Zobacz wszystkie</a>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>IP</th>
                                    <th>Przeglądarka</th>
                                    <th>Status</th>
                                    <th>Data</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $loginHistory as $login}
                                <tr>
                                    <td><code>{$login.ip_address}</code></td>
                                    <td>{$login.browser|default:'-'} / {$login.os|default:'-'}</td>
                                    <td>
                                        {if $login.success}
                                        <span class="badge bg-success">OK</span>
                                        {else}
                                        <span class="badge bg-danger">Błąd</span>
                                        {/if}
                                    </td>
                                    <td>{$login.created_at|date_format:"%d.%m.%Y %H:%M"}</td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="4" class="text-center py-3 text-muted">Brak historii logowań</td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
