<?php
require_once '../backend/config.php';

// Fetch all settlements
$settlements_res = supabase_request('GET', 'settlements?select=*,merchants(business_name)&order=created_at.desc');
$settlements = !empty($settlements_res['data']) ? $settlements_res['data'] : [];

// Handle settlement processing
if(isset($_POST['process_id'])) {
    $id = $_POST['process_id'];
    supabase_request('PATCH', 'settlements?id=eq.' . $id, ['status' => 'processed']);
    header("Location: settlements.php?success=1");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Settlements - Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../dashboard/assets/dashboard.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>

<div class="dashboard-wrapper">
    <!-- Sidebar -->
    <div class="sidebar">
        <div class="logo">Codex<span>Vertex</span></div>
        <ul class="nav-menu">
            <li class="nav-item"><a href="index.php" class="nav-link">Dashboard</a></li>
            <li class="nav-item"><a href="index.php#merchants" class="nav-link">Merchants</a></li>
            <li class="nav-item"><a href="index.php#transactions" class="nav-link">Global Transactions</a></li>
            <li class="nav-item"><a href="#" class="nav-link active">Settlements</a></li>
        </ul>
    </div>

    <!-- Main Content -->
    <div class="main-body">
        <div class="header">
            <h1>Settlement Management</h1>
        </div>

        <div class="card">
            <div class="card-header">
                <h3>Payout Requests</h3>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>Merchant</th>
                        <th>Amount</th>
                        <th>Reference ID</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(empty($settlements)): ?>
                        <tr><td colspan="6" style="text-align:center; padding:30px;">No settlement requests found.</td></tr>
                    <?php else: ?>
                        <?php foreach($settlements as $s): ?>
                        <tr>
                            <td><b><?php echo $s['merchants']['business_name']; ?></b></td>
                            <td>₹<?php echo number_format($s['amount'], 2); ?></td>
                            <td><code><?php echo $s['reference_id'] ?? 'Pending'; ?></code></td>
                            <td>
                                <span class="status-badge <?php echo ($s['status'] === 'processed') ? 'status-success' : 'status-pending'; ?>">
                                    <?php echo ucfirst($s['status']); ?>
                                </span>
                            </td>
                            <td><?php echo date('d M, H:i', strtotime($s['created_at'])); ?></td>
                            <td>
                                <?php if($s['status'] === 'pending'): ?>
                                <form method="POST" style="display:inline;">
                                    <input type="hidden" name="process_id" value="<?php echo $s['id']; ?>">
                                    <button type="submit" class="btn btn-primary" style="padding: 5px 12px; font-size: 0.75rem;">Mark as Processed</button>
                                </form>
                                <?php else: ?>
                                <span style="color: var(--success); font-weight: 700;">PROCESSED ✓</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>
