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

<head>
    <?php require_once('head.php');?>
    <title>AdminLTE 3 | Nhật Ký Giao Dịch Cao Cấp</title>
    <?php require_once('nav.php');?>
    <?php 
    $total_money = mysqli_fetch_assoc($ketnoi->query("SELECT SUM(`money`) FROM `users` WHERE `money` >= 0 AND `level`!=1")) ['SUM(`money`)']; 
    $total_thanhvien = mysqli_fetch_assoc(mysqli_query($ketnoi, "SELECT COUNT(*) FROM `users` ")) ['COUNT(*)']; 
    $total_code = mysqli_fetch_assoc(mysqli_query($ketnoi, "SELECT COUNT(*) FROM `lich_su_mua_code` ")) ['COUNT(*)']; 
    $total_thanhvienbanned = mysqli_fetch_assoc(mysqli_query($ketnoi, "SELECT COUNT(*) FROM `users` WHERE `bannd` = '1' ")) ['COUNT(*)']; 
    ?>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        body { font-family: 'Inter', sans-serif !important; background-color: #f8fafc; }
        .content-wrapper { background: #f8fafc !important; }

        /* Widget thống kê mini trên đỉnh trang */
        .stat-mini-card {
            background: #ffffff;
            border: 1px solid #e2e8f0;
            border-radius: 12px;
            padding: 16px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.02);
            margin-bottom: 24px;
        }
        .stat-mini-card .info h6 { color: #64748b; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; margin: 0 0 4px 0; font-weight: 600; }
        .stat-mini-card .info p { color: #0f172a; font-size: 1.25rem; font-weight: 700; margin: 0; }
        .stat-mini-card .icon-box { width: 44px; height: 44px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; }

        /* Container Card chính chứa bảng */
        .premium-card {
            border: none !important;
            box-shadow: 0 10px 30px rgba(15, 23, 42, 0.04) !important;
            border-radius: 16px !important;
            background: #ffffff;
            overflow: hidden;
            border: 1px solid #e2e8f0 !important;
        }

        /* Thanh điều khiển tìm kiếm ở Card Header */
        .search-box-container {
            display: flex;
            align-items: center;
            background: #f8fafc;
            border: 1px solid #cbd5e1;
            border-radius: 8px;
            padding: 6px 14px;
            width: 300px;
            transition: all 0.2s ease;
        }
        .search-box-container:focus-within {
            border-color: #6366f1;
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
            background: #ffffff;
        }
        .search-box-container input {
            border: none;
            background: transparent;
            font-size: 0.85rem;
            color: #1e293b;
            width: 100%;
            margin-left: 8px;
        }
        .search-box-container input:focus { outline: none; }
        .search-box-container i { color: #94a3b8; }

        /* Thiết kế bảng dữ liệu phong cách tối giản phẳng */
        .premium-table { border-collapse: separate !important; border-spacing: 0 !important; border: none !important; }
        .premium-table thead th {
            background-color: #f8fafc !important;
            color: #475569 !important;
            font-weight: 600 !important;
            text-transform: uppercase;
            font-size: 0.75rem !important;
            letter-spacing: 0.05em;
            padding: 16px 16px !important;
            border: none !important;
            border-bottom: 2px solid #e2e8f0 !important;
        }
        .premium-table tbody td {
            padding: 16px 16px !important;
            vertical-align: middle !important;
            border-bottom: 1px solid #f1f5f9 !important;
            font-size: 0.9rem;
            color: #334155;
        }
        .premium-table tbody tr:hover td { background-color: #f8fafc; }

        .table-link { color: #4f46e5; font-weight: 600; text-decoration: none; }
        .table-link:hover { color: #312e81; text-decoration: underline; }

        /* Định dạng mềm khối trạng thái Badge */
        .badge-soft { padding: 5px 10px; border-radius: 6px; font-weight: 600; font-size: 0.75rem; display: inline-flex; align-items: center; gap: 4px; }
        .badge-soft-success { background-color: #ecfdf5; color: #059669; }
        .badge-soft-warning { background-color: #fffbeb; color: #d97706; }
        .badge-soft-danger { background-color: #fef2f2; color: #ef4444; }

        /* Khu vực phân trang cuối trang */
        .pagination-container { display: flex; justify-content: flex-end; padding: 20px; border-top: 1px solid #f1f5f9; }
        .page-link { border: 1px solid #e2e8f0 !important; border-radius: 6px !important; margin: 0 2px; color: #64748b; padding: 6px 12px; font-size: 0.85rem; font-weight: 500; }
        .page-item.active .page-link { background-color: #4f46e5 !important; border-color: #4f46e5 !important; color: #fff !important; }
    </style>
</head>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <div class="content-wrapper">
            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6">
                            <h1 class="m-0 font-weight-bold" style="color: #0f172a; font-size: 1.5rem;">Báo cáo lịch sử</h1>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="#" class="text-muted">Home</a></li>
                                <li class="breadcrumb-item active">Lịch sử giao dịch</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            <section class="content">
                <div class="container-fluid">
                    
                    <div class="row">
                        <div class="col-md-4">
                            <div class="stat-mini-card">
                                <div class="info">
                                    <h6>Thành viên hệ thống</h6>
                                    <p><?= number_format($total_thanhvien); ?></p>
                                </div>
                                <div class="icon-box" style="background: #e0e7ff; color: #4f46e5;"><i class="fas fa-users"></i></div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stat-mini-card">
                                <div class="info">
                                    <h6>Số lượt tải / Mua code</h6>
                                    <p><?= number_format($total_code); ?></p>
                                </div>
                                <div class="icon-box" style="background: #ecfdf5; color: #10b981;"><i class="fas fa-shopping-cart"></i></div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stat-mini-card">
                                <div class="info">
                                    <h6>Tài khoản bị khóa (Banned)</h6>
                                    <p><?= number_format($total_thanhvienbanned); ?></p>
                                </div>
                                <div class="icon-box" style="background: #fef2f2; color: #ef4444;"><i class="fas fa-user-slash"></i></div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-12">
                            <div class="card premium-card">
                                <div class="card-header bg-white py-3 d-flex align-items-center justify-content-between flex-wrap gap-2">
                                    <h3 class="card-title font-weight-bold mb-0" style="font-size: 1.05rem; color: #0f172a;">
                                        <i class="fas fa-list-alt mr-2 text-indigo" style="color: #4f46e5;"></i> Danh sách hóa đơn mua mã nguồn
                                    </h3>
                                    <div class="search-box-container">
                                        <i class="fas fa-search"></i>
                                        <input type="text" id="tableSearch" placeholder="Tìm kiếm tên, người mua...">
                                    </div>
                                </div>
                                
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table id="datatable1" class="table premium-table text-center mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 80px;">STT</th>
                                                    <th style="text-align: left; min-width: 250px;">Sản phẩm mã nguồn</th>
                                                    <th>Khách hàng</th>
                                                    <th>Thời gian khởi tạo</th>
                                                    <th>Trạng thái giao dịch</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $i = 1;
                                                $sql = "SELECT l.*, k.title AS code_title, k.id AS code_id 
                                                        FROM `lich_su_mua_code` l
                                                        LEFT JOIN `khocode` k ON l.`loaicode` = k.`id`
                                                        ORDER BY l.`id` DESC";
                                                
                                                $result = mysqli_query($ketnoi, $sql);
                                                while($row = mysqli_fetch_assoc($result)) { 
                                                ?>
                                                <tr>
                                                    <td class="font-weight-bold text-muted">#<?= $i++; ?></td>
                                                    <td style="text-align: left;">
                                                        <?php if (!empty($row['code_title'])): ?>
                                                            <a class="table-link" href="/ma-nguon/<?= $row['code_id']; ?>">
                                                                <i class="fas fa-code-branch mr-2 text-muted" style="font-size:0.8rem;"></i><?= htmlspecialchars($row['code_title']); ?>
                                                            </a>
                                                        <?php else: ?>
                                                            <span class="text-danger font-italic"><i class="fas fa-trash-alt mr-1"></i> Liên kết gốc đã bị gỡ</span>
                                                        <?php endif; ?>
                                                    </td>
                                                    <td class="font-weight-bold text-dark">
                                                        <span class="text-secondary"><i class="far fa-user mr-1"></i></span><?= htmlspecialchars($row['username']); ?>
                                                    </td>
                                                    <td class="text-muted" style="font-size: 0.85rem;">
                                                        <?= function_exists('ngay') ? ngay($row['time']) : $row['time']; ?>
                                                    </td>
                                                    <td>
                                                        <?php 
                                                        $status_raw = $row['status'];
                                                        $status_text = function_exists('code') ? code($status_raw) : $status_raw;

                                                        if (strpos(strtolower($status_text), 'thành công') !== false || $status_raw == '1' || strtolower($status_raw) == 'success') {
                                                            echo '<span class="badge-soft badge-soft-success"><span class="dot"></span> ' . htmlspecialchars($status_text) . '</span>';
                                                        } elseif (strpos(strtolower($status_text), 'chờ') !== false || $status_raw == '0' || strtolower($status_raw) == 'pending') {
                                                            echo '<span class="badge-soft badge-soft-warning"><i class="fas fa-hourglass-half fa-spin mr-1"></i> ' . htmlspecialchars($status_text) . '</span>';
                                                        } else {
                                                            echo '<span class="badge-soft badge-soft-danger"><span class="dot"></span> ' . htmlspecialchars($status_text) . '</span>';
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                        
                                        <div id="pagination-container" class="pagination-container"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>

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

        function runPagination(filteredRows) {
            $paginationContainer.empty();
            var totalPages = Math.ceil(filteredRows.length / limitPerPage);

            if (totalPages > 1) {
                var pagination = '<ul class="pagination mb-0">';
                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();
                filteredRows.slice(0, limitPerPage).show();
                $paginationContainer.find('.page-link').first().addClass('active');

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

                    $rows.hide();
                    filteredRows.slice(start, end).show();
                    $paginationContainer.find('.page-link').removeClass('active');
                    $(this).addClass('active');
                });
            } else {
                $paginationContainer.hide();
                $rows.hide();
                filteredRows.show();
            }
        }

        // Khởi chạy phân trang mặc định ban đầu
        runPagination($rows);

        // Bộ lọc tìm kiếm real-time không delay giật trang
        $('#tableSearch').on('keyup', function() {
            var value = $(this).val().toLowerCase();
            
            var $filtered = $rows.filter(function() {
                return $(this).text().toLowerCase().indexOf(value) > -1;
            });

            runPagination($filtered);

            // Nếu tìm kiếm không ra kết quả, báo trống trực quan
            if($filtered.length === 0) {
                if($('#empty-row-msg').length === 0) {
                    $table.find('tbody').append('<tr id="empty-row-msg"><td colspan="5" class="text-muted py-4"><i class="fas fa-folder-open mr-2"></i>Không tìm thấy dữ liệu giao dịch trùng khớp</td></tr>');
                }
            } else {
                $('#empty-row-msg').remove();
            }
        });
    });
    </script>

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