<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/Language.php';

if (!isLoggedIn() || $_SESSION['user_type'] !== 'seller') { redirect('../login.php'); }

$lang = new Language($db);
$current_lang = $lang->getCurrentLanguage();
$user_id = $_SESSION['user_id'];
$company_logo = getSetting('company_logo', '../assets/images/logo.png');

$total_revenue = $db->select("SELECT SUM(amount) as total FROM orders WHERE seller_id = ? AND status = 'Completed'", [$user_id])[0]['total'] ?? 0;
$pending_revenue = $db->select("SELECT SUM(amount) as total FROM orders WHERE seller_id = ? AND status = 'Pending'", [$user_id])[0]['total'] ?? 0;

$total_orders = count($db->select("SELECT id FROM orders WHERE seller_id = ?", [$user_id]));
$completed_orders = count($db->select("SELECT id FROM orders WHERE seller_id = ? AND status = 'Completed'", [$user_id]));

$recent_orders = $db->select("SELECT o.*, u.name as customer_name FROM orders o JOIN users u ON o.user_id = u.id WHERE o.seller_id = ? ORDER BY o.created_at DESC", [$user_id]);
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>">
<head>
    <meta charset="UTF-8">
    <title>My Reports - Seller</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style> body { font-family: 'Inter', sans-serif; } </style>
</head>
<?php
/* ===== Premium shared layout (Navy + Gold) ===== */
require_once __DIR__ . '/../includes/theme.php';
$__scats = [];
if (isset($categories)) { $__ids = isset($seller_cat_ids) ? $seller_cat_ids : null; foreach ($categories as $__c) { if ($__ids === null || in_array($__c['id'], $__ids)) $__scats[] = $__c; } }
$__menu = theme_seller_menu($__scats);
theme_open([
    'panel'  => 'seller',
    'base'   => '../',
    'title'  => 'My Reports',
    'active' => 'reports',
    'user'   => isset($user) ? $user : ['name' => 'User', 'mobile' => ''],
    'wallet' => isset($wallet_balance) ? $wallet_balance : 0,
    'notifs' => isset($unread_notifs) ? $unread_notifs : [],
    'logo'   => isset($company_logo) ? $company_logo : '../assets/images/logo.png',
    'brand'  => isset($company_name) ? $company_name : 'WBMCZ',
    'menu'   => $__menu,
    'state'  => '',
]);
?>

        <main class="flex-1 overflow-y-auto p-8">
            
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
                <div class="bg-teal-600 rounded-2xl p-6 text-white shadow-md">
                    <p class="text-xs uppercase font-bold mb-1 opacity-80">Total Earnings</p>
                    <h3 class="text-2xl font-extrabold">₹<?php echo number_format($total_revenue, 2); ?></h3>
                </div>
                <div class="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
                    <p class="text-xs uppercase font-bold text-gray-500 mb-1">Pending Payouts</p>
                    <h3 class="text-2xl font-extrabold text-yellow-600">₹<?php echo number_format($pending_revenue, 2); ?></h3>
                </div>
                <div class="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
                    <p class="text-xs uppercase font-bold text-gray-500 mb-1">Total Orders Received</p>
                    <h3 class="text-2xl font-extrabold text-slate-800"><?php echo $total_orders; ?></h3>
                </div>
                <div class="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
                    <p class="text-xs uppercase font-bold text-gray-500 mb-1">Successfully Completed</p>
                    <h3 class="text-2xl font-extrabold text-blue-600"><?php echo $completed_orders; ?></h3>
                </div>
            </div>

            <div class="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="p-4 bg-gray-50 border-b border-gray-200 font-bold text-gray-700">Detailed Transaction Log</div>
                <table class="w-full text-left text-sm">
                    <thead>
                        <tr class="text-gray-500 uppercase bg-gray-50 border-b">
                            <th class="p-4">Date</th>
                            <th class="p-4">Customer Name</th>
                            <th class="p-4">Service</th>
                            <th class="p-4 text-right">Amount</th>
                            <th class="p-4 text-right">Status</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php if(empty($recent_orders)): ?>
                            <tr><td colspan="5" class="p-8 text-center text-gray-500">No orders to display yet.</td></tr>
                        <?php else: foreach($recent_orders as $o): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="p-4 text-gray-500 font-mono text-xs"><?php echo date('d M Y, h:i A', strtotime($o['created_at'])); ?></td>
                            <td class="p-4 font-bold text-gray-800"><?php echo htmlspecialchars($o['customer_name']); ?></td>
                            <td class="p-4 text-teal-600 font-medium"><?php echo htmlspecialchars($o['service_name']); ?></td>
                            <td class="p-4 text-right font-bold">₹<?php echo number_format($o['amount'], 2); ?></td>
                            <td class="p-4 text-right font-bold text-xs uppercase tracking-wider">
                                <span class="px-2 py-1 rounded <?php echo $o['status']=='Completed'?'bg-emerald-100 text-emerald-700':($o['status']=='Pending'?'bg-yellow-100 text-yellow-700':'bg-rose-100 text-rose-700'); ?>"><?php echo $o['status']; ?></span>
                            </td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </main>
    </div>
</body>
</html>