<?php
require_once __DIR__ . '/../../includes/bootstrap.php';
require_login();

$pageTitle = 'Rate Plans';
require_once __DIR__ . '/../../includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare("
        INSERT INTO room_type_rate_plans
        (
            room_type_id, agent_id, title, valid_from, valid_to,
            sharing_rate, single_rate, single_supplement,
            child_rate_percent, currency, status
        )
        VALUES
        (
            :room_type_id, :agent_id, :title, :valid_from, :valid_to,
            :sharing_rate, :single_rate, :single_supplement,
            :child_rate_percent, :currency, :status
        )
    ");
    $stmt->execute([
        ':room_type_id'        => (int)$_POST['room_type_id'],
        ':agent_id'            => !empty($_POST['agent_id']) ? (int)$_POST['agent_id'] : null,
        ':title'               => trim((string)$_POST['title']),
        ':valid_from'          => $_POST['valid_from'],
        ':valid_to'            => $_POST['valid_to'],
        ':sharing_rate'        => (float)$_POST['sharing_rate'],
        ':single_rate'         => (float)$_POST['single_rate'],
        ':single_supplement'   => (float)$_POST['single_supplement'],
        ':child_rate_percent'  => (float)($_POST['child_rate_percent'] ?: 50),
        ':currency'            => trim((string)($_POST['currency'] ?: 'USD')),
        ':status'              => 1,
    ]);
}

$roomTypes = $pdo->query("SELECT id, title FROM room_types ORDER BY title")->fetchAll(PDO::FETCH_ASSOC);
$agents = $pdo->query("SELECT id, firstname, lastname FROM agents ORDER BY firstname, lastname")->fetchAll(PDO::FETCH_ASSOC);
$plans = $pdo->query("
    SELECT p.*, rt.title AS room_type,
           CONCAT(COALESCE(a.firstname,''), ' ', COALESCE(a.lastname,'')) AS agent_name
    FROM room_type_rate_plans p
    LEFT JOIN room_types rt ON rt.id = p.room_type_id
    LEFT JOIN agents a ON a.id = p.agent_id
    ORDER BY p.valid_from DESC, p.id DESC
")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="card panel-card mb-4">
    <div class="card-header"><h5 class="mb-0">New Rate Plan</h5></div>
    <div class="card-body">
        <form method="post" class="row g-3">
            <div class="col-md-4">
                <label class="form-label">Room Type</label>
                <select name="room_type_id" class="form-select" required>
                    <?php foreach ($roomTypes as $row): ?>
                        <option value="<?php echo (int)$row['id']; ?>"><?php echo e($row['title']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">Agent</label>
                <select name="agent_id" class="form-select">
                    <option value="">Default / Public</option>
                    <?php foreach ($agents as $row): ?>
                        <option value="<?php echo (int)$row['id']; ?>">
                            <?php echo e(trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? ''))); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">Title</label>
                <input type="text" name="title" class="form-control" required>
            </div>

            <div class="col-md-3">
                <label class="form-label">Valid From</label>
                <input type="date" name="valid_from" class="form-control" required>
            </div>

            <div class="col-md-3">
                <label class="form-label">Valid To</label>
                <input type="date" name="valid_to" class="form-control" required>
            </div>

            <div class="col-md-2">
                <label class="form-label">Sharing Rate</label>
                <input type="number" step="0.01" name="sharing_rate" class="form-control" required>
            </div>

            <div class="col-md-2">
                <label class="form-label">Single Rate</label>
                <input type="number" step="0.01" name="single_rate" class="form-control" required>
            </div>

            <div class="col-md-2">
                <label class="form-label">Single Supplement</label>
                <input type="number" step="0.01" name="single_supplement" class="form-control" value="0">
            </div>

            <div class="col-md-2">
                <label class="form-label">Child %</label>
                <input type="number" step="0.01" name="child_rate_percent" class="form-control" value="50">
            </div>

            <div class="col-md-2">
                <label class="form-label">Currency</label>
                <input type="text" name="currency" class="form-control" value="USD">
            </div>

            <div class="col-12">
                <button class="btn btn-primary">Save Rate Plan</button>
            </div>
        </form>
    </div>
</div>

<div class="card panel-card">
    <div class="card-header"><h5 class="mb-0">Existing Rate Plans</h5></div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-modern mb-0">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Room Type</th>
                        <th>Agent</th>
                        <th>Dates</th>
                        <th>Sharing</th>
                        <th>Single</th>
                        <th>Supplement</th>
                        <th>Child %</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($plans as $row): ?>
                        <tr>
                            <td><?php echo e($row['title']); ?></td>
                            <td><?php echo e($row['room_type']); ?></td>
                            <td><?php echo e(trim($row['agent_name']) ?: 'Default'); ?></td>
                            <td><?php echo e($row['valid_from']); ?> → <?php echo e($row['valid_to']); ?></td>
                            <td><?php echo number_format((float)$row['sharing_rate'], 2); ?></td>
                            <td><?php echo number_format((float)$row['single_rate'], 2); ?></td>
                            <td><?php echo number_format((float)$row['single_supplement'], 2); ?></td>
                            <td><?php echo number_format((float)$row['child_rate_percent'], 2); ?>%</td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>