<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; }
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

$sliders = get_all_sliders($pdo);
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['admin_sliders']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="sidebar">
        <div class="sidebar-brand"><h2>VAULTIXAI</h2></div>
        <nav><ul>
            <li><a href="dashboard.php"><span class="nav-icon">🏠</span><?php echo $t['admin_dashboard']; ?></a></li>
            <li><a href="pages.php"><span class="nav-icon">📄</span><?php echo $t['admin_pages']; ?></a></li>
            <li><a href="certifications.php"><span class="nav-icon">🏆</span><?php echo $t['admin_certifications']; ?></a></li>
            <li><a href="services.php"><span class="nav-icon">⚙️</span><?php echo $t['admin_services']; ?></a></li>
            <li><a href="sliders.php" class="active"><span class="nav-icon">🖼️</span><?php echo $t['admin_sliders']; ?></a></li>
            <li><a href="users.php"><span class="nav-icon">👥</span><?php echo $t['admin_users']; ?></a></li>
        </ul></nav>
        <a href="<?php echo lang_switch_url($t['lang_switch_code']); ?>" class="sidebar-lang-btn">🌐 <?php echo $t['lang_switch_label']; ?></a>
        <div class="sidebar-footer"><a href="logout.php"><span class="nav-icon">🚪</span><?php echo $t['admin_logout']; ?></a></div>
    </div>

    <div class="main-content">
        <div class="topbar">
            <h1>🖼️ <?php echo $t['admin_sliders']; ?></h1>
            <a href="add_slider.php" class="btn btn-add px-4 py-2">➕ <?php echo $t['slider_add_btn']; ?></a>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="alert alert-success fw-bold mb-4">✅ <?php switch ($_GET['success']) {
                case 'add': echo $t['success_added']; break;
                case 'edit': echo $t['success_updated']; break;
                case 'delete': echo $t['success_deleted']; break;
            } ?></div>
        <?php endif; ?>

        <div class="table-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th style="width:60px;"><?php echo $t['users_table_id']; ?></th>
                            <th style="width:110px;"><?php echo $isRTL ? 'الصورة' : 'Image'; ?></th>
                            <th><?php echo $t['slider_title']; ?></th>
                            <th><?php echo $t['slider_description']; ?></th>
                            <th style="width:100px;"><?php echo $t['slider_status']; ?></th>
                            <th style="width:200px;text-align:center;"><?php echo $t['users_table_actions']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($sliders)): ?>
                            <tr><td colspan="6" class="text-center py-5 text-muted">😶 <?php echo $t['slider_no_sliders']; ?></td></tr>
                        <?php else: ?>
                            <?php foreach ($sliders as $slider): ?>
                            <tr>
                                <td class="text-muted small fw-bold">#<?php echo $slider['id']; ?></td>
                                <td>
                                    <?php if (!empty($slider['image_url'])): ?>
                                        <?php $imageSrc = '../public/' . ltrim($slider['image_url'], '/'); ?>
                                        <img src="<?php echo htmlspecialchars($imageSrc); ?>" alt="<?php echo htmlspecialchars($slider['title']); ?>" style="width:80px;height:48px;object-fit:cover;border-radius:8px;">
                                    <?php else: ?>
                                        <span class="text-muted small">-</span>
                                    <?php endif; ?>
                                </td>
                                <td class="fw-bold text-dark"><?php echo htmlspecialchars($slider['title']); ?></td>
                                <td class="text-muted small"><?php echo htmlspecialchars(mb_strimwidth($slider['description'], 0, 70, '…')); ?></td>
                                <td>
                                    <?php if ($slider['active']): ?>
                                        <span class="badge bg-success"><?php echo $t['slider_active']; ?></span>
                                    <?php else: ?>
                                        <span class="badge bg-danger"><?php echo $t['slider_inactive']; ?></span>
                                    <?php endif; ?>
                                </td>
                                <td style="text-align:center;">
                                    <a href="add_slider.php?edit=<?php echo $slider['id']; ?>" class="btn btn-edit btn-sm px-3 py-1 me-1">✏️ <?php echo $t['service_edit_btn']; ?></a>
                                    <a href="delete_slider.php?id=<?php echo $slider['id']; ?>"
                                       class="btn btn-delete btn-sm px-3 py-1" data-confirm="<?php echo htmlspecialchars($t['slider_delete_confirm']); ?>">
                                       🗑️ <?php echo $t['service_delete_btn']; ?></a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
