<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/middleware.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/functions.php';
require_admin();

if (is_post()) {
    ensure_csrf();
    $id = (int)($_POST['id'] ?? 0);
    $status = $_POST['status'] ?? 'active';
    $pdo->prepare('UPDATE users SET status=? WHERE id=?')->execute([$status,$id]);
    set_flash('success','User updated');
    redirect('/admin/users.php');
}

$users = $pdo->query('SELECT id,full_name,email,phone,role,status,created_at FROM users ORDER BY created_at DESC')->fetchAll();
include __DIR__ . '/../includes/header.php';
?>
<style>
/* Users Admin Styles */
.admin-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; gap: 20px; flex-wrap: wrap; }
.admin-title { margin: 0; font-size: 32px; font-weight: 800; color: var(--text); letter-spacing: -0.02em; }

/* User Table */
.users-table-card { background: #fff; border: 1px solid var(--border); border-radius: 24px; overflow: hidden; box-shadow: var(--shadow-sm); }
.user-avatar { width: 44px; height: 44px; border-radius: 14px; background: #f1f5f9; color: #475569; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 15px; flex-shrink: 0; transition: transform 0.3s ease; }
.user-avatar.admin { background: rgba(16, 185, 129, 0.1); color: var(--primary); }


@media(max-width:767px){
  .admin-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  .table-wrap { display: none; }
  .mobile-grid { display: grid; gap: 16px; padding: 16px; }
}
@media(min-width:768px){
  .mobile-grid { display: none; }
}
</style>

<div class="admin-header">
    <div>
        <h1 class="admin-title">User Management</h1>
        <p style="margin:4px 0 0; color:var(--muted); font-size:15px">Overview of all registered administrators and customers.</p>
    </div>
</div>

<div class="users-table-card">
    <div class="table-wrap">
        <table class="striped">
            <thead>
                <tr>
                    <th style="padding-left:32px">User Info</th>
                    <th>Email Address</th>
                    <th>Phone Number</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th style="padding-right:32px; text-align:right">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $u): 
                    $isAdmin = $u['role'] === 'admin';
                    $initials = strtoupper(substr($u['full_name'], 0, 1));
                ?>
                <tr>
                    <td style="padding-left:32px">
                        <div style="display:flex; align-items:center; gap:12px">
                            <div class="user-avatar <?=$isAdmin?'admin':''?>"><?=$initials?></div>
                            <div style="font-weight:700; color:var(--text)"><?=e($u['full_name'])?></div>
                        </div>
                    </td>
                    <td><div style="font-size:14px; color:var(--text)"><?=e($u['email'])?></div></td>
                    <td><div style="font-size:14px; color:var(--muted)"><?=e($u['phone'])?></div></td>
                    <td>
                        <span class="badge" style="font-size:11px; font-weight:700; text-transform:uppercase; background:<?=$isAdmin?'#eff6ff':'#f1f5f9'?>; color:<?=$isAdmin?'#2563eb':'#475569'?>">
                            <?=e($u['role'])?>
                        </span>
                    </td>
                    <td>
                        <?php 
                        $st = $u['status'];
                        $cls = $st === 'active' ? 'status-approved' : 'status-inactive';
                        ?>
                        <span class="status-pill <?=$cls?>"><?=ucfirst($st)?></span>
                    </td>
                    <td style="padding-right:32px; text-align:right">
                        <form method="post" style="display:inline">
                            <?=csrf_field()?>
                            <input type="hidden" name="id" value="<?=e((string)$u['id'])?>">
                            <input type="hidden" name="status" value="<?=$u['status']==='active'?'inactive':'active'?>">
                            <button class="btn <?= $u['status']==='active' ? '' : 'primary' ?>" type="submit" style="padding:8px 16px; font-size:13px; font-weight:600">
                                <?=$u['status']==='active'?'Deactivate':'Activate'?>
                            </button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="mobile-grid">
        <?php foreach ($users as $u): 
            $isAdmin = $u['role'] === 'admin';
            $initials = strtoupper(substr($u['full_name'], 0, 1));
        ?>
        <div class="card" style="margin-bottom:0; padding:20px; border-radius:16px">
            <div style="display:flex; justify-content:space-between; align-items:flex-start; margin-bottom:16px">
                <div style="display:flex; align-items:center; gap:12px">
                    <div class="user-avatar <?=$isAdmin?'admin':''?>"><?=$initials?></div>
                    <div>
                        <div style="font-weight:700; color:var(--text)"><?=e($u['full_name'])?></div>
                        <div style="font-size:11px; color:var(--muted); text-transform:uppercase; font-weight:700"><?=e($u['role'])?></div>
                    </div>
                </div>
                <span class="status-pill <?=$u['status']==='active'?'status-approved':'status-inactive'?>">
                    <?=ucfirst($u['status'])?>
                </span>
            </div>
            <div style="font-size:13px; color:var(--muted); margin-bottom:4px"><?=e($u['email'])?></div>
            <div style="font-size:13px; color:var(--muted); margin-bottom:16px"><?=e($u['phone'])?></div>
            <form method="post" style="display:block">
                <?=csrf_field()?>
                <input type="hidden" name="id" value="<?=e((string)$u['id'])?>">
                <input type="hidden" name="status" value="<?=$u['status']==='active'?'inactive':'active'?>">
                <button class="btn" type="submit" style="width:100%; font-weight:700">
                    <?=$u['status']==='active'?'Deactivate Account':'Activate Account'?>
                </button>
            </form>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
