<?php
// 1. Tạo Shortcode [vietcam_admin_dashboard]
add_shortcode('vietcam_admin_dashboard', 'vietcam_admin_dashboard_handler');

function vietcam_admin_dashboard_handler() {
    // Kiểm tra quyền Admin - Chỉ cho phép Lê Triệu Quốc Khánh hoặc Admin truy cập
    if ( !current_user_can('manage_options') ) {
        return '<div style="color:red; padding:20px;">Bạn không có quyền truy cập vào khu vực này.</div>';
    }

    // Lấy thống kê doanh thu WooCommerce
    $total_revenue = wc_format_decimal(array_sum(wc_get_order_report_data(array(
        'data' => array(
            '_line_total' => array(
                'type'     => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name'     => 'total_sales'
            )
        ),
        'where' => array(
            array(
                'key'      => 'post_status',
                'value'    => array('wc-completed', 'wc-processing'),
                'operator' => 'IN'
            )
        ),
        'return_json' => false
    ))->total_sales), 2);

    // Lấy số lượng đơn hàng đang chờ xử lý
    $processing_count = wc_orders_count('processing');

    // Lấy danh sách 5 đơn hàng mới nhất
    $recent_orders = wc_get_orders(array('limit' => 5, 'status' => 'processing'));

    ob_start();
    ?>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap');
        
        .admin-dashboard-wrapper {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background: #f8fafc;
            border-radius: 30px;
            padding: 30px;
            position: relative;
            overflow: hidden;
            color: #1e293b;
        }

        /* Liquid Glass Background */
        .admin-blob {
            position: absolute;
            z-index: 0;
            filter: blur(60px);
            opacity: 0.3;
            border-radius: 50%;
            animation: moveBlob 15s infinite alternate;
        }
        .ab-1 { width: 300px; height: 300px; background: #3b82f6; top: -50px; left: -50px; }
        .ab-2 { width: 250px; height: 250px; background: #a855f7; bottom: -50px; right: -50px; }

        @keyframes moveBlob { from { transform: translate(0,0); } to { transform: translate(50px, 30px); } }

        .admin-glass-card {
            position: relative;
            z-index: 1;
            background: rgba(255, 255, 255, 0.6) !important;
            backdrop-filter: blur(15px);
            border: 1px solid rgba(255, 255, 255, 0.8);
            border-radius: 24px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.03);
        }

        .stat-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 20px;
            border: 1px solid #f1f5f9;
            text-align: center;
        }

        .stat-value { font-size: 1.5rem; font-weight: 700; color: #3b82f6; }
        .stat-label { font-size: 0.8rem; color: #64748b; text-transform: uppercase; margin-top: 5px; }

        .order-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        .order-table th { text-align: left; padding: 12px; border-bottom: 2px solid #f1f5f9; color: #64748b; font-size: 0.85rem; }
        .order-table td { padding: 15px 12px; border-bottom: 1px solid #f1f5f9; font-size: 0.9rem; }

        .btn-approve {
            background: #10b981;
            color: white !important;
            padding: 6px 15px;
            border-radius: 8px;
            text-decoration: none;
            font-size: 0.8rem;
            font-weight: 600;
        }
    </style>

    <div class="admin-dashboard-wrapper">
        <div class="admin-blob ab-1"></div>
        <div class="admin-blob ab-2"></div>

        <div class="admin-glass-card">
            <h2 style="margin-bottom: 20px; font-weight: 700;">Quản lý VIETCAM TEAM - Admin Dashboard</h2>
            
            <div class="stat-grid">
                <div class="stat-card">
                    <div class="stat-value"><?php echo number_format($total_revenue, 0, ',', '.'); ?>đ</div>
                    <div class="stat-label">Tổng doanh thu</div>
                </div>
                <div class="stat-card">
                    <div class="stat-value"><?php echo $processing_count; ?></div>
                    <div class="stat-label">Đơn chờ duyệt</div>
                </div>
                <div class="stat-card">
                    <div class="stat-value">2H</div>
                    <div class="stat-label">Tốc độ hỗ trợ</div>
                </div>
            </div>

            <h3 style="font-size: 1.1rem; font-weight: 600;">Đơn hàng cần duyệt mới nhất</h3>
            <table class="order-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Khách hàng</th>
                        <th>Tổng tiền</th>
                        <th>Thao tác</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ( $recent_orders ) : 
                        foreach ( $recent_orders as $order ) : ?>
                        <tr>
                            <td>#<?php echo $order->get_id(); ?></td>
                            <td><?php echo $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); ?></td>
                            <td><?php echo $order->get_total(); ?>đ</td>
                            <td>
                                <a href="<?php echo admin_url('post.php?post=' . $order->get_id() . '&action=edit'); ?>" class="btn-approve">Xem & Duyệt</a>
                            </td>
                        </tr>
                    <?php endforeach; else : ?>
                        <tr><td colspan="4">Không có đơn hàng nào đang chờ.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php
    return ob_get_clean();
}