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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Supplier Statements',
    'workspaceTitle'        => 'Supplier Statements',
    'workspaceDesc'         => 'Review bills, payments, and debit notes with a running balance.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Filters</h2>
        <p class="enterprise-data-panel__subtitle">Select a supplier to generate the statement.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/supplier-statements') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Supplier <span class="text-danger">*</span></label>
                    <select name="supplier_id" class="form-select form-select-sm" required>
                        <option value="">— Select supplier —</option>
                        <?php foreach ($suppliers as $s): ?>
                            <option value="<?= $s['id'] ?>" <?= (string) ($filters['supplierId'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                                <?= esc($s['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">From</label>
                    <input type="date" name="date_from" class="form-control form-control-sm" value="<?= esc($filters['dateFrom'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To</label>
                    <input type="date" name="date_to" class="form-control form-control-sm" value="<?= esc($filters['dateTo'] ?? '') ?>">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Generate</button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php if ($supplier): ?>
    <div class="enterprise-data-panel">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title"><?= esc($supplier['name']) ?></h2>
            <p class="enterprise-data-panel__subtitle">
                Closing balance <?= number_format((float) $balance, 2) ?>
                <?php if (! empty($filters['dateFrom']) || ! empty($filters['dateTo'])): ?>
                    · <?= esc($filters['dateFrom'] ?: '…') ?> to <?= esc($filters['dateTo'] ?: '…') ?>
                <?php endif; ?>
            </p>
        </div>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Number</th>
                            <th>Description</th>
                            <th class="text-end">Debit</th>
                            <th class="text-end">Credit</th>
                            <th class="text-end">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($entries as $row): ?>
                            <tr>
                                <td><?= esc($row['date']) ?></td>
                                <td><?= esc(ucwords(str_replace('_', ' ', $row['type']))) ?></td>
                                <td><code><?= esc($row['number']) ?></code></td>
                                <td><?= esc($row['description']) ?></td>
                                <td class="text-end"><?= $row['debit'] > 0 ? number_format($row['debit'], 2) : '—' ?></td>
                                <td class="text-end"><?= $row['credit'] > 0 ? number_format($row['credit'], 2) : '—' ?></td>
                                <td class="text-end"><?= number_format($row['balance'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (count($entries) === 0): ?>
                            <tr><td colspan="7" class="text-center text-muted py-5">No transactions for this supplier in the selected period.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php elseif (! empty($filters['supplierId'])): ?>
    <div class="alert alert-warning">Supplier not found.</div>
<?php endif; ?>

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