<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>
<div class="bg-info text-white py-4 mb-4">
    <div class="container fade-in">
        <h2 class="fw-bold"><i class="fa-solid fa-plane-departure me-2"></i>Flight Inventory Management</h2>
        <a href="<?= base_url('admin') ?>" class="text-white text-decoration-none small"><i class="fa-solid fa-arrow-left me-1"></i> Back to Dashboard</a>
    </div>
</div>

<div class="container mb-5 fade-in">
    <?php if(session()->getFlashdata('success')): ?>
        <div class="alert alert-success mb-4 rounded-3"><i class="fa-solid fa-check-circle me-2"></i><?= session()->getFlashdata('success') ?></div>
    <?php endif; ?>
    <?php if(session()->getFlashdata('error')): ?>
        <div class="alert alert-danger mb-4 rounded-3"><i class="fa-solid fa-circle-exclamation me-2"></i><?= session()->getFlashdata('error') ?></div>
    <?php endif; ?>

    <div class="d-flex justify-content-end mb-3">
        <button class="btn btn-primary shadow-sm rounded-pill fw-bold" data-bs-toggle="modal" data-bs-target="#addFlightModal"><i class="fa-solid fa-plus me-1"></i> Add New Flight</button>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="bg-light">
                        <tr>
                            <th>Airline</th>
                            <th>Flight No.</th>
                            <th>Route</th>
                            <th>Timings</th>
                            <th>Price</th>
                            <th>Seats</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($flights)): ?>
                        <tr><td colspan="7" class="text-center text-muted py-4">No flights recorded. <a href="<?= base_url('setup') ?>">Setup database if you haven't.</a></td></tr>
                        <?php else: ?>
                            <?php foreach($flights as $flight): ?>
                            <tr>
                                <td class="fw-bold"><?= esc($flight['airline']) ?></td>
                                <td><span class="badge bg-light text-dark border"><?= esc($flight['flight_number']) ?></span></td>
                                <td><?= esc($flight['source']) ?> <i class="fa-solid fa-arrow-right text-muted small mx-1"></i> <?= esc($flight['destination']) ?></td>
                                <td class="small">
                                    <div class="text-success fw-bold">D: <?= date('d M, H:i', strtotime($flight['departure_time'])) ?></div>
                                    <div class="text-primary fw-bold">A: <?= date('d M, H:i', strtotime($flight['arrival_time'])) ?></div>
                                </td>
                                <td class="fw-bold text-dark fs-5">₹<?= number_format($flight['base_price'], 2) ?></td>
                                <td>
                                    <?php 
                                        $sc = intval($flight['seats_available']);
                                        $badge = $sc > 20 ? 'success' : ($sc > 5 ? 'warning' : 'danger');
                                    ?>
                                    <span class="badge bg-<?= $badge ?>"><?= $sc ?> Left</span>
                                </td>
                                <td>
                                    <button class="btn btn-sm btn-outline-info rounded-circle me-1" title="Edit"><i class="fa-solid fa-pen"></i></button>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Add Flight Modal -->
<div class="modal fade" id="addFlightModal" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content border-0 rounded-4 shadow-lg">
      <div class="modal-header bg-light border-0">
        <h5 class="modal-title fw-bold"><i class="fa-solid fa-plane-up text-primary me-2"></i>Add New Flight</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form action="<?= base_url('admin/addFlight') ?>" method="POST">
        <?= csrf_field() ?>
        <div class="modal-body p-4">
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label small text-muted">Airline Name</label>
                    <input type="text" name="airline" class="form-control bg-light border-0" required placeholder="e.g. IndiGo">
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Flight Number</label>
                    <input type="text" name="flight_number" class="form-control bg-light border-0" required placeholder="e.g. 6E-452">
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Source City</label>
                    <input type="text" name="source" class="form-control bg-light border-0" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Destination City</label>
                    <input type="text" name="destination" class="form-control bg-light border-0" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Departure Time</label>
                    <input type="datetime-local" name="departure_time" class="form-control bg-light border-0" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Arrival Time</label>
                    <input type="datetime-local" name="arrival_time" class="form-control bg-light border-0" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Base Price (INR)</label>
                    <input type="number" step="0.01" name="base_price" class="form-control bg-light border-0" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label small text-muted">Total Seats</label>
                    <input type="number" name="seats_available" class="form-control bg-light border-0" value="60" required>
                </div>
            </div>
        </div>
        <div class="modal-footer border-0 bg-light rounded-bottom-4">
            <button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-primary rounded-pill px-4 shadow-sm">Save Flight</button>
        </div>
      </form>
    </div>
  </div>
</div>

<?= $this->endSection() ?>
