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

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">New Invoice — <?= esc($tenant->hotel_name) ?></div>
            <div class="card-body">
                <?php if ($subscription): ?>
                    <div class="info-box alert alert-light border small mb-3">
                        Current plan: <strong><?= esc($subscription->plan_name) ?></strong> — <?= format_money($subscription->plan_price) ?>/<?= esc($subscription->plan_interval) ?>
                    </div>
                <?php endif; ?>

                <form action="<?= site_url('admin/tenants/' . $tenant->id . '/invoices') ?>" method="post">
                    <?= csrf_field() ?>

                    <label class="form-label small fw-semibold">Line Items</label>
                    <div id="lineItems">
                        <div class="row g-2 mb-2 line-row">
                            <div class="col-7">
                                <input type="text" class="form-control form-control-sm" name="description[]"
                                       value="<?= $subscription ? esc($subscription->plan_name . ' plan — ' . date('F Y')) : '' ?>" placeholder="Description">
                            </div>
                            <div class="col-4">
                                <input type="text" class="form-control form-control-sm" name="amount[]"
                                       value="<?= $subscription ? esc($subscription->plan_price) : '' ?>" placeholder="Amount (GHS)">
                            </div>
                            <div class="col-1"></div>
                        </div>
                    </div>
                    <button type="button" id="addLine" class="btn btn-sm btn-outline-secondary mb-3"><i class="bi bi-plus"></i> Add Line</button>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Due Date</label>
                        <input type="date" class="form-control" name="due_date" value="<?= esc(date('Y-m-d', strtotime('+14 days'))) ?>">
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-receipt me-1"></i>Generate Invoice</button>
                        <a href="<?= site_url('admin/tenants/' . $tenant->id) ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
document.getElementById('addLine').addEventListener('click', function () {
    const row = document.querySelector('.line-row').cloneNode(true);
    row.querySelectorAll('input').forEach(i => i.value = '');
    document.getElementById('lineItems').appendChild(row);
});
</script>
<?= $this->endSection() ?>
