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

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'Edit Route',
    'workspaceTitle'        => 'Edit Route',
    'workspaceDesc'         => 'Update route details and intermediate stops.',
]) ?>

<article class="enterprise-settings-card" style="max-width:860px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-pen"></i>
        <h2 class="enterprise-settings-card__title"><?= esc($route['name']) ?></h2>
    </div>

    <?= $this->include('Modules\Transport\Views\partials\form_errors') ?>

    <form action="<?= base_url('transport/routes/' . $route['id']) ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <?= $this->include('Modules\Transport\Views\routes\_form') ?>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('transport/routes/' . $route['id']) ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Save Changes</button>
        </div>
    </form>
</article>

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

<?= $this->section('scripts') ?>
<script>
(function () {
    'use strict';
    var container = document.getElementById('route-stops-container');
    var template = document.getElementById('route-stop-template');
    var addBtn = document.getElementById('route-add-stop');

    function bindRemove(row) {
        var btn = row.querySelector('.route-remove-stop');
        if (btn) {
            btn.addEventListener('click', function () {
                var rows = container.querySelectorAll('.route-stop-row');
                if (rows.length <= 1) {
                    row.querySelector('select').value = '';
                    row.querySelector('input').value = '';
                    return;
                }
                row.remove();
            });
        }
    }

    container.querySelectorAll('.route-stop-row').forEach(bindRemove);

    addBtn.addEventListener('click', function () {
        var clone = template.content.cloneNode(true);
        var row = clone.querySelector('.route-stop-row');
        bindRemove(row);
        container.appendChild(clone);
    });
})();
</script>
<?= $this->endSection() ?>
