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

<head>
    <?php require_once('head.php');?>
    <title>Hệ Thống | Chỉnh Sửa Dịch Vụ Hosting</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 UI Premium Core */
        .custom-card {
            border: none !important;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.04) !important;
            border-radius: 16px !important;
            background: #ffffff;
            overflow: hidden;
        }
        .form-group label {
            font-weight: 600 !important;
            color: #475569 !important;
            font-size: 0.85rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 8px;
        }
        .form-control {
            border: 1px solid #e2e8f0 !important;
            border-radius: 10px !important;
            padding: 12px 16px !important;
            height: auto !important;
            color: #334155 !important;
            font-size: 0.95rem;
            transition: all 0.2s ease-in-out !important;
            background-color: #f8fafc;
        }
        .form-control:focus {
            background-color: #ffffff;
            border-color: #3b82f6 !important;
            box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1) !important;
        }
        .form-control[readonly] {
            background-color: #f1f5f9 !important;
            color: #64748b !important;
            cursor: not-allowed;
            border-style: dashed !important;
        }
        select.form-control {
            appearance: none;
            background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23475569' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: right 16px center;
            background-size: 16px;
            padding-right: 40px !important;
        }
        .btn-submit {
            background: #3b82f6 !important;
            border: none !important;
            border-radius: 10px !important;
            padding: 12px 30px !important;
            font-weight: 600 !important;
            box-shadow: 0 4px 12px rgba(59, 130, 246, 0.25);
            transition: all 0.2s;
        }
        .btn-submit:hover {
            background: #2563eb !important;
            transform: translateY(-1px);
            box-shadow: 0 6px 20px rgba(59, 130, 246, 0.35);
        }
        .btn-back {
            background: #f1f5f9 !important;
            color: #475569 !important;
            border: none !important;
            border-radius: 10px !important;
            padding: 12px 24px !important;
            font-weight: 500 !important;
            transition: all 0.2s;
        }
        .btn-back:hover {
            background: #e2e8f0 !important;
            color: #1e293b !important;
        }
        .section-divider {
            font-size: 0.8rem;
            font-weight: 700;
            color: #94a3b8;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            border-bottom: 2px dashed #e2e8f0;
            padding-bottom: 6px;
            margin-bottom: 20px;
            margin-top: 10px;
        }
    </style>
</head>

<?php
// Xử lý bảo mật ID từ URL
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);

if ($id) {
    // Lấy thông tin bản ghi cũ ra màn hình bằng Prepared Statement
    $stmt = $ketnoi->prepare("SELECT * FROM `lich_su_mua_host` WHERE `id` = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $toz_host = $stmt->get_result()->fetch_array();
    $stmt->close();
}

// Nếu không tìm thấy ID hợp lệ, chuyển hướng bảo vệ hệ thống
if (!$toz_host) {
    header("Location: lsmuahost.php");
    exit();
}

// Xử lý khi nhấn nút Lưu thay đổi
if (isset($_POST["submit"]))
{
    $tk_host = $_POST['tk_host'];
    $mk_host = $_POST['mk_host'];
    $status  = $_POST['status'];

    // Cập nhật an toàn chống Injection bằng SQL Prepared Statement
    $update_stmt = $ketnoi->prepare("UPDATE `lich_su_mua_host` SET `tk_host` = ?, `mk_host` = ?, `status` = ? WHERE `id` = ?");
    $update_stmt->bind_param("sssi", $tk_host, $mk_host, $status, $id);
    $update = $update_stmt->execute();
    $update_stmt->close();

    if($update)
    {
        echo '<script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            Swal.fire({
                icon: "success",
                title: "Thành Công",
                text: "Cập nhật dữ liệu tài khoản Hosting thành công!",
                confirmButtonColor: "#3b82f6"
            }).then(() => {
                window.location.href = "lsmuahost.php";
            });
        });
        </script>';
    }
    else
    {
        echo '<script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            Swal.fire({
                icon: "error",
                title: "Thất Bại",
                text: "Lỗi kết nối cơ sở dữ liệu, không thể lưu thay đổi!",
                confirmButtonColor: "#ef4444"
            });
        });
        </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 Hosting ##<?=$toz_host['id'];?></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"><a href="lsmuahost.php" class="text-muted">Lịch sử Host</a></li>
                                <li class="breadcrumb-item active text-primary">Biên tập</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 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-user-gear mr-2 text-primary"></i> Quản Trị & Cấp Phát Tài Khoản Hosting
                                    </h3>
                                </div>
                                <div class="card-body p-4">
                                    <form role="form" action="" method="post">
                                        
                                        <div class="section-divider"><i class="fa-solid fa-circle-info mr-1"></i> Thông tin dịch vụ gốc</div>
                                        <div class="row">
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-regular fa-user mr-1 text-muted"></i> Chủ Sở Hữu (Username)</label>
                                                <input readonly type="text" class="form-control" value="<?=$toz_host['username'];?>">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-solid fa-globe mr-1 text-muted"></i> Tên Miền Chính (Domain)</label>
                                                <input readonly type="text" class="form-control" value="<?=$toz_host['domain'];?>">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-regular fa-envelope mr-1 text-muted"></i> Email Đăng Ký</label>
                                                <input readonly type="text" class="form-control" value="<?=$toz_host['email'];?>">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-solid fa-box mr-1 text-muted"></i> Gói Cấu Hình Đã Mua</label>
                                                <input readonly type="text" class="form-control" value="<?=$toz_host['goi_host'];?>">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-regular fa-calendar-check mr-1 text-muted"></i> Ngày Kích Hoạt</label>
                                                <input readonly type="text" class="form-control" value="<?=ngay($toz_host['ngay_mua']);?>">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label><i class="fa-regular fa-calendar-minus mr-1 text-muted"></i> Thời Hạn Hết Hạn</label>
                                                <input readonly type="text" class="form-control" value="<?=ngay($toz_host['ngay_het']);?>">
                                            </div>
                                        </div>

                                        <div class="section-divider text-primary" style="border-color: rgba(59,130,246,0.2);"><i class="fa-solid fa-key mr-1"></i> Thông tin quản trị cấp cho khách</div>
                                        <div class="row">
                                            <div class="col-md-6 form-group mb-4">
                                                <label class="text-primary"><i class="fa-solid fa-circle-user mr-1"></i> Tài Khoản Đăng Nhập Host</label>
                                                <input type="text" class="form-control style-editable" name="tk_host" 
                                                       value="<?=$toz_host['tk_host'];?>" placeholder="Nhập tài khoản (ví dụ: u12344)">
                                            </div>
                                            <div class="col-md-6 form-group mb-4">
                                                <label class="text-primary"><i class="fa-solid fa-lock mr-1"></i> Mật Khẩu Truy Cập Host</label>
                                                <input type="text" class="form-control style-editable" name="mk_host" 
                                                       value="<?=$toz_host['mk_host'];?>" placeholder="Nhập mật khẩu truy cập">
                                            </div>
                                            <div class="col-md-12 form-group mb-4">
                                                <label class="text-primary"><i class="fa-solid fa-toggle-on mr-1"></i> Trạng Thái Dịch Vụ (Status)</label>
                                                <select class="form-control" name="status">
                                                    <option value="hoatdong" <?=($toz_host['status'] == 'hoatdong') ? 'selected' : '';?>>ON (Hoạt động tốt)</option>
                                                    <option value="daxoa" <?=($toz_host['status'] == 'daxoa') ? 'selected' : '';?>>Xoá (Tạm khóa / Thu hồi)</option>
                                                </select>
                                            </div>
                                        </div>

                                        <div class="d-flex align-items-center justify-content-between mt-4 pt-3" style="border-top: 1px solid #f1f5f9;">
                                            <a href="lsmuahost.php" class="btn btn-back">
                                                <i class="fas fa-arrow-left mr-2"></i>Quay Về Lịch Sử
                                            </a>
                                            <button type="submit" name="submit" class="btn btn-primary btn-submit">
                                                <i class="fas fa-save mr-2"></i>Lưu Thay Đổi
                                            </button>
                                        </div>
                                    </form>
                                </div>
                                </div>
                            </div>
                    </div>
                    </div></section>
            </div>
        <aside class="control-sidebar control-sidebar-dark"></aside>
    </div>
    <?php require_once('foot.php');?>
</body>

</html>