<?php
session_start();
require_once('../includes/vendor-security.php');
include_once('../../includes/config.php');

$vendorId = $_SESSION['vendor_id'];

// Get earnings summary
$earningsSummary = mysqli_query($con, "SELECT 
    COALESCE(SUM(vendor_earning), 0) as total_earnings,
    COALESCE(SUM(CASE WHEN status='pending' THEN vendor_earning ELSE 0 END), 0) as pending_earnings,
    COALESCE(SUM(CASE WHEN status='paid' THEN vendor_earning ELSE 0 END), 0) as paid_earnings,
    COUNT(*) as total_orders
    FROM vendor_earnings 
    WHERE vendor_id='$vendorId'");

$summary = mysqli_fetch_array($earningsSummary);

// Get recent earnings
$recentEarnings = mysqli_query($con, "SELECT ve.*, o.orderNumber, p.productName 
                                      FROM vendor_earnings ve
                                      JOIN orders o ON ve.order_id = o.id
                                      JOIN products p ON ve.product_id = p.id
                                      WHERE ve.vendor_id='$vendorId'
                                      ORDER BY ve.created_at DESC
                                      LIMIT 20");
?>

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

<head>
    <meta charset="utf-8" />
    <title>Earnings Dashboard - Vendor Portal</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="../../__admin__/css/styles.css" rel="stylesheet" />
    <link href="../../__admin__/css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('../includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('../includes/sidebar.php'); ?>

        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-money-bill-wave me-2"></i> Earnings Dashboard</h1>
                            <p>Track your sales and earnings</p>
                        </div>
                    </div>

                    <!-- Earnings Stats -->
                    <div class="row mb-4">
                        <div class="col-md-4 mb-3">
                            <div class="stat-card">
                                <div class="stat-icon success">
                                    <i class="fas fa-dollar-sign"></i>
                                </div>
                                <div class="stat-value">₵
                                    <?php echo number_format($summary['total_earnings'], 2); ?>
                                </div>
                                <div class="stat-label">Total Earnings</div>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="stat-card">
                                <div class="stat-icon warning">
                                    <i class="fas fa-clock"></i>
                                </div>
                                <div class="stat-value">₵
                                    <?php echo number_format($summary['pending_earnings'], 2); ?>
                                </div>
                                <div class="stat-label">Pending Payout</div>
                                <a class="stat-link" href="payouts.php">Request Payout →</a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="stat-card">
                                <div class="stat-icon info">
                                    <i class="fas fa-check-circle"></i>
                                </div>
                                <div class="stat-value">₵
                                    <?php echo number_format($summary['paid_earnings'], 2); ?>
                                </div>
                                <div class="stat-label">Paid Out</div>
                            </div>
                        </div>
                    </div>

                    <!-- Commission Info -->
                    <div class="alert alert-info mb-4">
                        <i class="fas fa-info-circle me-2"></i>
                        <strong>Commission Rate:</strong>
                        <?php echo $VENDOR_INFO['commission_rate']; ?>%
                        | You earn
                        <?php echo (100 - $VENDOR_INFO['commission_rate']); ?>% on each sale
                    </div>

                    <!-- Recent Earnings Table -->
                    <div class="table-card">
                        <div class="card-header">
                            <h5 class="mb-0"><i class="fas fa-list me-2"></i>Earnings History</h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-hover" id="datatablesSimple">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Order No.</th>
                                        <th>Product</th>
                                        <th>Qty</th>
                                        <th>Order Amount</th>
                                        <th>Commission</th>
                                        <th>Your Earning</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $cnt = 1;
                                    while ($earning = mysqli_fetch_array($recentEarnings)) {
                                        $statusClass = 'warning';
                                        if ($earning['status'] == 'paid')
                                            $statusClass = 'success';
                                        elseif ($earning['status'] == 'processing')
                                            $statusClass = 'info';
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $cnt; ?>
                                            </td>
                                            <td><strong>
                                                    <?php echo htmlentities($earning['orderNumber']); ?>
                                                </strong></td>
                                            <td>
                                                <?php echo htmlentities($earning['productName']); ?>
                                            </td>
                                            <td>
                                                <?php echo $earning['quantity']; ?>
                                            </td>
                                            <td>₵
                                                <?php echo number_format($earning['order_amount'], 2); ?>
                                            </td>
                                            <td>
                                                <span class="text-danger">-₵
                                                    <?php echo number_format($earning['commission_amount'], 2); ?>
                                                </span>
                                                <small class="text-muted">(
                                                    <?php echo $earning['commission_rate']; ?>%)
                                                </small>
                                            </td>
                                            <td><strong class="text-success">₵
                                                    <?php echo number_format($earning['vendor_earning'], 2); ?>
                                                </strong></td>
                                            <td><span class="badge bg-<?php echo $statusClass; ?>">
                                                    <?php echo ucfirst($earning['status']); ?>
                                                </span></td>
                                            <td>
                                                <?php echo date('M d, Y', strtotime($earning['created_at'])); ?>
                                            </td>
                                        </tr>
                                        <?php
                                        $cnt++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>
            </main>
            <?php include_once('../includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../../__admin__/js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
    <script src="../../__admin__/js/datatables-simple-demo.js"></script>
</body>

</html>