<?php
// admin/banners.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

// Handle Delete
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    
    // Get banner image
    $stmt = $pdo->prepare("SELECT image FROM banners WHERE id = ?");
    $stmt->execute([$id]);
    $banner = $stmt->fetch();
    
    if ($banner) {
        // Delete image file
        $image_path = __DIR__ . '/../assets/images/banners/' . $banner['image'];
        if (file_exists($image_path)) {
            unlink($image_path);
        }
    }
    
    $stmt = $pdo->prepare("DELETE FROM banners WHERE id = ?");
    $stmt->execute([$id]);
    $_SESSION['success_msg'] = 'Banner deleted successfully!';
    header('Location: banners.php');
    exit;
}

// Handle Status Toggle
if (isset($_GET['toggle'])) {
    $id = intval($_GET['toggle']);
    $stmt = $pdo->prepare("UPDATE banners SET status = NOT status WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: banners.php');
    exit;
}

// Get all banners
$stmt = $pdo->query("SELECT * FROM banners ORDER BY `order` ASC, created_at DESC");
$banners = $stmt->fetchAll();

$success_msg = $_SESSION['success_msg'] ?? '';
$error_msg = $_SESSION['error_msg'] ?? '';
unset($_SESSION['success_msg']);
unset($_SESSION['error_msg']);

include 'header.php';
?>

<style>
.banner-preview {
    width: 120px;
    height: 70px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
}

.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.status-badge.active {
    background: #86efac;
    color: #166534;
}

.status-badge.inactive {
    background: #fca5a5;
    color: #991b1b;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-images me-2"></i> Banners</h2>
        <a href="add-banner.php" class="btn btn-primary">
            <i class="fa-solid fa-plus"></i> Add Banner
        </a>
    </div>
    
    <?php if ($success_msg): ?>
    <div class="alert alert-success alert-dismissible fade show">
        <?php echo $success_msg; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>
    
    <?php if ($error_msg): ?>
    <div class="alert alert-danger alert-dismissible fade show">
        <?php echo $error_msg; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>
    
    <div class="admin-card">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Image</th>
                        <th>Title</th>
                        <th>Description</th>
                        <th>Link</th>
                        <th>Order</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($banners)): ?>
                    <tr>
                        <td colspan="9" class="text-center py-4">
                            <i class="fa-regular fa-image fa-2x text-muted d-block mb-2"></i>
                            No banners found. <a href="add-banner.php">Add your first banner</a>
                        </td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($banners as $banner): ?>
                    <tr>
                        <td><?php echo $banner['id']; ?></td>
                        <td>
                            <?php if ($banner['image']): ?>
                            <img src="../assets/images/banners/<?php echo $banner['image']; ?>" 
                                 alt="<?php echo $banner['title']; ?>" 
                                 class="banner-preview">
                            <?php else: ?>
                            <span class="text-muted">No image</span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo $banner['title'] ?: 'N/A'; ?></td>
                        <td>
                            <?php if ($banner['description']): ?>
                            <?php echo substr($banner['description'], 0, 40) . (strlen($banner['description']) > 40 ? '...' : ''); ?>
                            <?php else: ?>
                            <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($banner['link']): ?>
                            <a href="<?php echo $banner['link']; ?>" target="_blank" class="text-truncate d-inline-block" style="max-width:100px;">
                                <?php echo $banner['link']; ?>
                            </a>
                            <?php else: ?>
                            <span class="text-muted">#</span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo $banner['order']; ?></td>
                        <td>
                            <span class="status-badge <?php echo $banner['status'] ? 'active' : 'inactive'; ?>">
                                <?php echo $banner['status'] ? 'Active' : 'Inactive'; ?>
                            </span>
                        </td>
                        <td><?php echo date('d M Y', strtotime($banner['created_at'])); ?></td>
                        <td>
                            <a href="edit-banner.php?id=<?php echo $banner['id']; ?>" class="btn btn-sm btn-warning">
                                <i class="fa-solid fa-edit"></i>
                            </a>
                            <a href="banners.php?toggle=<?php echo $banner['id']; ?>" class="btn btn-sm <?php echo $banner['status'] ? 'btn-secondary' : 'btn-success'; ?>">
                                <i class="fa-solid <?php echo $banner['status'] ? 'fa-pause' : 'fa-play'; ?>"></i>
                            </a>
                            <a href="banners.php?delete=<?php echo $banner['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this banner?')">
                                <i class="fa-solid fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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