<!DOCTYPE html>
<html lang="vi">

<head>
    <?php require_once('head.php');?>
    <title>AdminLTE 3 | Danh sách gói host</title>
    <?php require_once('nav.php');?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        /* CSS Giao diện thiết kế cao cấp mới */
        .custom-card {
            border: none !important;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.03) !important;
            border-radius: 16px !important;
            overflow: hidden;
            background: #ffffff;
        }
        .custom-table {
            border-collapse: separate !important;
            border-spacing: 0 !important;
            width: 100% !important;
        }
        .custom-table thead th {
            background-color: #f8fafc !important;
            color: #64748b !important;
            font-weight: 600 !important;
            text-transform: uppercase !important;
            font-size: 0.75rem !important;
            letter-spacing: 0.05em !important;
            border-bottom: 2px solid #edf2f7 !important;
            border-top: none !important;
            padding: 16px !important;
        }
        .custom-table tbody tr {
            transition: all 0.2s ease;
        }
        .custom-table tbody tr:hover {
            background-color: #f1f5f9 !important;
        }
        .custom-table tbody td {
            padding: 16px !important;
            vertical-align: middle !important;
            color: #334155 !important;
            border-bottom: 1px solid #f1f5f9 !important;
            font-size: 0.9rem;
        }
        
        /* Trạng thái Badge tinh tế */
        .host-badge-name {
            background-color: #e0f2fe;
            color: #0369a1;
            padding: 4px 10px;
            border-radius: 6px;
            font-weight: 600;
            font-size: 0.85rem;
        }
        .host-badge-server {
            background-color: #f0fdf4;
            color: #166534;
            padding: 4px 10px;
            border-radius: 50px;
            font-weight: 500;
            font-size: 0.8rem;
            border: 1px solid #bbf7d0;
        }
        .host-badge-storage {
            background-color: #f5f3ff;
            color: #5b21b6;
            padding: 4px 10px;
            border-radius: 6px;
            font-weight: 500;
            font-size: 0.8rem;
        }
        .host-price {
            font-weight: 700;
            color: #0f172a;
            font-size: 0.95rem;
        }

        /* Nút thao tác tối giản tiện lợi */
        .btn-action {
            width: 36px;
            height: 36px;
            border-radius: 10px !important;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            margin: 0 2px;
            transition: all 0.2s;
            border: none !important;
        }
        .btn-action-edit { background: #f1f5f9; color: #475569; }
        .btn-action-edit:hover { background: #3b82f6; color: #ffffff; box-shadow: 0 4px 10px rgba(59,130,246,0.25); }
        .btn-action-delete { background: #fee2e2; color: #b91c1c; }
        .btn-action-delete:hover { background: #b91c1c; color: #ffffff; box-shadow: 0 4px 10px rgba(185,28,28,0.25); }
        
        /* Hệ thống phân trang kiểu hiện đại */
        .pagination-container {
            display: flex;
            justify-content: flex-end;
            padding: 20px 16px;
            background: #ffffff;
            border-top: 1px solid #f1f5f9;
        }
        .pagination {
            margin: 0 !important;
            gap: 6px;
        }
        .pagination .page-item .page-link {
            border-radius: 8px !important;
            border: 1px solid #e2e8f0 !important;
            color: #64748b;
            padding: 8px 14px;
            font-weight: 500;
            transition: all 0.2s;
        }
        .pagination .page-item .page-link:hover {
            background: #f1f5f9;
            color: #1e293b;
        }
        .pagination .page-item .page-link.active, 
        .pagination .page-item.active .page-link {
            background: #3b82f6 !important;
            border-color: #3b82f6 !important;
            color: #ffffff !important;
            box-shadow: 0 4px 10px rgba(59,130,246,0.25);
        }
    </style>
</head>

<?php
// XỬ LÝ KHI CƠ SỞ DỮ LIỆU NHẬN LỆNH XÓA (?delete=ID)
if (isset($_GET['delete'])) 
{
    // Ép kiểu số nguyên (int) để phòng chống SQL Injection nguy hiểm
    $delete = (int)$_GET['delete'];

    $create = mysqli_query($ketnoi, "DELETE FROM `list_host` WHERE `id` = '".$delete."' ");

    if ($create)
    {
      echo '<script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            Swal.fire({
                icon: "success",
                title: "Thành Công",
                text: "Đã xóa gói lưu trữ thành công!",
                confirmButtonColor: "#3b82f6"
            }).then(() => {
                location.href = "chuyen-muc.php";
            });
        });
      </script>'; 
    }
    else
    {
      echo '<script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            Swal.fire({
                icon: "error",
                title: "Lỗi",
                text: "Không thể xử lý, lỗi từ phía máy chủ!",
                confirmButtonColor: "#ef4444"
            }).then(() => {
                location.href = "chuyen-muc.php";
            });
        });
      </script>'; 
    }
}

// KHỞI CHẠY POPUP HỎI XÓA (?del=ID) BẰNG SWEETALERT2
if (isset($_GET['del'])) {
    $del = (int)$_GET['del'];
    echo '<script>
    document.addEventListener("DOMContentLoaded", function() {
        Swal.fire({
            title: "Xác nhận xóa?",
            text: "Bạn có chắc chắn muốn xóa gói host này? Hành động không thể hoàn tác!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#ef4444",
            cancelButtonColor: "#64748b",
            confirmButtonText: "Vẫn xóa gói",
            cancelButtonText: "Hủy bỏ"
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href = "?delete='.$del.'";
            } else {
                window.location.href = "?ok";
            }
        });
    });
    </script>';
}
?>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <div class="content-wrapper" style="background: #f8fafc;">
            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-3 align-items-center">
                        <div class="col-sm-6">
                            <h1 class="m-0 font-weight-bold text-dark" style="font-size: 1.5rem;">Cấu Hình Gói Hosting</h1>
                        </div><div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right" style="background: transparent; padding: 0;">
                                <li class="breadcrumb-item"><a href="#" class="text-muted">Home</a></li>
                                <li class="breadcrumb-item active text-primary">Gói host</li>
                            </ol>
                        </div></div></div></div>
            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card custom-card">
                                <div class="card-header d-flex align-items-center justify-content-between py-3" style="background: #ffffff; border-bottom: 1px solid #f1f5f9;">
                                    <h3 class="card-title font-weight-bold text-secondary mb-0" style="font-size: 1.05rem;">
                                        <i class="fas fa-boxes mr-2 text-primary"></i>Danh sách gói lưu trữ hiện có
                                    </h3>
                                    <div>
                                        <a class="btn btn-primary font-weight-bold px-4" href="add-goi-host.php" style="border-radius: 10px; background: #3b82f6; border: none; box-shadow: 0 4px 12px rgba(59,130,246,0.25);">
                                            <i class="fas fa-plus-circle mr-2"></i>Thêm Gói Mới
                                        </a>
                                    </div>
                                </div>
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table id="datatable1" class="table custom-table mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 70px; text-align: center;">ID</th>
                                                    <th>Tên Gói</th>
                                                    <th>Tiêu đề mô tả</th>
                                                    <th>Máy chủ quản lý</th>
                                                    <th>Dung lượng</th>
                                                    <th>Giá cấu hình</th>
                                                    <th style="width: 140px; text-align: center;">Thao Tác</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $result = mysqli_query($ketnoi,"SELECT * FROM `list_host` ");
                                                while($row = mysqli_fetch_assoc($result))
                                                {
                                                ?>
                                                <tr>
                                                    <td style="text-align: center;" class="font-weight-bold text-muted"><?=$row['id'];?></td>
                                                    <td><span class="host-badge-name"><?=$row['name_host'];?></span></td>
                                                    <td class="text-secondary font-weight-light"><?=$row['title_host'];?></td>
                                                    <?php 
                                                    $sv_host = $ketnoi->query("SELECT * FROM `list_server_host` WHERE `id` = '".$row['server_host']."' ")->fetch_array();
                                                    ?>
                                                    <td><span class="host-badge-server"><i class="fas fa-server mr-1"></i><?=isset($sv_host['name_server']) ? $sv_host['name_server'] : 'N/A';?></span></td>
                                                    <td><span class="host-badge-storage"><i class="fas fa-database mr-1"></i><?=$row['dung_luong'];?></span></td>
                                                    <td><span class="host-price"><?=tien($row['gia_host']);?>đ</span></td>
                                                    <td style="text-align: center;">
                                                        <a href="edit-goi-host.php?id=<?=$row['id'];?>"
                                                           class="btn-action btn-action-edit" title="Chỉnh sửa">
                                                            <i class="fas fa-pen-to-square"></i>
                                                        </a>
                                                        <a href="?del=<?=$row['id'];?>" 
                                                           class="btn-action btn-action-delete" title="Gỡ bỏ">
                                                            <i class="fas fa-trash-can"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                                <?php }?>
                                            </tbody>
                                        </table>

                                        <div id="pagination-container" class="pagination-container"></div>

                                        <script>
                                        $(document).ready(function() {
                                            var $table = $('#datatable1');
                                            var $rows = $table.find('tbody tr');
                                            var $paginationContainer = $('#pagination-container');

                                            var limitPerPage = 10;
                                            var totalPages = Math.ceil($rows.length / limitPerPage);

                                            if (totalPages > 1) {
                                                var pagination = '<ul class="pagination">';

                                                for (var i = 1; i <= totalPages; i++) {
                                                    pagination +=
                                                        '<li class="page-item"><a class="page-link" href="javascript:void(0);">' +
                                                        i + '</a></li>';
                                                }

                                                pagination += '</ul>';

                                                $paginationContainer.html(pagination).show();
                                                $rows.hide().slice(0, limitPerPage).show();
                                                $paginationContainer.find('.page-link').first().addClass('active');

                                                $paginationContainer.on('click', '.page-link', function() {
                                                    var currentPage = $(this).text();
                                                    var start = (currentPage - 1) * limitPerPage;
                                                    var end = start + limitPerPage;

                                                    $rows.hide().slice(start, end).show();

                                                    $paginationContainer.find('.page-link').removeClass('active');
                                                    $(this).addClass('active');
                                                });
                                            }
                                        });
                                        </script>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            </div>
    </div>

    <aside class="control-sidebar control-sidebar-dark">
    </aside>

    <?php require_once('foot.php');?>
</body>

</html>