<!DOCTYPE html>
<html lang="en">
<head>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/head2.php';?>
    <title>Quản lý web | <?=$lxtdev['ten_web'];?></title>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/nav.php';?>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        /* CSS Gói nâng cấp giao diện Modern Premium */
        .premium-management-card {
            background: rgba(255, 255, 255, 0.75) !important;
            backdrop-filter: blur(15px) !important;
            -webkit-backdrop-filter: blur(15px) !important;
            border: 1px solid rgba(255, 255, 255, 0.4) !important;
            border-radius: 24px !important;
            box-shadow: 0 20px 45px rgba(0, 0, 0, 0.05) !important;
            overflow: hidden;
        }
        
        [data-bs-theme="dark"] .premium-management-card {
            background: rgba(30, 32, 43, 0.75) !important;
            border: 1px solid rgba(255, 255, 255, 0.05) !important;
            box-shadow: 0 20px 45px rgba(0, 0, 0, 0.25) !important;
        }

        /* Menu tab bên trái */
        .custom-menu-link {
            border-radius: 12px !important;
            padding: 14px 20px !important;
            font-weight: 700 !important;
            color: #5e6278 !important;
            transition: all 0.3s ease !important;
            display: flex;
            align-items: center;
            gap: 10px;
            cursor: pointer;
        }
        .custom-menu-link:hover {
            background: rgba(48, 133, 214, 0.08) !important;
            color: #3085d6 !important;
        }
        .custom-menu-link.active-tab {
            background: linear-gradient(135deg, #2e86de, #3085d6) !important;
            color: #ffffff !important;
            box-shadow: 0 8px 20px rgba(48, 133, 214, 0.25) !important;
        }

        /* Khung chứa data thông tin */
        .info-display-group {
            background: rgba(255, 255, 255, 0.9);
            border: 1px solid rgba(0, 0, 0, 0.05);
            border-radius: 14px;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            transition: 0.3s;
        }
        [data-bs-theme="dark"] .info-display-group {
            background: rgba(255, 255, 255, 0.02);
            border: 1px solid rgba(255, 255, 255, 0.05);
        }
        .info-display-group:hover {
            transform: translateX(4px);
            border-color: #3085d6;
        }

        /* Nút copy, eye action */
        .action-icon-btn {
            color: #a1a5b7;
            cursor: pointer;
            transition: 0.2s;
            font-size: 1.1rem;
            margin-left: 12px;
        }
        .action-icon-btn:hover {
            color: #3085d6;
            transform: scale(1.15);
        }

        /* Hiệu ứng tab mượt */
        .tab-content-fade {
            animation: fadeInTab 0.4s ease-in-out forwards;
        }
        @keyframes fadeInTab {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .btn-premium-action {
            background: linear-gradient(135deg, #2e86de, #3085d6) !important;
            border: none !important;
            color: white !important;
            font-weight: 700 !important;
            padding: 12px 25px !important;
            border-radius: 12px !important;
            box-shadow: 0 8px 20px rgba(48, 133, 214, 0.3) !important;
            transition: 0.3s !important;
        }
        .btn-premium-action:hover {
            transform: translateY(-2px) !important;
            box-shadow: 0 12px 25px rgba(48, 133, 214, 0.45) !important;
        }
    </style>
</head>

<?php
if(isset($_GET['id'])) {
    $id = $_GET['id'];
    $check_host = $ketnoi->query("SELECT * FROM `lich_su_tao_web` WHERE `id` = '$id' ");
    if($check_host->num_rows == 1){
        $api_site = $ketnoi->query("SELECT * FROM `lich_su_tao_web` WHERE `id` = '$id' ")->fetch_array();
        $loai_site = $ketnoi->query("SELECT * FROM `list_mau_web` WHERE `id` = '".$api_site['loaiweb']."' ")->fetch_array();
        if($api_site['username']!=$username){
            echo '<script type="text/javascript">if(!alert("Website không tồn tại hay không phải của bạn!")){window.location.href = "/history-host";}</script>';
        }
    }else{
        echo '<script type="text/javascript">if(!alert("Website không tồn tại hay không phải của bạn!")){window.location.href = "/history-host";}</script>';
    }
}
?>

<body id="kt_app_body" data-kt-app-layout="light-sidebar" data-kt-app-header-fixed="true"
    data-kt-app-sidebar-enabled="true" data-kt-app-sidebar-fixed="true" data-kt-app-sidebar-hoverable="true"
    data-kt-app-sidebar-push-header="true" data-kt-app-sidebar-push-toolbar="true"
    data-kt-app-sidebar-push-footer="true" data-kt-app-toolbar-enabled="true" class="app-default">
    
    <script>var defaultThemeMode = "light"; var themeMode; if (document.documentElement) { if (document.documentElement.hasAttribute("data-bs-theme-mode")) { themeMode = document.documentElement.getAttribute("data-bs-theme-mode"); } else { if (localStorage.getItem("data-bs-theme") !== null) { themeMode = localStorage.getItem("data-bs-theme"); } else { themeMode = defaultThemeMode; } } if (themeMode === "system") { themeMode = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; } document.documentElement.setAttribute("data-bs-theme", themeMode); }</script>
    
    <div class="d-flex flex-column flex-root app-root" id="kt_app_root">
        <div class="app-page flex-column flex-column-fluid" id="kt_app_page">
            
            <div class="app-wrapper flex-column flex-row-fluid" id="kt_app_wrapper">
                <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/sider.php';?>
            </div>
            
            <div class="app-main flex-column flex-row-fluid" id="kt_app_main">
                <div class="d-flex flex-column flex-column-fluid">
                    
                    <div id="kt_app_toolbar" class="app-toolbar py-3 py-lg-6">
                        <div id="kt_app_toolbar_container" class="app-container container-fluid d-flex flex-stack">
                            <div class="page-title d-flex flex-column justify-content-center flex-wrap me-3">
                                <h1 class="page-heading d-flex text-dark fw-bold fs-3 flex-column justify-content-center my-0">
                                    <i class="fa-solid fa-gauge-high text-primary fs-4 mb-1"></i> Quản lý trang Web
                                </h1>
                                <ul class="breadcrumb breadcrumb-separatorless fw-semibold fs-7 my-0 pt-1">
                                    <li class="breadcrumb-item text-muted"><a href="/" class="text-muted text-hover-primary">Home</a></li>
                                    <li class="breadcrumb-item"><span class="bullet bg-gray-400 w-5px h-2px"></span></li>
                                    <li class="breadcrumb-item text-muted">Bảng Tính</li>
                                </ul>
                            </div>
                            <div class="d-flex align-items-center gap-2 gap-lg-3">
                                <a href="#" class="btn btn-sm fw-bold btn-light-success px-4 py-3" data-bs-toggle="modal" data-bs-target="#kt_modal_create_app">
                                    <i class="fa-solid fa-wallet me-1"></i> <?=tien($user['money']);?>đ
                                </a>
                            </div>
                        </div>
                    </div>

                    <div class="content d-flex flex-column flex-column-fluid" id="kt_content">
                        <div class="container-xxl" id="kt_content_container">
                            
                            <div class="card premium-management-card">
                                <div class="card-body p-lg-12">
                                    
                                    <div class="text-center mb-10">
                                        <h1 class="fw-black text-dark mb-2" style="letter-spacing: -0.5px;"><i class="fa-solid fa-laptop-code text-primary me-2"></i>THÔNG TIN QUẢN LÝ WEB</h1>
                                        <div class="d-inline-block bg-light-danger px-4 py-2 rounded-pill border border-dashed border-danger mt-1">
                                            <a href="https://<?=$api_site['domain'];?>/" target="_blank" class="fw-bold fs-5 text-danger text-hover-primary"><i class="fa-solid fa-link me-1"></i> <?=$api_site['domain'];?></a>
                                        </div>
                                    </div>
                                    
                                    <div class="row g-10">
                                        <div class="col-lg-3 border-end-lg">
                                            <h4 class="text-gray-400 text-uppercase fs-7 fw-bolder mb-6"><i class="fa-solid fa-bars-staggered me-1"></i> Thao tác nhanh</h4>
                                            <div class="d-flex flex-column gap-2">
                                                <a onclick="showitem1()" class="custom-menu-link active-tab" id="btn1">
                                                    <i class="fa-solid fa-circle-info fs-5"></i> Thông tin quản lý
                                                </a>
                                                <a onclick="showitem2()" class="custom-menu-link" id="btn4">
                                                    <i class="fa-solid fa-clock-rotate-left fs-5"></i> Gia hạn hệ thống
                                                </a>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-9">
                                            
                                            <div id="item1" class="tab-content-fade">
                                                <h3 class="text-dark fw-bolder mb-6"><i class="fa-solid fa-shield-halved text-primary me-2"></i>Cơ sở dữ liệu quản trị</h3>
                                                
                                                <div class="row align-items-center mb-6">
                                                    <label class="col-md-4 fs-6 fw-bold text-gray-700">Địa chỉ Tên miền:</label>
                                                    <div class="col-md-8">
                                                        <div class="info-display-group">
                                                            <a href="https://<?=$api_site['domain'];?>/" target="_blank" class="text-primary fw-bolder fs-5 text-decoration-underline"><i class="fa-solid fa-globe me-1"></i> <?=$api_site['domain'];?></a>
                                                            <i class="fa-solid fa-arrow-up-right-from-square text-muted fs-7"></i>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="row align-items-center mb-6">
                                                    <label class="col-md-4 fs-6 fw-bold text-gray-700">Tài khoản Admin:</label>
                                                    <div class="col-md-8">
                                                        <div class="info-display-group">
                                                            <span class="fw-bolder fs-5 text-gray-800" id="tk_admin_text"><?=$api_site['user_admin'];?></span>
                                                            <i class="fa-regular fa-copy action-icon-btn" id="copy-tk-btn" onclick="copytk_admin()"></i>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="row align-items-center mb-6">
                                                    <label class="col-md-4 fs-6 fw-bold text-gray-700">Mật khẩu Admin:</label>
                                                    <div class="col-md-8">
                                                        <div class="info-display-group">
                                                            <span class="fw-bolder fs-5 text-gray-800" id="password">******</span>
                                                            <div>
                                                                <i class="fa-regular fa-eye-slash action-icon-btn" id="toggle-password" onclick="showpass_admin()"></i>
                                                                <i class="fa-regular fa-copy action-icon-btn" id="copy-password" onclick="copypass_admin()"></i>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="row align-items-center">
                                                    <label class="col-md-4 fs-6 fw-bold text-gray-700">Trạng thái vận hành:</label>
                                                    <div class="col-md-8">
                                                        <div class="px-2"><?=status($api_site['status']);?></div>
                                                    </div>
                                                </div>
                                            </div>

                                            <div id="item2" class="tab-content-fade" style="display: none;">
                                                <h3 class="text-dark fw-bolder mb-6"><i class="fa-regular fa-calendar-plus text-primary me-2"></i>Duy trì & Gia hạn Website</h3>
                                                
                                                <div class="row g-6 mb-6">
                                                    <div class="col-md-6">
                                                        <div class="p-5 rounded-3 bg-light-primary border border-dashed border-primary text-center">
                                                            <span class="text-muted fw-semibold d-block mb-1">Ngày kích hoạt mua</span>
                                                            <span class="fs-5 fw-bold text-dark"><i class="fa-solid fa-calendar-check text-primary me-1"></i> <?=ngay($api_site['ngay_mua']);?></span>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="p-5 rounded-3 bg-light-danger border border-dashed border-danger text-center">
                                                            <span class="text-muted fw-semibold d-block mb-1">Thời hạn hết hạn</span>
                                                            <span class="fs-5 fw-bold text-danger"><i class="fa-solid fa-calendar-xmark text-danger me-1"></i> <?=ngay($api_site['ngay_het']);?></span>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="info-display-group mb-6 py-4">
                                                    <span class="fs-6 fw-bold text-gray-700"><i class="fa-solid fa-hand-holding-dollar text-success me-1"></i> Chi phí gia hạn cố định:</span>
                                                    <span class="fs-5 fw-black text-success"><?=tien($loai_site['gia_han']);?>đ / tháng</span>
                                                </div>

                                                <div class="mb-8">
                                                    <label class="form-label fw-bold text-gray-800 mb-2">Chọn thời hạn muốn gia hạn thêm:</label>
                                                    <select name="giahan" id="giahan" class="form-select form-select-solid form-select-lg premium-input">
                                                        <option value="1">Gia hạn 1 tháng</option>
                                                        <option value="2">Gia hạn 2 tháng</option>
                                                        <option value="3">Gia hạn 3 tháng</option>
                                                    </select>
                                                </div>

                                                <div class="d-flex justify-content-end">
                                                    <button class="btn btn-premium-action" id="kt_account_profile_details_submit" type="button" onclick="giahan()">
                                                        <span id="button1" class="indicator-label"><i class="fa-solid fa-bolt me-1"></i> Xác nhận gia hạn</span>
                                                        <span id="button2" class="indicator-progress" style="display: none;">
                                                            Đang thiết lập... <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
                                                        </span>
                                                    </button>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                    
                                </div>
                            </div>

                        </div>
                    </div>

                    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/foot.php';?>
                </div>
            </div>
            
        </div>
    </div>

    <script>
    /* Sửa hàm đổi tab mượt và chính xác ID nút bấm */
    function showitem1() {
        document.getElementById("item1").style.display = "block";
        document.getElementById("item2").style.display = "none";
        document.getElementById("btn1").classList.add("active-tab");
        document.getElementById("btn4").classList.remove("active-tab");
    }

    function showitem2() {
        document.getElementById("item1").style.display = "none";
        document.getElementById("item2").style.display = "block";
        document.getElementById("btn1").classList.remove("active-tab");
        document.getElementById("btn4").classList.add("active-tab");
    }

    /* Sao chép tài khoản Admin */
    function copytk_admin() {
        var username = "<?=$api_site['user_admin'];?>";
        var tempInput = document.createElement("input");
        tempInput.setAttribute("value", username);
        document.body.appendChild(tempInput);
        tempInput.select();
        document.execCommand("copy");
        document.body.removeChild(tempInput);

        var copy = document.getElementById("copy-tk-btn");
        copy.classList.remove("fa-copy", "fa-regular");
        copy.classList.add("fa-solid", "fa-circle-check", "text-success");
        setTimeout(function() {
            copy.classList.remove("fa-solid", "fa-circle-check", "text-success");
            copy.classList.add("fa-regular", "fa-copy");
        }, 1200);
    }

    /* Ẩn/Hiện mật khẩu admin */
    function showpass_admin() {
        var password = document.getElementById("password");
        var toggle = document.getElementById("toggle-password");

        if (password.innerHTML === "******") {
            password.innerHTML = "<?=$api_site['pass_admin'];?>";
            toggle.classList.remove("fa-eye-slash");
            toggle.classList.add("fa-eye", "text-primary");
        } else {
            password.innerHTML = "******";
            toggle.classList.remove("fa-eye", "text-primary");
            toggle.classList.add("fa-eye-slash");
        }
    }

    /* Sao chép mật khẩu admin */
    function copypass_admin() {
        var password = document.getElementById("password");
        var copy = document.getElementById("copy-password");

        var tempInput = document.createElement("input");
        var passwordValue = password.innerHTML === "******" ? "<?=$api_site['pass_admin'];?>" : password.innerHTML;
        tempInput.setAttribute("value", passwordValue);
        document.body.appendChild(tempInput);
        tempInput.select();
        document.execCommand("copy");
        document.body.removeChild(tempInput);

        copy.classList.remove("fa-copy", "fa-regular");
        copy.classList.add("fa-solid", "fa-circle-check", "text-success");
        setTimeout(function() {
            copy.classList.remove("fa-solid", "fa-circle-check", "text-success");
            copy.classList.add("fa-regular", "fa-copy");
        }, 1200);
    }

    /* Xử lý thanh toán gửi lệnh AJAX lên cổng giahan.php */
    function giahan() {
        const button1 = document.getElementById("button1");
        const button2 = document.getElementById("button2");

        button1.style.display = "none";
        button2.style.display = "inline-block";
        button2.disabled = true;

        const username = "<?=$username;?>";
        const id_web = "<?=$_GET['id'];?>";
        const giahan = document.getElementById("giahan").value;

        Swal.fire({
            title: 'Xác nhận gia hạn',
            text: "Hệ thống sẽ khấu trừ số dư tương ứng với gói tháng đã chọn. Tiến hành ngay?",
            icon: 'question',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Đồng ý',
            cancelButtonText: 'Hủy'
        }).then((result) => {
            if (result.isConfirmed) {
                const xhr = new XMLHttpRequest();
                xhr.open("POST", "/ajax/taoweb/giahan.php");
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.onload = function() {
                    button1.style.display = "inline-block";
                    button2.style.display = "none";
                    button2.disabled = false;

                    if (xhr.status === 200) {
                        const response = JSON.parse(xhr.responseText);
                        if (response.success) {
                            Swal.fire({
                                icon: "success",
                                title: "Gia hạn thành công!",
                                text: "Hệ thống đang cập nhật lại hạn dùng cho Website của bạn.",
                            }).then(function() {
                                location.reload();
                            });
                        } else {
                            Swal.fire({
                                icon: "error",
                                title: "Gia hạn thất bại",
                                text: response.message,
                            });
                        }
                    } else {
                        Swal.fire({
                            icon: "error",
                            title: "Lỗi kết nối",
                            text: "Error: " + xhr.statusText,
                        });
                    }
                };
                xhr.onerror = function() {
                    button1.style.display = "inline-block";
                    button2.style.display = "none";
                    button2.disabled = false;

                    Swal.fire({
                        icon: "error",
                        title: "Lỗi đường truyền",
                        text: "Error: " + xhr.statusText,
                    });
                };
                xhr.send(
                    "username=" + encodeURIComponent(username) +
                    "&id_web=" + encodeURIComponent(id_web) +
                    "&giahan=" + encodeURIComponent(giahan)
                );
            } else {
                button1.style.display = "inline-block";
                button2.style.display = "none";
                button2.disabled = false;
            }
        });
    }
    </script>
</body>
</html>
