<!DOCTYPE html>
<html lang="vi">
<!--begin::Head-->
<head>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/head2.php';?>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/head.php';?>
    <title>Nạp Thẻ Cào Tự Động | <?=$lxtdev['ten_web'];?></title>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/nav.php';?>
    
    <style>
        .premium-input-group {
            border: 1px solid rgba(225, 230, 239, 0.8) !important;
            transition: all 0.3s ease;
        }
        .premium-input-group:focus-within {
            border-color: #009ef7 !important;
            box-shadow: 0 0 10px rgba(0, 158, 247, 0.1) !important;
        }
    </style>
</head>
<!--end::Head-->

<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">
    
    <!--begin::Theme mode setup on page load-->
    <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>
    <!--end::Theme mode setup on page load-->

    <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 class="app-main flex-column flex-row-fluid" id="kt_app_main">
                    <div class="d-flex flex-column flex-column-fluid">
                        
                        <!--begin::Toolbar-->
                        <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-gray-900 fw-boldest fs-3 flex-column justify-content-center my-0 tracking-tight text-uppercase">Cổng Nạp Thẻ Cào</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">Trang chủ</a></li>
                                        <li class="breadcrumb-item"><span class="bullet bg-gray-400 w-5px h-2px"></span></li>
                                        <li class="breadcrumb-item text-muted">Nạp tiền số dư</li>
                                    </ul>
                                </div>
                                <div class="d-flex align-items-center gap-2 gap-lg-3">
                                    <div class="bg-white rounded-pill d-flex align-items-center p-1 shadow-sm border">
                                        <span class="badge badge-primary font-monospace fw-boldest px-4 py-2.5 rounded-pill fs-7"><?=tien($user['money']);?>đ</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--end::Toolbar-->

                        <!--begin::Content-->
                        <div id="kt_app_content" class="app-content flex-column-fluid">
                            <div id="kt_app_content_container" class="app-container container-fluid">
                                
                                <div class="row g-6 g-xl-9" style="font-family: 'Plus Jakarta Sans', sans-serif;">
                                    
                                    <!-- CỘT TRÁI: FORM NẠP THẺ -->
                                    <div class="col-12 col-xl-8">
                                        <div class="card card-flush shadow-sm rounded-4 border-0 bg-white">
                                            <div class="card-header align-items-center py-5 px-7 border-0">
                                                <div class="card-title d-flex flex-column">
                                                    <h3 class="fw-boldest text-uppercase tracking-wide fs-5 m-0 text-gray-900">
                                                        <i class="ki-duotone ki-credit-cart text-primary me-2 fs-3"><span class="path1"></span><span class="path2"></span></i>Khởi tạo lệnh gạch thẻ
                                                    </h3>
                                                    <span class="text-muted fs-8 mt-1">Vui lòng nhập chính xác thông tin để tránh bị phạt trừ hoặc nuốt thẻ từ phía nhà mạng</span>
                                                </div>
                                            </div>
                                            
                                            <div class="card-body px-7 py-0 row g-5">
                                                <div class="col-md-6 flex-column mb-3">
                                                    <label class="required fw-boldest text-gray-700 fs-7 text-uppercase tracking-wide mb-2">Loại nhà mạng</label>
                                                    <select name="type_the" id="type_the" class="form-select form-select-solid form-select-lg rounded-3 fw-bold fs-6 border premium-input-group">
                                                        <option value="">-- Chọn loại thẻ cào --</option>
                                                        <option value="VIETTEL">Viettel (Auto)</option>
                                                        <option value="VINAPHONE">Vinaphone (Auto)</option>
                                                        <option value="MOBIFONE">Mobifone (Auto)</option>
                                                        <option value="VNMOBI">Vietnamobile</option>
                                                        <option value="ZING">Thẻ Zing</option>
                                                        <option value="GARENA">Thẻ Garena</option>
                                                        <option value="GATE">Thẻ GATE</option>
                                                        <option value="VCOIN">Thẻ Vcoin</option>
                                                    </select>
                                                </div>

                                                <div class="col-md-6 flex-column mb-3">
                                                    <label class="required fw-boldest text-gray-700 fs-7 text-uppercase tracking-wide mb-2">Mệnh giá khai báo</label>
                                                    <select name="menh_gia" id="menh_gia" class="form-select form-select-solid form-select-lg rounded-3 fw-bold font-monospace fs-6 border premium-input-group">
                                                        <option value="">-- Chọn mệnh giá --</option>
                                                        <option value="10000">10,000 đ</option>
                                                        <option value="20000">20,000 đ</option>
                                                        <option value="30000">30,000 đ</option>
                                                        <option value="50000">50,000 đ</option>
                                                        <option value="100000">100,000 đ</option>
                                                        <option value="200000">200,000 đ</option>
                                                        <option value="300000">300,000 đ</option>
                                                        <option value="500000">500,000 đ</option>
                                                    </select>
                                                </div>

                                                <div class="col-md-6 flex-column mb-3">
                                                    <label class="required fw-boldest text-gray-700 fs-7 text-uppercase tracking-wide mb-2">Mã số thẻ (PIN)</label>
                                                    <div class="position-relative d-flex align-items-center">
                                                        <i class="ki-duotone ki-key position-absolute ms-4 fs-3 text-gray-400"><span class="path1"></span><span class="path2"></span></i>
                                                        <input type="text" name="ma_the" id="ma_the" class="form-control form-control-lg form-control-solid ps-12 rounded-3 fw-bold font-monospace border premium-input-group" placeholder="Nhập dãy số sau lớp cào..." />
                                                    </div>
                                                </div>

                                                <div class="col-md-6 flex-column mb-6">
                                                    <label class="required fw-boldest text-gray-700 fs-7 text-uppercase tracking-wide mb-2">Số Serial (Seri)</label>
                                                    <div class="position-relative d-flex align-items-center">
                                                        <i class="ki-duotone ki-barcode position-absolute ms-4 fs-3 text-gray-400"><span class="path1"></span><span class="path2"></span><span class="path3"></span></i>
                                                        <input type="text" name="seri_the" id="seri_the" class="form-control form-control-lg form-control-solid ps-12 rounded-3 fw-bold font-monospace border premium-input-group" placeholder="Nhập mã số seri in ngoài thẻ..." />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="card-footer d-flex justify-content-end py-5 px-7 border-0 bg-light bg-opacity-30 rounded-bottom-4">
                                                <button id="napthe" class="btn btn-primary px-6 rounded-3 fw-boldest fs-6 hover-elevate-up" onclick="nap_the()">
                                                    <span id="button1" class="indicator-label">Xác Nhận Nạp Ngay</span>
                                                    <span id="button2" class="indicator-progress" style="display: none;">
                                                        Đang Kiểm Tra Core API... <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
                                                    </span>
                                                </button>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- CỘT PHẢI: LƯU Ý & TỶ GIÁ -->
                                    <div class="col-12 col-xl-4">
                                        <div class="card card-flush shadow-sm rounded-4 border-0 h-100 bg-white">
                                            <div class="card-header align-items-center py-5 px-7 border-0">
                                                <h3 class="fw-boldest text-uppercase tracking-wide fs-6 m-0 text-danger">
                                                    <i class="ki-duotone ki-information-2 text-danger me-2 fs-4"><span class="path1"></span><span class="path2"></span><span class="path3"></span></i>Điều khoản gạch thẻ
                                                </h3>
                                            </div>
                                            <div class="card-body px-7 pt-0 pb-6 fs-7 text-gray-600">
                                                <div class="bg-light-danger border border-danger border-opacity-20 rounded-3 p-4 mb-4">
                                                    <span class="fw-boldest text-danger text-uppercase tracking-wider fs-9 d-block mb-1">CẢNH BÁO SAI MỆNH GIÁ:</span>
                                                    Khai báo <strong class="text-danger">Sai Mệnh Giá</strong> sẽ bị phạt mất <strong class="text-dark">50%</strong> hoặc <strong class="text-dark">100%</strong> giá trị thực nhận tùy thuộc vào quy định điều phối nhà mạng viễn thông.
                                                </div>
                                                <ul class="list-unstyled d-flex flex-column gap-3 mb-0">
                                                    <li class="d-flex align-items-start gap-2">
                                                        <i class="ki-duotone ki-check text-success fs-5 mt-0.5"><span class="path1"></span></i> Thẻ lỗi hoặc đã qua sử dụng sẽ bị hủy bỏ.
                                                    </li>
                                                    <li class="d-flex align-items-start gap-2">
                                                        <i class="ki-duotone ki-check text-success fs-5 mt-0.5"><span class="path1"></span></i> Thời gian khớp thẻ tự động từ 30 giây đến 3 phút.
                                                    </li>
                                                    <li class="d-flex align-items-start gap-2">
                                                        <i class="ki-duotone ki-check text-success fs-5 mt-0.5"><span class="path1"></span></i> Nhật ký giao dịch được mã hóa vĩnh viễn bảo mật.
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- DÒNG DƯỚI: BẢNG LỊCH SỬ GIAO DỊCH -->
                                    <div class="col-12">
                                        <div class="card shadow-sm rounded-4 border-0 bg-white">
                                            <div class="card-header align-items-center py-5 px-7 border-0">
                                                <h3 class="card-title d-flex flex-column m-0">
                                                    <span class="fw-boldest text-uppercase tracking-wide fs-5 text-gray-900">Nhật ký xử lý thẻ cào</span>
                                                    <span class="text-muted mt-1 fw-semibold fs-8">Hệ thống cập nhật thời gian thực lúc: <?=$time;?></span>
                                                </h3>
                                            </div>

                                            <div class="card-body p-0">
                                                <div class="table-responsive">
                                                    <table class="table table-row-dashed table-row-gray-200 align-middle gs-7 gy-4 m-0">
                                                        <thead>
                                                            <tr class="fw-boldest text-muted text-uppercase fs-8 tracking-widest bg-light bg-opacity-75 border-bottom border-gray-200">
                                                                <th class="w-60px text-center">STT</th>
                                                                <th class="min-w-150px">Mã Thẻ (Pin)</th>
                                                                <th class="min-w-110px text-center">Nhà Mạng</th>
                                                                <th class="min-w-130px text-end">Mệnh Giá Vốn</th>
                                                                <th class="min-w-130px text-end">Khớp Thực Nhận</th>
                                                                <th class="min-w-140px text-center">Trạng Thái</th>
                                                                <th class="min-w-160px text-end pe-7">Thời Gian Gửi</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php
                                                            $i = 1;
                                                            $result = mysqli_query($ketnoi, "SELECT * FROM `history_nap_the` WHERE `username` = '$username' ORDER BY `id` DESC");
                                                            if (mysqli_num_rows($result) > 0) {
                                                                while($row = mysqli_fetch_assoc($result)) { 
                                                            ?>
                                                            <tr class="fw-bold text-gray-700 fs-7 hover-bg-light hover-bg-opacity-40 transition-all">
                                                                <td class="text-center font-monospace text-muted"><?=$i++;?></td>
                                                                <td><span class="font-monospace text-gray-900 fw-boldest"><?=htmlentities($row['pin']);?></span></td>
                                                                <td class="text-center"><span class="badge badge-light-dark font-monospace text-uppercase px-3 py-1 rounded-pill fw-bold border"><?=htmlentities($row['loaithe']);?></span></td>
                                                                <td class="text-end font-monospace text-gray-800"><?=tien($row['menhgia']);?>đ</td>
                                                                <td class="text-end font-monospace text-success fw-boldest"><?=tien($row['thucnhan']);?>đ</td>
                                                                <td class="text-center">
                                                                    <!-- Giả định cấu trúc hàm napthe() trả ra cấu trúc HTML Badge hợp lệ -->
                                                                    <?=napthe($row['status']);?>
                                                                </td>
                                                                <td class="text-end font-monospace text-muted pe-7"><?=$row['time'];?></td>
                                                            </tr>
                                                            <?php 
                                                                } 
                                                            } else {
                                                            ?>
                                                            <tr>
                                                                <td colspan="7" class="text-center py-10 text-muted fw-semibold fs-6">Bạn chưa có lệnh nạp thẻ cào nào trên hệ thống này.</td>
                                                            </tr>
                                                            <?php } ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!--end::Content-->
                        
                    </div>
                    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/foot.php';?>
                </div>
            </div>
        </div>
    </div>

    <!-- HỆ THỐNG ĐỒNG BỘ JS VÀ BACKEND VIA AJAX FETCH -->
    <script>
    function nap_the() {
        const btn1 = document.getElementById("button1");
        const btn2 = document.getElementById("button2");
        const btnMain = document.getElementById("napthe");

        const type_the = document.getElementById("type_the").value;
        const menh_gia = document.getElementById("menh_gia").value;
        const ma_the = document.getElementById("ma_the").value;
        const seri_the = document.getElementById("seri_the").value;

        if(!type_the || !menh_gia || !ma_the || !seri_the) {
            Swal.fire({ icon: "error", text: "Vui lòng hoàn thiện toàn bộ biểu mẫu khai báo thẻ!", buttonsStyling: !1, customClass: { confirmButton: "btn btn-danger px-6 rounded-pill fw-boldest fs-7" } });
            return;
        }

        Swal.fire({
            title: 'Xác nhận đẩy thẻ lên Core?',
            text: "Nếu sai thông tin mệnh giá cố tình, thẻ sẽ bị phạt hủy hoàn toàn theo cơ chế phân phối!",
            icon: 'warning',
            showCancelButton: true,
            buttonsStyling: false,
            confirmButtonText: 'Tôi đã kiểm tra - Đẩy thẻ',
            cancelButtonText: 'Hủy kiểm tra lại',
            customClass: {
                confirmButton: "btn btn-primary px-6 rounded-pill fw-boldest fs-7 me-3",
                cancelButton: "btn btn-light px-6 rounded-pill fw-boldest fs-7"
            }
        }).then((result) => {
            if (result.isConfirmed) {
                btn1.style.display = "none";
                btn2.style.display = "inline-block";
                btnMain.disabled = true;

                // Nâng cấp lên cấu trúc Fetch API đồng bộ mượt mà
                let formData = new FormData();
                formData.append("username", "<?=$username?>");
                formData.append("type_the", type_the);
                formData.append("menh_gia", menh_gia);
                formData.append("ma_the", ma_the);
                formData.append("seri_the", seri_the);

                fetch("/ajax/naptien/napthe", {
                    method: "POST",
                    body: formData
                })
                .then(response => {
                    if (!response.ok) throw new Error("Kết nối máy chủ Core thất bại.");
                    return response.json();
                })
                .then(data => {
                    btn1.style.display = "inline-block";
                    btn2.style.display = "none";
                    btnMain.disabled = false;

                    if (data.success) {
                        Swal.fire({ icon: "success", text: "Hệ thống đã tiếp nhận thẻ thành công, vui lòng chờ xử lý!", buttonsStyling: !1, customClass: { confirmButton: "btn btn-success px-6 rounded-pill fw-boldest fs-7" } }).then(() => {
                            location.reload();
                        });
                    } else {
                        Swal.fire({ icon: "error", text: data.message, buttonsStyling: !1, customClass: { confirmButton: "btn btn-danger px-6 rounded-pill fw-boldest fs-7" } });
                    }
                })
                .catch(error => {
                    btn1.style.display = "inline-block";
                    btn2.style.display = "none";
                    btnMain.disabled = false;
                    Swal.fire({ icon: "error", text: error.message, buttonsStyling: !1, customClass: { confirmButton: "btn btn-danger px-6 rounded-pill fw-boldest fs-7" } });
                });
            }
        });
    }
    </script>
</body>
</html>
