<?php
include 'config.php';

// Ambil data TV
$query = mysqli_query($conn, "SELECT * FROM tvs ORDER BY nama_tv ASC");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Billing Rental PS - TV List</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div class="container">
    <h1>DASHBOARD BILLING</h1>
    
    <div class="tv-grid">
        <?php while($tv = mysqli_fetch_assoc($query)): ?>
            
            <?php
            // Logika Status
            $statusClass = 'available';
            $statusText = 'KOSONG';
            
            if($tv['status'] == 'active') {
                $statusClass = 'active';
                $statusText = 'MAIN';
            } elseif($tv['status'] == 'paused') {
                $statusClass = 'paused';
                $statusText = 'PAUSE';
            }
            ?>

            <div class="tv-card <?php echo $statusClass; ?>">
                <div class="tv-header">
                    <span style="font-weight:bold; font-size:1.2rem;"><?php echo $tv['nama_tv']; ?></span>
                    <span class="tv-status status-<?php echo $statusClass; ?>"><?php echo $statusText; ?></span>
                </div>

                <!-- Timer Area (Jika Aktif) -->
                <?php if($tv['status'] != 'available'): 
                    // Hitung durasi berjalan
                    $trx = mysqli_query($conn, "SELECT * FROM transactions WHERE id = '".$tv['current_trx_id']."'");
                    $d = mysqli_fetch_assoc($trx);
                    $start = new DateTime($d['start_time']);
                    $now = new DateTime();
                    $diff = $start->diff($now);
                ?>
                    <div class="timer">
                        <?php echo $diff->h . " Jam " . $diff->i . " Menit"; ?>
                    </div>
                <?php else: ?>
                    <div class="timer">-- : --</div>
                <?php endif; ?>

                <!-- Tombol Kontrol -->
                <div class="btn-group">
                    <?php if($tv['status'] == 'available'): ?>
                        <!-- Form Mulai -->
                        <form action="process.php" method="POST" style="width:100%">
                            <input type="hidden" name="tv_id" value="<?php echo $tv['id']; ?>">
                            <input type="hidden" name="action" value="start">
                            <select name="paket_id" style="width:100%; padding:5px; margin-bottom:5px;">
                                <?php 
                                $pkts = mysqli_query($conn, "SELECT * FROM packages");
                                while($p = mysqli_fetch_assoc($pkts)) {
                                    echo "<option value='".$p['id']."'>".$p['nama_paket']." - Rp ".number_format($p['harga'])."</option>";
                                }
                                ?>
                            </select>
                            <button type="submit" class="btn btn-start">MULAI</button>
                        </form>

                    <?php else: ?>
                        <!-- Tombol Aksi saat Main -->
                        <a href="process.php?action=pause&tv_id=<?php echo $tv['id']; ?>" class="btn btn-pause">JEDA</a>
                        <a href="process.php?action=stop&tv_id=<?php echo $tv['id']; ?>" class="btn btn-stop" onclick="return confirm('Selesaikan billing?')">STOP</a>
                        
                        <!-- Pindah TV Button -->
                        <button onclick="openMoveModal(<?php echo $tv['id']; ?>)" class="btn btn-move">PINDAH</button>
                    <?php endif; ?>
                    
                    <!-- Tombol Matikan TV (ADB Only) -->
                    <a href="process.php?action=shutdown_tv&tv_id=<?php echo $tv['id']; ?>" class="btn btn-off" style="margin-top:5px; width:100%">MATIKAN TV</a>
                </div>
            </div>

        <?php endwhile; ?>
    </div>
</div>

<!-- Modal Pindah TV Sederhana -->
<div id="moveModal" class="modal">
    <div class="modal-content">
        <h3>Pindah TV</h3>
        <form action="process.php" method="POST">
            <input type="hidden" name="action" value="move">
            <input type="hidden" name="from_tv_id" id="from_tv_id">
            
            <p>Pindah ke:</p>
            <select name="to_tv_id" style="width:100%; padding:10px; margin-bottom:15px;">
                <?php 
                // Hanya tampilkan TV yang KOSONG
                $free_tvs = mysqli_query($conn, "SELECT * FROM tvs WHERE status='available'");
                while($ft = mysqli_fetch_assoc($free_tvs)) {
                    echo "<option value='".$ft['id']."'>".$ft['nama_tv']."</option>";
                }
                ?>
            </select>
            <button type="submit" class="btn btn-move">KONFIRMASI PINDAH</button>
            <button type="button" class="btn btn-stop" onclick="document.getElementById('moveModal').style.display='none'">BATAL</button>
        </form>
    </div>
</div>

<script>
function openMoveModal(id) {
    document.getElementById('from_tv_id').value = id;
    document.getElementById('moveModal').style.display = 'block';
}
</script>

</body>
</html>