<?php
$pageTitle = 'Edit User';
require_once __DIR__ . '/../../app/includes/header.php';
require_admin();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
$editUser = $stmt->fetch();
if (!$editUser) { flash('error', 'User not found.'); redirect('/users/index.php'); }
?>
<div class="page-head"><div><h1>Edit User</h1><p><?= e($editUser['email']) ?></p></div></div>
<div class="row g-4">
<div class="col-lg-7">
<form class="card app-card p-4" method="post" action="<?= APP_BASE ?>/users/update.php?id=<?= (int)$editUser['id'] ?>">
<?= csrf_field() ?><input type="hidden" name="action" value="update_user">
<h2>Account Details</h2>
<div class="row g-3">
    <div class="col-md-6"><label class="form-label">Name</label><input class="form-control" name="name" value="<?= e($editUser['name']) ?>" required maxlength="120"></div>
    <div class="col-md-6"><label class="form-label">Email</label><input class="form-control" type="email" name="email" value="<?= e($editUser['email']) ?>" required maxlength="190"></div>
    <div class="col-md-6"><label class="form-label">Role</label><select class="form-select" name="role"><?php foreach (['admin','sales','engineer','viewer'] as $r): ?><option value="<?= e($r) ?>" <?= $editUser['role']===$r?'selected':'' ?>><?= e(ucfirst($r)) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-6"><label class="form-label">Status</label><select class="form-select" name="status"><option value="active" <?= $editUser['status']==='active'?'selected':'' ?>>Active</option><option value="inactive" <?= $editUser['status']==='inactive'?'selected':'' ?>>Inactive</option></select></div>
</div>
<button class="btn btn-primary mt-4" type="submit">Save User</button>
</form>
</div>
<div class="col-lg-5">
<form class="card app-card p-4" method="post" action="<?= APP_BASE ?>/users/update.php?id=<?= (int)$editUser['id'] ?>">
<?= csrf_field() ?><input type="hidden" name="action" value="reset_password">
<h2>Reset Password</h2>
<label class="form-label">New Password</label><input class="form-control mb-3" type="password" name="password" required>
<label class="form-label">Confirm New Password</label><input class="form-control" type="password" name="confirm_password" required>
<button class="btn btn-primary mt-4" type="submit">Reset Password</button>
</form>
</div>
</div>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
