<?php include __DIR__ . '/../layout/header.php'; ?>

<?php
$usrSearch = $search ?? '';
$usrSort   = $sort   ?? '';
$usrOrder  = $order  ?? 'asc';
function usrSortUrl($f, $cs, $co, $s) {
    $o = ($cs === $f && $co === 'asc') ? 'desc' : 'asc';
    return '?sort='.$f.'&order='.$o.($s !== '' ? '&search='.urlencode($s) : '');
}
function usrSortIcon($f, $cs, $co) {
    if ($cs !== $f) return '<i class="fa fa-sort" style="opacity:.3;font-size:10px;"></i>';
    return $co === 'asc'
        ? '<i class="fa fa-sort-asc" style="color:var(--primary);font-size:11px;"></i>'
        : '<i class="fa fa-sort-desc" style="color:var(--primary);font-size:11px;"></i>';
}
?>

<div class="page-toolbar">
    <form method="GET" action="<?php echo $baseUrl; ?>/admin/users" id="user-search-form" class="search-bar" style="max-width:440px;">
        <i class="fa fa-search search-icon"></i>
        <input type="text" name="search" id="user-search-input"
               value="<?php echo htmlspecialchars($usrSearch); ?>"
               placeholder="Ad veya e-posta ara..."
               autocomplete="off">
        <?php if ($usrSearch !== ''): ?>
        <a href="<?php echo $baseUrl; ?>/admin/users" class="search-clear-btn"><i class="fa fa-times"></i></a>
        <?php endif; ?>
    </form>
</div>

<div class="admin-card" style="padding:0;overflow:hidden;">

    <?php if (!empty($users)): ?>

    <!-- Desktop Table -->
    <div class="admin-table-wrap desktop-only" style="border:none;border-radius:0;">
        <table class="admin-table">
            <thead>
                <tr>
                    <th><a href="<?php echo usrSortUrl('name', $usrSort, $usrOrder, $usrSearch); ?>" style="color:inherit;text-decoration:none;display:inline-flex;align-items:center;gap:5px;">Ad Soyad <?php echo usrSortIcon('name', $usrSort, $usrOrder); ?></a></th>
                    <th><a href="<?php echo usrSortUrl('email', $usrSort, $usrOrder, $usrSearch); ?>" style="color:inherit;text-decoration:none;display:inline-flex;align-items:center;gap:5px;">E-posta <?php echo usrSortIcon('email', $usrSort, $usrOrder); ?></a></th>
                    <th>Kayıt Türü</th>
                    <th><a href="<?php echo usrSortUrl('created_at', $usrSort, $usrOrder, $usrSearch); ?>" style="color:inherit;text-decoration:none;display:inline-flex;align-items:center;gap:5px;">Kayıt Tarihi <?php echo usrSortIcon('created_at', $usrSort, $usrOrder); ?></a></th>
                    <th><a href="<?php echo usrSortUrl('role', $usrSort, $usrOrder, $usrSearch); ?>" style="color:inherit;text-decoration:none;display:inline-flex;align-items:center;gap:5px;">Yetki <?php echo usrSortIcon('role', $usrSort, $usrOrder); ?></a></th>
                    <th style="text-align:right;width:200px;">İşlem</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $u):
                    $currentAdmin = (new \App\Core\Session())->get('admin_user');
                    $isSelf = ($currentAdmin && $currentAdmin['id'] === $u->id);
                ?>
                <tr>
                    <td style="font-weight:600;color:var(--text-1);"><?php echo htmlspecialchars($u->name); ?></td>
                    <td style="color:var(--text-2);"><?php echo htmlspecialchars($u->email); ?></td>
                    <td>
                        <?php if (!empty($u->google_id)): ?>
                        <span class="status-badge badge-danger" style="font-size:10px;"><i class="fa fa-google"></i> Google</span>
                        <?php else: ?>
                        <span class="status-badge badge-muted" style="font-size:10px;">Standart</span>
                        <?php endif; ?>
                    </td>
                    <td style="color:var(--text-3);font-size:12.5px;"><?php echo isset($u->created_at) ? htmlspecialchars($u->created_at) : '-'; ?></td>
                    <td>
                        <?php if ($u->role === 'admin'): ?>
                        <span style="color:var(--success);font-weight:700;"><i class="fa fa-shield"></i> Yönetici</span>
                        <?php elseif ($u->role === 'restricted'): ?>
                        <span style="color:var(--danger);font-weight:600;"><i class="fa fa-ban"></i> Kısıtlı</span>
                        <?php else: ?>
                        <span style="color:var(--text-2);font-weight:500;">Müşteri</span>
                        <?php endif; ?>
                    </td>
                    <td style="text-align:right;">
                        <?php if (!$isSelf): ?>
                        <form action="<?php echo $baseUrl; ?>/admin/users/update-role/<?php echo $u->id; ?>" method="POST" style="display:flex;gap:5px;align-items:center;justify-content:flex-end;">
                            <select name="role" class="admin-select" style="padding:4px 8px;font-size:11px;height:28px;min-width:90px;">
                                <option value="user"       <?php echo $u->role === 'user'       ? 'selected' : ''; ?>>Müşteri</option>
                                <option value="admin"      <?php echo $u->role === 'admin'      ? 'selected' : ''; ?>>Yönetici</option>
                                <option value="restricted" <?php echo $u->role === 'restricted' ? 'selected' : ''; ?>>Kısıtlı</option>
                            </select>
                            <button type="submit" class="admin-btn admin-btn-primary" style="padding:4px 10px;font-size:11px;height:28px;">Kaydet</button>
                        </form>
                        <?php else: ?>
                        <span style="color:var(--text-3);font-size:12px;font-style:italic;">Kendinizsiniz</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Mobile Cards -->
    <div class="mobile-only" style="padding:12px;">
        <?php foreach ($users as $u):
            $currentAdmin = (new \App\Core\Session())->get('admin_user');
            $isSelf = ($currentAdmin && $currentAdmin['id'] === $u->id);
        ?>
        <div class="m-card">
            <div class="m-card-header">
                <div class="m-card-title-wrap">
                    <div class="m-card-icon"><i class="fa fa-user"></i></div>
                    <div style="overflow:hidden;">
                        <h3 class="m-card-title"><?php echo htmlspecialchars($u->name); ?></h3>
                        <p class="m-card-subtitle"><?php echo htmlspecialchars($u->email); ?></p>
                    </div>
                </div>
                <div>
                    <?php if ($u->role === 'admin'): ?>
                    <span style="color:var(--success);font-size:12px;font-weight:700;"><i class="fa fa-shield"></i> Yönetici</span>
                    <?php elseif ($u->role === 'restricted'): ?>
                    <span style="color:var(--danger);font-size:12px;font-weight:700;"><i class="fa fa-ban"></i> Kısıtlı</span>
                    <?php else: ?>
                    <span style="color:var(--text-2);font-size:12px;">Müşteri</span>
                    <?php endif; ?>
                </div>
            </div>
            <div class="m-card-footer">
                <div class="m-card-badges">
                    <?php if (!empty($u->google_id)): ?>
                    <span class="m-badge" style="background:rgba(234,67,53,0.1);color:#ea4335;"><i class="fa fa-google"></i> Google</span>
                    <?php else: ?>
                    <span class="m-badge m-badge-dark">Standart</span>
                    <?php endif; ?>
                    <span class="m-badge m-badge-dark"><?php echo isset($u->created_at) ? date('d.m.y', strtotime($u->created_at)) : '-'; ?></span>
                </div>
                <?php if (!$isSelf): ?>
                <form action="<?php echo $baseUrl; ?>/admin/users/update-role/<?php echo $u->id; ?>" method="POST" style="display:flex;gap:5px;align-items:center;">
                    <select name="role" class="admin-select" style="padding:4px;font-size:11px;height:28px;min-width:80px;">
                        <option value="user"       <?php echo $u->role === 'user'       ? 'selected' : ''; ?>>Müşteri</option>
                        <option value="admin"      <?php echo $u->role === 'admin'      ? 'selected' : ''; ?>>Yönetici</option>
                        <option value="restricted" <?php echo $u->role === 'restricted' ? 'selected' : ''; ?>>Kısıtlı</option>
                    </select>
                    <button type="submit" class="admin-btn admin-btn-primary admin-btn-xs">Kaydet</button>
                </form>
                <?php else: ?>
                <span style="color:var(--text-3);font-size:12px;font-style:italic;">Kendinizsiniz</span>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <?php else: ?>
    <div style="text-align:center;padding:64px 20px;color:var(--text-3);">
        <i class="fa fa-users" style="font-size:56px;opacity:0.15;display:block;margin-bottom:18px;"></i>
        <p style="font-size:15px;"><?php echo $usrSearch ? '"'.htmlspecialchars($usrSearch).'" için sonuç bulunamadı.' : 'Henüz kayıtlı kullanıcı bulunmuyor.'; ?></p>
    </div>
    <?php endif; ?>

</div>

<script>
initAutoSearch('user-search-input', 'user-search-form', 400);
</script>

<?php include __DIR__ . '/../layout/footer.php'; ?>