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

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <a href="<?= site_url('admin/tenants') ?>" class="text-decoration-none small text-muted"><i class="bi bi-arrow-left"></i> Hotels</a>
        <h2 class="h4 fw-bold mt-1 mb-0"><?= esc($tenant->hotel_name) ?></h2>
        <div class="text-muted small">Slug: <?= esc($tenant->slug) ?> · DB: <?= esc($tenant->db_name) ?></div>
    </div>
    <div class="d-flex align-items-center gap-2">
        <span class="badge bg-<?= $tenant->status === 'active' ? 'success' : 'danger' ?> fs-6"><?= esc(ucfirst($tenant->status)) ?></span>
        <form action="<?= site_url('admin/tenants/' . $tenant->id . '/status') ?>" method="post" onsubmit="return confirm('<?= $tenant->status === 'active' ? 'Suspend' : 'Reactivate' ?> this hotel?');">
            <?= csrf_field() ?>
            <input type="hidden" name="status" value="<?= $tenant->status === 'active' ? 'suspended' : 'active' ?>">
            <button type="submit" class="btn btn-sm btn-outline-<?= $tenant->status === 'active' ? 'danger' : 'success' ?>">
                <?= $tenant->status === 'active' ? 'Suspend Hotel' : 'Reactivate Hotel' ?>
            </button>
        </form>
    </div>
</div>

<div class="row g-3 mb-3">
    <div class="col-lg-4">
        <div class="card h-100">
            <div class="card-header">Hotel Info</div>
            <div class="card-body small">
                <div class="mb-2"><span class="text-muted">Contact Email:</span> <?= esc($tenant->contact_email ?: '—') ?></div>
                <div class="mb-2"><span class="text-muted">Contact Phone:</span> <?= esc($tenant->contact_phone ?: '—') ?></div>
                <div class="mb-2"><span class="text-muted">Onboarded:</span> <?= esc(date('d M Y', strtotime($tenant->created_at))) ?></div>
                <div><span class="text-muted">DB User:</span> <?= esc($tenant->db_user ?: '—') ?></div>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card h-100">
            <div class="card-header">Subscription</div>
            <div class="card-body">
                <?php if ($subscription): ?>
                    <?php $badge = (new \Config\Lookups())->subscriptionStatuses[$subscription->status]['badge'] ?? 'secondary'; ?>
                    <div class="d-flex align-items-center gap-2 mb-2">
                        <span class="fw-semibold fs-5"><?= esc($subscription->plan_name) ?></span>
                        <span class="badge bg-<?= $badge ?>"><?= esc(ucfirst($subscription->status)) ?></span>
                    </div>
                    <div class="text-muted small mb-3">
                        <?= format_money($subscription->plan_price) ?> / <?= esc($subscription->plan_interval) ?>
                        <?php if ($subscription->current_period_end): ?>
                            · renews <?= esc(date('d M Y', strtotime($subscription->current_period_end))) ?>
                        <?php endif; ?>
                    </div>
                    <form action="<?= site_url('admin/subscriptions/' . $subscription->id . '/status') ?>" method="post" class="d-flex gap-2 align-items-center">
                        <?= csrf_field() ?>
                        <select name="status" class="form-select form-select-sm w-auto">
                            <?php foreach ((new \Config\Lookups())->subscriptionStatuses as $key => $s): ?>
                                <option value="<?= $key ?>" <?= $subscription->status === $key ? 'selected' : '' ?>><?= esc($s['label']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="btn btn-sm btn-outline-primary">Update Status</button>
                    </form>
                <?php else: ?>
                    <p class="text-muted small mb-3">No plan assigned yet.</p>
                <?php endif; ?>

                <hr>
                <form action="<?= site_url('admin/tenants/' . $tenant->id . '/subscription') ?>" method="post" class="row g-2 align-items-end">
                    <?= csrf_field() ?>
                    <div class="col-auto">
                        <label class="form-label small fw-semibold mb-1"><?= $subscription ? 'Change Plan' : 'Assign Plan' ?></label>
                        <select name="plan_id" class="form-select form-select-sm" required>
                            <option value="">Choose a plan…</option>
                            <?php foreach ($plans as $p): ?>
                                <option value="<?= $p->id ?>"><?= esc($p->name) ?> — <?= format_money($p->price) ?>/<?= esc($p->billing_interval) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-auto">
                        <button type="submit" class="btn btn-sm btn-primary"><?= $subscription ? 'Change' : 'Assign' ?></button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                Invoices
                <a href="<?= site_url('admin/tenants/' . $tenant->id . '/invoices/new') ?>" class="btn btn-sm btn-primary"><i class="bi bi-plus-lg"></i> New Invoice</a>
            </div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle">
                    <thead><tr><th>Invoice #</th><th>Amount</th><th>Status</th><th>Due</th><th></th></tr></thead>
                    <tbody>
                    <?php if (empty($invoices)): ?>
                        <tr><td colspan="5" class="text-center text-muted py-4">No invoices yet.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($invoices as $inv):
                        $badge = (new \Config\Lookups())->invoiceStatuses[$inv->status]['badge'] ?? 'secondary'; ?>
                        <tr>
                            <td><a href="<?= site_url('admin/invoices/' . $inv->id) ?>" class="text-decoration-none"><?= esc($inv->invoice_number) ?></a></td>
                            <td><?= format_money($inv->amount) ?></td>
                            <td><span class="badge bg-<?= $badge ?>"><?= esc(ucfirst($inv->status)) ?></span></td>
                            <td class="text-muted small"><?= $inv->due_date ? esc(date('d M Y', strtotime($inv->due_date))) : '—' ?></td>
                            <td class="text-end"><a href="<?= site_url('admin/invoices/' . $inv->id) ?>" class="btn btn-sm btn-outline-primary">View</a></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header">Transactions</div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle small">
                    <thead><tr><th>Amount</th><th>Method</th><th>Status</th><th>Date</th></tr></thead>
                    <tbody>
                    <?php if (empty($transactions)): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">No transactions yet.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($transactions as $tx):
                        $badge = (new \Config\Lookups())->transactionStatuses[$tx->status]['badge'] ?? 'secondary'; ?>
                        <tr>
                            <td><?= format_money($tx->amount) ?></td>
                            <td class="text-muted"><?= esc($tx->method ?: '—') ?></td>
                            <td><span class="badge bg-<?= $badge ?>"><?= esc(ucfirst($tx->status)) ?></span></td>
                            <td class="text-muted"><?= esc(date('d M Y', strtotime($tx->created_at))) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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