<!DOCTYPE html>
<html lang="vi">
<head>
    <?php require_once('head.php');?>
    <title>Quản Lý Server Cron</title>
    <?php require_once('nav.php');?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        .card { border-radius: 20px; border: none; box-shadow: 0 5px 15px rgba(0,0,0,0.05); }
        .table thead { background: #f8fafc; color: #475569; }
        .btn-action { transition: 0.3s; }
        .btn-action:hover { transform: translateY(-2px); }
        .badge-count { background: #e0e7ff; color: #4338ca; padding: 5px 12px; border-radius: 8px; font-weight: 700; }
    </style>
</head>

<?php
// Xử lý xóa an toàn
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $ketnoi->prepare("DELETE FROM `list_server_cron` WHERE `id` = ?");
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        echo '<script>Swal.fire("Thành công", "Đã xóa server", "success").then(() => { location.href="chuyen-muc.php"; });</script>';
    } else {
        echo '<script>Swal.fire("Lỗi", "Không thể xóa server", "error");</script>';
    }
}
?>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <div class="content-wrapper p-4">
            <div class="content-header mb-3">
                <div class="d-flex justify-content-between align-items-center">
                    <h1 class="h3 font-weight-bold">Danh sách Server Cron</h1>
                    <a href="add-server-cron.php" class="btn btn-primary px-4 py-2 rounded-pill shadow">
                        <i class="fas fa-plus mr-2"></i>Thêm Server
                    </a>
                </div>
            </div>

            <section class="content">
                <div class="card p-3">
                    <div class="card-body">
                        <table id="serverTable" class="table table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>NAME SERVER</th>
                                    <th>TỐI THIỂU</th>
                                    <th>ĐÃ SỬ DỤNG</th>
                                    <th>GIÁ</th>
                                    <th class="text-center">THAO TÁC</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $result = mysqli_query($ketnoi, "SELECT * FROM `list_server_cron`");
                                while($row = mysqli_fetch_assoc($result)) {
                                    $total_sv = mysqli_fetch_assoc(mysqli_query($ketnoi, "SELECT COUNT(*) as count FROM `list_cron` WHERE `server_cron`='".$row['id']."' AND `status`='ON' "))['count'];
                                ?>
                                <tr>
                                    <td class="font-weight-bold text-muted">#<?=$row['id'];?></td>
                                    <td class="font-weight-bold">Server <?=$row['name_cron'];?></td>
                                    <td><?=$row['min_time'];?>s</td>
                                    <td><span class="badge-count"><?=$total_sv;?></span></td>
                                    <td class="text-success font-weight-bold"><?=number_format($row['money']);?>đ</td>
                                    <td class="text-center">
                                        <a href="edit-server-cron.php?id=<?=$row['id'];?>" class="btn btn-sm btn-info btn-action">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button onclick="confirmDelete(<?=$row['id'];?>)" class="btn btn-sm btn-danger btn-action">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </section>
        </div>
    </div>

    <script>
    function confirmDelete(id) {
        Swal.fire({
            title: 'Xóa server này?',
            text: "Dữ liệu sẽ bị xóa vĩnh viễn!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            confirmButtonText: 'Đồng ý'
        }).then((result) => {
            if (result.isConfirmed) { window.location.href = "?delete=" + id; }
        });
    }

    $(document).ready(function() {
        $('#serverTable').DataTable({
            "language": { "url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Vietnamese.json" }
        });
    });
    </script>
    <?php require_once('foot.php');?>
</body>
</html>