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


requireRole(['admin', 'super_admin']);

$db = getDB();
$power_supplies = $db->query("SELECT * FROM power_supply ORDER BY id DESC")->fetchAll();
?>

<!-- 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 Power Supply</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 Power Supply</h3>
                    <div class="card-tools">
                        <a href="add.php" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> Tambah Power Supply
                        </a>
                    </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; ?>

                    <table class="table table-bordered table-striped datatable">
                        <thead>
                            <tr>
                                <th width="5%">No</th>
                                <th>Nama Power Supply</th>
                                <th>Wattage</th>
                                <th>Quality</th>
                                <th>Age</th>
                                <th>Harga</th>
                                <th width="15%">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($power_supplies as $index => $psu): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td><?php echo htmlspecialchars($psu['name']); ?></td>
                                <td><?php echo $psu['wattage']; ?>W</td>
                                <td>
                                    <div class="progress">
                                        <?php
                                        $quality = $psu['quality'];
                                        $qualityClass = '';
                                        if ($quality >= 7) {
                                            $qualityClass = 'bg-success';
                                        } elseif ($quality >= 4) {
                                            $qualityClass = 'bg-primary';
                                        } else {
                                            $qualityClass = 'bg-warning';
                                        }
                                        ?>
                                        <div class="progress-bar <?php echo $qualityClass; ?>" 
                                             role="progressbar" 
                                             style="width: <?php echo ($quality * 10); ?>%">
                                            <?php echo $quality; ?>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="progress">
                                        <?php
                                        $age = $psu['age'];
                                        $ageClass = '';
                                        if ($age <= 1) {
                                            $ageClass = 'bg-success';
                                        } elseif ($age <= 3) {
                                            $ageClass = 'bg-warning';
                                        } else {
                                            $ageClass = 'bg-danger';
                                        }
                                        ?>
                                        <div class="progress-bar <?php echo $ageClass; ?>" 
                                             role="progressbar" 
                                             style="width: <?php echo ($age * 20); ?>%">
                                            <?php echo $age; ?>
                                        </div>
                                    </div>
                                </td>
                                <td>Rp <?php echo number_format($psu['price'], 0, ',', '.'); ?></td>
                                <td class="text-center">
                                    <a href="edit.php?id=<?php echo $psu['id']; ?>" 
                                       class="btn btn-info btn-sm" 
                                       data-toggle="tooltip" 
                                       title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="delete.php?id=<?php echo $psu['id']; ?>" 
                                       class="btn btn-danger btn-sm" 
                                       onclick="return confirm('Apakah Anda yakin ingin menghapus power supply ini?');"
                                       data-toggle="tooltip" 
                                       title="Delete">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>

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

<script>
$(function () {
    $('.datatable').DataTable({
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": false,
        "responsive": true,
    });
});
</script>
