<?php
include 'auth.php';
include_once '../config/database.php';

// حظر أو فك حظر المستخدم
if (isset($_GET['toggle_block'])) {
    $id = $_GET['toggle_block'];
    $stmt = $conn->prepare("UPDATE users SET is_blocked = 1 - is_blocked WHERE id = ?");
    $stmt->execute([$id]);
    header("Location: users.php");
}

// تغيير كلمة السر
if (isset($_POST['change_pass'])) {
    $id = $_POST['user_id'];
    $new_pass = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
    $stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
    $stmt->execute([$new_pass, $id]);
    $msg = "تم تغيير كلمة السر بنجاح!";
}

if (isset($_GET['delete'])) {
    $stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
    $stmt->execute([$_GET['delete']]);
    header("Location: users.php");
}

$users = $conn->query("SELECT * FROM users WHERE role = 'user' ORDER BY id DESC")->fetchAll();

ob_start();
?>

<?php if(isset($msg)) echo "<div class='alert alert-success'>$msg</div>"; ?>

<div class="card p-4">
    <h5 class="fw-bold mb-4">إدارة المستخدمين</h5>
    <table class="table table-hover align-middle">
        <thead>
            <tr>
                <th>الاسم</th>
                <th>البريد الإلكتروني</th>
                <th>الحالة</th>
                <th>الإجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($users as $user): ?>
            <tr>
                <td>
                    <div class="d-flex align-items-center">
                        <img src="https://ui-avatars.com/api/?name=<?= urlencode($user['name']) ?>&background=random" class="rounded-circle me-2" width="40">
                        <span class="fw-bold"><?= htmlspecialchars($user['name']) ?></span>
                    </div>
                </td>
                <td><?= htmlspecialchars($user['email']) ?></td>
                <td>
                    <?php if($user['is_blocked']): ?>
                        <span class="badge bg-danger">محظور</span>
                    <?php else: ?>
                        <span class="badge bg-success">نشط</span>
                    <?php endif; ?>
                </td>
                <td>
                    <a href="?toggle_block=<?= $user['id'] ?>" class="btn btn-sm <?= $user['is_blocked'] ? 'btn-success' : 'btn-warning' ?>">
                        <?= $user['is_blocked'] ? 'فك الحظر' : 'حظر' ?>
                    </a>
                    
                    <button class="btn btn-sm btn-info text-white" data-bs-toggle="modal" data-bs-target="#passModal<?= $user['id'] ?>">
                        تغيير الباسورد
                    </button>

                    <a href="?delete=<?= $user['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('حذف المستخدم نهائياً؟')">
                        <i class="fas fa-trash"></i>
                    </a>

                    <!-- Modal تغيير الباسورد -->
                    <div class="modal fade" id="passModal<?= $user['id'] ?>" tabindex="-1">
                        <div class="modal-dialog">
                            <form method="POST" class="modal-content">
                                <div class="modal-header"><h5>تغيير باسوورد <?= $user['name'] ?></h5></div>
                                <div class="modal-body">
                                    <input type="hidden" name="user_id" value="<?= $user['id'] ?>">
                                    <input type="text" name="new_password" class="form-control" placeholder="كلمة السر الجديدة" required>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" name="change_pass" class="btn btn-primary">حفظ التغيير</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php
$content = ob_get_clean();
$page_title = 'إدارة المستخدمين';
include 'layout.php';
?>
