<?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', 'capacity', 'type'];
$allowed_order = ['ASC', 'DESC'];

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

if (!empty($search)) {
    $ram = $db->query("SELECT * FROM ram WHERE name LIKE '%$search%' ORDER BY $sort $order LIMIT $start, $limit")->fetchAll();
    $total_data = $db->query("SELECT COUNT(*) AS total FROM ram WHERE name LIKE '%$search%'")->fetch()['total'];
    $total_pages = ceil($total_data / $limit);
} else {
    $ram = $db->query("SELECT * FROM ram ORDER BY $sort $order LIMIT $start, $limit")->fetchAll();
    $total_data = $db->query("SELECT COUNT(*) AS total FROM ram")->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 RAM</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 RAM</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 RAM
                            </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 RAM" 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=capacity&order=<?php echo $sort == 'capacity' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Kapasitas <?php echo $sort == 'capacity' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th><a href="?sort=type&order=<?php echo $sort == 'type' && $order == 'ASC' ? 'DESC' : 'ASC'; ?>&search=<?php echo $search; ?>">Tipe <?php echo $sort == 'type' ? '<i class="fas fa-sort-' . strtolower($order) . '"></i>' : ''; ?></a></th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($ram as $rm): ?>
                                <tr>
                                    <td><?php echo $rm['id']; ?></td>
                                    <td><?php echo htmlspecialchars($rm['name']); ?></td>
                                    <td>Rp <?php echo number_format($rm['price'], 0, ',', '.'); ?></td>
                                    <td><?php echo $rm['capacity']; ?></td>
                                    <td><?php echo $rm['type']; ?></td>
                                    <td>
                                        <?php if (in_array($_SESSION['role'], ['admin', 'super_admin'])): ?>
                                            <a href="edit.php?id=<?php echo $rm['id']; ?>" class="btn btn-info btn-sm">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="delete.php?id=<?php echo $rm['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'; ?>