<?php
require 'config/config.php';

?>
<!DOCTYPE html>
<html>
<head>
    <title>Fix Overpayments</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
        .success { background: #d4edda; color: #155724; padding: 15px; border-radius: 5px; border: 1px solid #c3e6cb; margin: 15px 0; }
        .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; border: 1px solid #f5c6cb; margin: 15px 0; }
        .info { background: #d1ecf1; color: #0c5460; padding: 15px; border-radius: 5px; border: 1px solid #bee5eb; margin: 15px 0; }
        .warning { background: #fff3cd; color: #856404; padding: 15px; border-radius: 5px; border: 1px solid #ffeeba; margin: 15px 0; }
        table { width: 100%; border-collapse: collapse; margin: 15px 0; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        th { background: #f5f5f5; }
        h1 { color: #333; }
        button { background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; font-size: 16px; }
        button:hover { background: #0056b3; }
        .btn-danger { background: #dc3545; }
        .btn-danger:hover { background: #c82333; }
    </style>
</head>
<body>
    <h1>Fix Overpayments</h1>

    <?php
    try {
        $db = db();
        
        // Find all sales where amount_paid > total
        $stmt = $db->prepare("
            SELECT id, sale_no, total, amount_paid, (amount_paid - total) as overpayment
            FROM sales 
            WHERE amount_paid > total
        ");
        $stmt->execute();
        $overpaidSales = $stmt->fetchAll();
        
        if (empty($overpaidSales)) {
            ?>
            <div class="success">
                <strong>No overpayments found!</strong><br>
                All sales have correct payment amounts.
            </div>
            <?php
        } else {
            $fixedCount = 0;
            
            foreach ($overpaidSales as $sale) {
                // Set amount_paid to total (full payment)
                $updateStmt = $db->prepare("UPDATE sales SET amount_paid = total, payment_status = 'paid' WHERE id = ?");
                $updateStmt->execute([$sale['id']]);
                $fixedCount++;
            }
            
            ?>
            <div class="success">
                <strong>Fixed <?= $fixedCount ?> overpaid sales!</strong><br>
                Set Amount Paid to equal Total for all overpaid records.
            </div>
            <table>
                <thead>
                    <tr>
                        <th>Sale No</th>
                        <th>Total</th>
                        <th>Original Amount Paid</th>
                        <th>Overpayment</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($overpaidSales as $sale): ?>
                    <tr>
                        <td><?= htmlspecialchars($sale['sale_no']) ?></td>
                        <td><?= number_format($sale['total'], 2) ?></td>
                        <td><?= number_format($sale['amount_paid'], 2) ?></td>
                        <td style="color: red; font-weight: bold;"><?= number_format($sale['overpayment'], 2) ?></td>
                        <td style="color: green; font-weight: bold;">Fixed</td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php
        }
        
        // Show current totals after fix
        $stmt = $db->query("SELECT SUM(total) as total_sales, SUM(amount_paid) as total_paid FROM sales");
        $totals = $stmt->fetch();
        
        ?>
        <div class="info" style="margin-top: 20px;">
            <strong>Current Totals After Fix:</strong><br>
            Total Sales: <?= number_format($totals['total_sales'], 2) ?><br>
            Total Paid: <?= number_format($totals['total_paid'], 2) ?><br>
            Difference: <?= number_format($totals['total_sales'] - $totals['total_paid'], 2) ?>
        </div>
        <p><a href="<?= APP_URL ?>/sales">Go to Sales</a></p>
        
    } catch (Exception $e) { ?>
        <div class="error">
            <strong>Error:</strong> <?= htmlspecialchars($e->getMessage()) ?>
        </div>
    <?php } ?>

</body>
</html>
