<?php
require_once __DIR__ . '/../inc/layout.php';
require_once __DIR__ . '/../inc/utils.php';

require_login();

$mainConfig = require __DIR__ . '/../../config/config.php';
$rules = load_bid_rules();
$verticals = $mainConfig['verticals'] ?? [];

page_header('Bid Control');
?>

<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-3">
    <div>
        <h2 class="mb-1">Minimum Bid Control</h2>
        <div class="text-muted">Auto-synced from your main ServiceDirect routing config.</div>
    </div>
    <a class="btn btn-outline-secondary" href="../dashboard.php">← Back to Dashboard</a>
</div>

<?php if (!empty($_GET['saved'])): ?>
    <div class="alert alert-success">Bid settings saved successfully.</div>
<?php endif; ?>

<div class="card soft-card">
    <div class="card-body">
        <form method="post" action="save.php">
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                    <tr>
                        <th>Vertical</th>
                        <th>Service Category</th>
                        <th>Fallback</th>
                        <th>Enable Threshold</th>
                        <th>Minimum Bid</th>
                        <th>Effective Rule</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($verticals as $verticalKey => $v): ?>
                        <?php
                        $rule = $rules[$verticalKey] ?? ['enabled' => false, 'min_bid' => 0];
                        $enabled = !empty($rule['enabled']);
                        $minBid = (float)($rule['min_bid'] ?? 0);
                        ?>
                        <tr>
                            <td class="fw-semibold"><?= h($verticalKey) ?></td>
                            <td><?= h($v['service_category'] ?? '') ?></td>
                            <td class="mono"><?= h($v['fallback_ringba_number'] ?? '') ?></td>
                            <td>
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" name="rules[<?= h($verticalKey) ?>][enabled]" value="1" <?= $enabled ? 'checked' : '' ?>>
                                </div>
                            </td>
                            <td style="min-width: 150px;">
                                <div class="input-group">
                                    <span class="input-group-text">$</span>
                                    <input type="number" step="0.01" min="0" class="form-control" name="rules[<?= h($verticalKey) ?>][min_bid]" value="<?= number_format($minBid, 2, '.', '') ?>">
                                </div>
                            </td>
                            <td class="text-muted">
                                <?php if (!$enabled): ?>
                                    Threshold off. All buyer-available calls route.
                                <?php elseif ($minBid <= 0): ?>
                                    Threshold is 0. All buyer-available calls route.
                                <?php else: ?>
                                    Route only when bid is at least $<?= number_format($minBid, 2) ?>.
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <div class="mt-3 d-flex gap-2 flex-wrap">
                <button class="btn btn-success" type="submit">Save Settings</button>
                <a class="btn btn-outline-secondary" href="../dashboard.php">Back to Dashboard</a>
            </div>
        </form>
    </div>
</div>

<?php page_footer(); ?>