<!DOCTYPE html>
<html lang="vi">
<!--begin::Head-->
<head>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/head2.php';?>
    <title>Nạp Tiền Qua ATM / Ngân Hàng Tự Động | <?=$lxtdev['ten_web'];?></title>
    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/nav.php';?>
    
    <style>
        .bank-card {
            border: 1px solid rgba(225, 230, 239, 0.7);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .bank-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05) !important;
            border-color: #009ef7;
        }
        .copy-badge {
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .copy-badge:active {
            transform: scale(0.95);
        }
    </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">Nạp Tiền Qua Ngân Hàng</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">Phương thức ATM</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" style="font-family: 'Plus Jakarta Sans', sans-serif;">
                                
                                <!-- BẢN TIN NỘI DUNG CHUYỂN KHOẢN TỔNG -->
                                <div class="card card-flush shadow-sm border-0 mb-8 rounded-4 bg-light-primary border-primary border-opacity-20">
                                    <div class="card-body p-6 d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-4">
                                        <div class="d-flex align-items-center gap-4">
                                            <div class="symbol symbol-50px bg-white rounded-circle d-flex align-items-center justify-content-center shadow-sm p-3">
                                                <i class="ki-duotone ki-information-2 fs-1 text-primary"><span class="path1"></span><span class="path2"></span><span class="path3"></span></i>
                                            </div>
                                            <div>
                                                <h4 class="fw-boldest text-gray-900 m-0 fs-5 mb-1 text-uppercase tracking-wide">Nội dung chuyển khoản bắt buộc</h4>
                                                <p class="text-gray-600 fs-7 m-0">Hệ thống quét hóa đơn tự động bằng AI, điền chính xác nội dung phía dưới để được cộng tiền sau 10 giây.</p>
                                            </div>
                                        </div>
                                        <div class="bg-white px-6 py-4 rounded-4 border border-dashed border-primary d-flex align-items-center justify-content-between min-w-md-250px shadow-sm">
                                            <span id="content_copy" class="font-monospace fw-boldest fs-4 text-primary tracking-widest">
                                                <?php echo ($username != "") ? $lxtdev['cuphap'].$user['id'] : 'CHƯA ĐĂNG NHẬP'; ?>
                                            </span>
                                            <button class="btn btn-icon btn-sm btn-light-primary rounded-3 ms-4 copy-badge" onclick="copyTransferContent()" title="Sao chép nội dung">
                                                <i id="copy_icon" class="ki-duotone ki-copy fs-4"><span class="path1"></span><span class="path2"></span></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <!-- DANH SÁCH NGÂN HÀNG HỖ TRỢ -->
                                <h3 class="fw-boldest text-gray-900 text-uppercase tracking-wider fs-6 mb-6">
                                    <i class="ki-duotone ki-bank text-gray-500 me-2 fs-4"><span class="path1"></span><span class="path2"></span></i>Danh sách cổng thanh toán trực tuyến
                                </h3>

                                <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-6 mb-10">
                                    <?php
                                    $result = mysqli_query($ketnoi, "SELECT * FROM `ds_bank` WHERE `status`='ON'");
                                    if(mysqli_num_rows($result) > 0) {
                                        while($row = mysqli_fetch_assoc($result)) { 
                                            $cuphap_quet = ($username != "") ? $lxtdev['cuphap'].$user['id'] : 'CHUA DANG NHAP';
                                    ?>
                                    <div class="col">
                                        <div class="card h-100 bank-card shadow-sm border-0 rounded-4 bg-white overflow-hidden">
                                            <div class="p-6 d-flex align-items-center justify-content-between border-bottom border-gray-100 bg-light bg-opacity-40">
                                                <div class="d-flex align-items-center gap-3">
                                                    <span class="badge badge-light-primary text-primary font-monospace fw-boldest px-3 py-1.5 rounded-3 border border-primary border-opacity-10 text-uppercase fs-7"><?=$row['type'];?></span>
                                                </div>
                                                <span class="text-muted fs-8 font-monospace fw-semibold">Min: <strong class="text-gray-800"><?=tien($row['min']);?>đ</strong></span>
                                            </div>
                                            
                                            <div class="card-body p-6 d-flex flex-column align-items-center text-center">
                                                <!-- VietQR Dynamic Endpoint V2 -->
                                                <div class="bg-light p-3 rounded-4 mb-4 border border-dashed border-gray-300 position-relative group">
                                                    <img src="https://api.vietqr.io/<?=$row['qr_code'];?>/<?=$row['taikhoan'];?>/<?=$row['min'];?>/<?=$cuphap_quet;?>/qr_only.png?accountName=<?=urlencode($row['name']);?>" 
                                                         alt="VietQR Auto Payment" 
                                                         class="img-fluid rounded-3" 
                                                         style="width: 140px; height: 140px; object-fit: contain;">
                                                </div>

                                                <div class="w-100 text-start mt-2">
                                                    <div class="d-flex justify-content-between align-items-center mb-2.5 border-bottom border-dashed border-gray-200 pb-2">
                                                        <span class="text-muted fs-7 fw-medium">Chủ tài khoản:</span>
                                                        <span class="text-gray-900 fw-boldest text-uppercase fs-7 font-monospace"><?=$row['name'];?></span>
                                                    </div>
                                                    <div class="d-flex justify-content-between align-items-center mb-2.5 border-bottom border-dashed border-gray-200 pb-2">
                                                        <span class="text-muted fs-7 fw-medium">Số tài khoản:</span>
                                                        <div class="d-flex align-items-center gap-1">
                                                            <span class="text-gray-900 font-monospace fw-boldest fs-6"><?=$row['taikhoan'];?></span>
                                                        </div>
                                                    </div>
                                                    <div class="p-3 bg-light rounded-3 mt-3">
                                                        <span class="text-muted fs-8 fw-semibold d-block text-uppercase mb-1">Ghi chú cổng:</span>
                                                        <span class="text-gray-700 fw-medium fs-7 d-block"><?=$row['noidung'];?></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php 
                                        }
                                    } else { 
                                    ?>
                                    <div class="col-12">
                                        <div class="alert alert-dismissible bg-light-warning d-flex flex-column flex-sm-row p-5 rounded-4 border-dashed border-warning">
                                            <div class="d-flex flex-column pe-0 pe-sm-10">
                                                <h5 class="fw-boldest text-warning text-uppercase fs-6">Hệ thống đang bảo trì</h5>
                                                <span class="fs-7 text-gray-700">Hiện tại không có cổng ngân hàng nào hoạt động. Vui lòng quay lại sau ít phút hoặc liên hệ Admin.</span>
                                            </div>
                                        </div>
                                    </div>
                                    <?php } ?>
                                </div>

                                <!-- NHẬT KÝ LỊCH SỬ NẠP TIỀN -->
                                <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ý giao dịch ngân hàng</span>
                                            <span class="text-muted mt-1 fw-semibold fs-8">Hệ thống cập nhật tự động 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-100px text-center">Ngân Hàng</th>
                                                        <th class="min-w-180px">Mã Giao Dịch (FT)</th>
                                                        <th class="min-w-180px">Tên Người Chuyển</th>
                                                        <th class="min-w-130px text-end">Số Tiền 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 Khớp</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $i = 1;
                                                    $result = mysqli_query($ketnoi, "SELECT * FROM `history_nap_bank` WHERE `username` = '$username' ORDER BY `id` DESC");
                                                    if(mysqli_num_rows($result) > 0) {
                                                        while($row2 = 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 class="text-center"><span class="badge badge-light-secondary font-monospace text-uppercase px-3 py-1 rounded-pill fw-bold border border-gray-300 text-gray-800"><?=$row2['type'];?></span></td>
                                                        <td><span class="font-monospace text-gray-900 fw-boldest"><?=htmlentities($row2['trans_id']);?></span></td>
                                                        <td><span class="text-gray-800 text-uppercase fs-7 font-monospace fw-semibold"><?=htmlentities($row2['ctk']);?></span></td>
                                                        <td class="text-end font-monospace text-success fw-boldest"><?=tien($row2['thucnhan']);?>đ</td>
                                                        <td class="text-center">
                                                            <?=napthe($row2['status']);?>
                                                        </td>
                                                        <td class="text-end font-monospace text-muted pe-7"><?=ngay($row2['time']);?></td>
                                                    </tr>
                                                    <?php 
                                                        } 
                                                    } else {
                                                    ?>
                                                    <tr>
                                                        <td colspan="7" class="text-center py-12 text-muted fw-semibold fs-6">Không tìm thấy bất kỳ lịch sử chuyển khoản nào của tài khoản này.</td>
                                                    </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>
                        <!--end::Content-->
                        
                    </div>
                    <?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/foot.php';?>
                </div>
            </div>
        </div>
    </div>

    <!-- SCRIPT COPY CHUẨN ĐỒNG BỘ HIỆN ĐẠI -->
    <script>
    function copyTransferContent() {
        const textToCopy = document.getElementById("content_copy").innerText.trim();
        const copyIcon = document.getElementById("copy_icon");

        if(textToCopy === "CHƯA ĐĂNG NHẬP") {
            Swal.fire({ icon: "error", text: "Vui lòng đăng nhập hệ thống để lấy cú pháp chuyển khoản!", buttonsStyling: !1, customClass: { confirmButton: "btn btn-danger px-6 rounded-pill fw-boldest fs-7" } });
            return;
        }

        // Sử dụng API Clipboard không đồng bộ chuẩn W3C thay thế execCommand
        navigator.clipboard.writeText(textToCopy).then(() => {
            copyIcon.className = "ki-duotone ki-check-circle fs-4 text-success"><span class="path1"></span><span class="path2"></span>;
            
            Swal.fire({
                icon: "success",
                text: "Đã sao chép nội dung chuyển khoản tự động!",
                toast: true,
                position: "top-end",
                showConfirmButton: false,
                timer: 2000,
                timerProgressBar: true
            });

            setTimeout(() => {
                copyIcon.className = "ki-duotone ki-copy fs-4"><span class="path1"></span><span class="path2"></span>;
            }, 1500);
        }).catch(err => {
            console.error("Lỗi khi sao chép nội dung: ", err);
        });
    }
    </script>
</body>
</html>
