<?php
require_once '../../../config/database.php';
require_once '../../../config/auth.php';
require_once '../../../includes/header.php';
require_once '../../../includes/sidebar.php';

$db = getDB();

$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $limit;
$search = isset($_GET['search']) ? $_GET['search'] : '';
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$order = isset($_GET['order']) ? $_GET['order'] : 'ASC';

$allowed_sort = ['id', 'name', 'price', 'vram_capacity', 'age', 'wattage'];
$allowed_order = ['ASC', 'DESC'];

$sort = in_array($sort, $allowed_sort) ? $sort : 'id';
$order = in_array($order, $allowed_order) ? $order : 'ASC';

if (!empty($search)) {
    $vga_cards = $db->query("SELECT * FROM vga_cards WHERE name LIKE '%$search%' ORDER BY $sort $order LIMIT $start, $limit")->fetchAll();
    $total_data = $db->query("SELECT COUNT(*) AS total FROM vga_cards WHERE name LIKE '%$search%'")->fetch()['total'];
    $total_pages = ceil($total_data / $limit);
} else {
    $vga_cards = $db->query("SELECT * FROM vga_cards ORDER BY $sort $order LIMIT $start, $limit")->fetchAll();
    $total_data = $db->query("SELECT COUNT(*) AS total FROM vga_cards")->fetch()['total'];
    $total_pages = ceil($total_data / $limit);
}
?>

<!-- Content Wrapper -->
<div class="content-wrapper">
    <!-- Content Header -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Data VGA</h1>
                </div>
            </div>
        </div>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Daftar VGA</h3>
                    <div class="card-tools">
                        <?php if (in_array($_SESSION['role'], ['admin', 'super_admin'])): ?>
                            <a href="add.php" class="btn btn-primary btn-sm">
                                <i class="fas fa-plus"></i> Tambah VGA
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="card-body">
                    <?php if (isset($_SESSION['success'])): ?>
                        <div class="alert alert-success alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            <?php 
                            echo $_SESSION['success'];
                            unset($_SESSION['success']);
                            ?>
                        </div>
                    <?php endif; ?>

                    <?php if (isset($_SESSION['error'])): ?>
                        <div class="alert alert-danger alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            <?php 
                            echo $_SESSION['error'];
                            unset($_SESSION['error']);
                            ?>
                        </div>
                    <?php endif; ?>
                <div class="card-body">
                    <form action="" method="GET">
                        <div class="input-group mb-3">
                            <input type="text" class="form-control" placeholder="Cari VGA" name="search" value="<?php echo $search; ?>">
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">Cari</button>
                            </div>
                        </div>
                    </form>
                    <div class="table-responsive">
                        <table class="table table-hover text-nowrap">
                            <thead>
                                <tr>
                                    <th><a href="?sort=id&order=<?php echo $sort == 'id' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">ID <?php echo $sort == 'id' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=name&order=<?php echo $sort == 'name' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Nama <?php echo $sort == 'name' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=price&order=<?php echo $sort == 'price' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Harga <?php echo $sort == 'price' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=vram_capacity&order=<?php echo $sort == 'vram_capacity' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Kapasitas VRAM <?php echo $sort == 'vram_capacity' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=age&order=<?php echo $sort == 'age' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Umur <?php echo $sort == 'age' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=wattage&order=<?php echo $sort == 'wattage' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Wattage <?php echo $sort == 'wattage' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($vga_cards as $vga): ?>
                                <tr>
                                    <td><?php echo $vga['id']; ?></td>
                                    <td><?php echo htmlspecialchars($vga['name']); ?></td>
                                    <td>Rp <?php echo number_format($vga['price'], 0, ',', '.'); ?></td>
                                    <td><?php echo $vga['vram_capacity']; ?></td>
                                    <td><?php echo $vga['age']; ?> </td>
                                    <td><?php echo $vga['wattage']; ?> W</td>
                                    <td>
                                        <?php if (in_array($_SESSION['role'], ['admin', 'super_admin'])): ?>
                                            <a href="edit.php?id=<?php echo $vga['id']; ?>" class="btn btn-info btn-sm">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="delete.php?id=<?php echo $vga['id']; ?>" class="btn btn-danger btn-sm" 
                                               onclick="return confirm('Yakin ingin menghapus data ini?')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        <?php else: ?>
                                            <span class="btn btn-secondary btn-sm">
                                                <i class="fas fa-eye"></i> Lihat
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            <?php if ($page > 1): ?>
                                <li class="page-item"><a class="page-link" href="?page=<?php echo $page-1; ?>&search=<?php echo $search; ?>&sort=<?php echo $sort; ?>&order=<?php echo $order; ?>">Previous</a></li>
                            <?php endif; ?>

                            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                <li class="page-item <?php echo $page == $i ? 'active' : ''; ?>"><a class="page-link" href="?page=<?php echo $i; ?>&search=<?php echo $search; ?>&sort=<?php echo $sort; ?>&order=<?php echo $order; ?>"><?php echo $i; ?></a></li>
                            <?php endfor; ?>

                            <?php if ($page < $total_pages): ?>
                                <li class="page-item"><a class="page-link" href="?page=<?php echo $page+1; ?>&search=<?php echo $search; ?>&sort=<?php echo $sort; ?>&order=<?php echo $order; ?>">Next</a></li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </section>
</div>

<?php require_once '../../../includes/footer.php'; ?>