<?php
session_start();

if (!isset($_SESSION['user']) || $_SESSION['userRole'] != 'admin') {
    header('Location: login.php');
    exit;
}

include 'db.php';

// حذف سفارش
if (isset($_GET['del_order'])) {
    $stmt = $pdo->prepare("DELETE FROM orders WHERE id = :id");
    $stmt->execute([':id' => (int)$_GET['del_order']]);
    header('Location: admin.php');
    exit;
}

// حذف کاربر
if (isset($_GET['del_user'])) {
    $uid = (int)$_GET['del_user'];
    
    $pdo->prepare("DELETE FROM orders WHERE user_id = :id")->execute([':id' => $uid]);
    $pdo->prepare("DELETE FROM users WHERE id = :id AND role != 'admin'")->execute([':id' => $uid]);
    header('Location: admin.php?tab=users');
    exit;
}

// لود داده‌ها
$orders = $pdo->query("SELECT orders.*, users.username FROM orders JOIN users ON orders.user_id = users.id ORDER BY orders.created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
$users  = $pdo->query("SELECT * FROM users ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);

$tab = isset($_GET['tab']) ? $_GET['tab'] : 'orders';

include 'header.php';
?>

<style>
    .admin-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:20px; }
    .admin-header h2 { margin:0; color:#2c3e50; font-size:18px; }
    .logout-btn { padding:7px 16px; background:#e74c3c; color:white; text-decoration:none; border-radius:5px; font-size:13px; }

    .tabs { margin-bottom:20px; }
    .tabs a {
        display:inline-block; padding:8px 20px; margin-left:5px;
        background:white; color:#2c3e50; text-decoration:none;
        border-radius:5px 5px 0 0; font-size:14px;
        border:1px solid #ddd; border-bottom:none;
    }
    .tabs a.active { background:#2c3e50; color:white; }

    .data-table { width:100%; border-collapse:collapse; background:white; border-radius:0 8px 8px 8px; overflow:hidden; box-shadow:0 1px 4px rgba(0,0,0,0.1); }
    .data-table th { background:#2c3e50; color:white; padding:11px 15px; text-align:right; font-size:14px; }
    .data-table td { padding:10px 15px; font-size:13px; border-bottom:1px solid #eee; vertical-align:middle; }
    .data-table tr:last-child td { border-bottom:none; }
    .receipt-thumb { width:55px; height:55px; object-fit:cover; border-radius:4px; }
    .btn-del { color:#e74c3c; text-decoration:none; font-size:13px; }
    .btn-del:hover { text-decoration:underline; }
    .badge-admin { background:#e74c3c; color:white; font-size:11px; padding:2px 7px; border-radius:10px; }
    .badge-user  { background:#2980b9; color:white; font-size:11px; padding:2px 7px; border-radius:10px; }
    .no-data { text-align:center; color:#999; padding:30px; }
</style>

<div class="admin-header">
    <h2>🛠 پنل مدیریت</h2>
    <a href="logout.php" class="logout-btn">خروج</a>
</div>

<div class="tabs">
    <a href="admin.php?tab=orders" class="<?php echo $tab=='orders'?'active':''; ?>">📦 سفارشات (<?php echo count($orders); ?>)</a>
    <a href="admin.php?tab=users"  class="<?php echo $tab=='users' ?'active':''; ?>">👥 کاربران (<?php echo count($users); ?>)</a>
</div>

<?php if ($tab == 'orders'): ?>
    <?php if (count($orders) == 0): ?>
        <p class="no-data">هیچ سفارشی ثبت نشده.</p>
    <?php else: ?>
    <table class="data-table">
        <tr><th>#</th><th>کاربر</th><th>نام خریدار</th><th>کتاب‌ها</th><th>فیش</th><th>تاریخ</th><th>حذف</th></tr>
        <?php $i=1; foreach ($orders as $o): ?>
        <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $o['username']; ?></td>
            <td><?php echo $o['buyer_name']; ?></td>
            <td><?php echo $o['books']; ?></td>
            <td><a href="<?php echo $o['receipt_img']; ?>" target="_blank">
                <img src="<?php echo $o['receipt_img']; ?>" class="receipt-thumb" alt="فیش">
            </a></td>
            <td><?php echo $o['created_at']; ?></td>
            <td><a href="admin.php?del_order=<?php echo $o['id']; ?>" class="btn-del" onclick="return confirm('حذف شود؟')">🗑 حذف</a></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>

<?php else: ?>
    <table class="data-table">
        <tr><th>#</th><th>نام کاربری</th><th>نقش</th><th>حذف</th></tr>
        <?php $i=1; foreach ($users as $u): ?>
        <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $u['username']; ?></td>
            <td><span class="badge-<?php echo $u['role']; ?>"><?php echo $u['role']=='admin'?'مدیر':'کاربر'; ?></span></td>
            <td>
                <?php if ($u['role'] != 'admin'): ?>
                <a href="admin.php?del_user=<?php echo $u['id']; ?>&tab=users" class="btn-del" onclick="return confirm('کاربر و سفارشاتش حذف شود؟')">🗑 حذف</a>
                <?php else: ?>
                <span style="color:#ccc;font-size:12px;">—</span>
                <?php endif; ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>

<?php include 'footer.php'; ?>
