<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/zoho.php';
auth_require_admin();

$page_title = 'Accounting';
require __DIR__ . '/_guard.php';

$bank_accounts = zoho_is_configured() ? zoho_list_bank_accounts() : [];

function fmt_money($v, string $sym = 'R'): string {
    return $sym . '&nbsp;' . number_format((float)$v, 2, '.', ',');
}
function bank_type_label(string $type): string {
    $map = [
        'bank'        => 'Bank',
        'credit_card' => 'Credit card',
        'paypal'      => 'PayPal',
        'cash'        => 'Cash',
    ];
    return $map[strtolower($type)] ?? ucfirst(str_replace('_', ' ', $type));
}
?>

<style>
.acct-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill, minmax(280px, 1fr));
    gap:1rem;
    margin-top:1rem;
}
.acct-card{
    background:#fff;
    border:1px solid var(--line);
    border-radius:8px;
    padding:1.25rem;
    display:flex;
    flex-direction:column;
    gap:.5rem;
    transition:border-color .15s, box-shadow .15s;
    text-decoration:none;
    color:inherit;
}
.acct-card:hover{
    border-color:var(--brand-primary);
    box-shadow:0 4px 14px rgba(0,0,0,.06);
}
.acct-card .top{display:flex;justify-content:space-between;align-items:flex-start;gap:.5rem;}
.acct-card .name{font-weight:700;font-size:1rem;color:var(--ink);margin:0;}
.acct-card .type-pill{
    background:#f3f4f6;color:#374151;
    padding:.15em .5em;border-radius:3px;
    font-size:.65rem;font-weight:700;
    text-transform:uppercase;letter-spacing:.04em;
    white-space:nowrap;
}
.acct-card .balance{font-size:1.5rem;font-weight:700;color:var(--ink);font-variant-numeric:tabular-nums;}
.acct-card .balance.neg{color:#991b1b;}
.acct-card .meta{font-size:.75rem;color:var(--ink-muted);}
.acct-card .meta strong{color:var(--ink);}
.acct-card .lbl{font-size:.7rem;color:var(--ink-muted);text-transform:uppercase;letter-spacing:.05em;margin-top:.4rem;}
.acct-card .actions{margin-top:auto;padding-top:.75rem;display:flex;gap:.5rem;align-items:center;font-size:.85rem;}
.acct-card .actions a{color:var(--brand-primary);text-decoration:none;}
.acct-card .actions a:hover{text-decoration:underline;}
.acct-card.inactive{opacity:.6;}

.section-head{display:flex;justify-content:space-between;align-items:flex-end;margin-top:2rem;margin-bottom:.5rem;flex-wrap:wrap;gap:.5rem;}
.section-head h2{margin:0;font-size:1.1rem;}
.section-head p{margin:0;font-size:.82rem;color:var(--ink-muted);}

.quick-links{display:flex;gap:.75rem;flex-wrap:wrap;margin-top:.75rem;}
.quick-links a{
    background:#fff;
    border:1px solid var(--line);
    border-radius:8px;
    padding:.85rem 1.1rem;
    text-decoration:none;
    color:var(--ink);
    font-size:.9rem;
    display:flex;
    align-items:center;
    gap:.5rem;
    transition:border-color .15s;
}
.quick-links a:hover{border-color:var(--brand-primary);}
.quick-links a strong{font-weight:700;}
.quick-links a span.muted{color:var(--ink-muted);font-size:.78rem;}
</style>

<section class="section">
<div class="container">

<h1 style="margin:0 0 .25rem;">Accounting</h1>
<p class="muted" style="margin:0 0 1.5rem;font-size:.9rem;">
    Live data from Zoho Books.
</p>

<?php if (!zoho_is_configured()): ?>
    <div class="card">
        <p class="muted" style="margin:0;">
            Zoho Books is not configured.
            Go to <a href="zoho-log.php">Settings → Zoho Books</a> to connect.
        </p>
    </div>
<?php else: ?>

<!-- Quick links -->
<div class="quick-links">
    <a href="invoices.php">
        <strong>Invoices</strong>
        <span class="muted">all customer invoices</span>
    </a>
    <a href="payfast-payouts.php">
        <strong>PayFast Payouts</strong>
        <span class="muted">record &amp; track payouts</span>
    </a>
    <a href="zoho-log.php">
        <strong>Zoho activity</strong>
        <span class="muted">sync log &amp; retries</span>
    </a>
</div>

<!-- Bank accounts -->
<div class="section-head">
    <div>
        <h2>Bank accounts &amp; cards</h2>
        <p>Click any account to view its transactions.</p>
    </div>
</div>

<?php if (empty($bank_accounts)): ?>
    <div class="card" style="margin-top:1rem;">
        <p class="muted" style="margin:0;">
            No bank accounts found in Zoho Books.
            Add one in Zoho under <em>Banking</em>, then refresh this page.
        </p>
    </div>
<?php else: ?>

<div class="acct-grid">
    <?php foreach ($bank_accounts as $acc):
        $balance        = (float)($acc['balance'] ?? 0);
        $is_active      = (bool)($acc['is_active'] ?? true);
        $currency_code  = $acc['currency_code'] ?? 'ZAR';
        $currency_sym   = $acc['currency_symbol'] ?? 'R';
        $account_type   = $acc['account_type'] ?? 'bank';
        $account_id     = (string)($acc['account_id'] ?? '');
        $account_number = $acc['account_number'] ?? '';
    ?>
        <a href="bank-transactions.php?account_id=<?= htmlspecialchars($account_id) ?>"
           class="acct-card <?= $is_active ? '' : 'inactive' ?>">
            <div class="top">
                <h3 class="name"><?= htmlspecialchars($acc['account_name'] ?? 'Unnamed') ?></h3>
                <span class="type-pill"><?= htmlspecialchars(bank_type_label((string)$account_type)) ?></span>
            </div>

            <div>
                <div class="lbl">Current balance</div>
                <div class="balance <?= $balance < 0 ? 'neg' : '' ?>">
                    <?= fmt_money($balance, htmlspecialchars($currency_sym)) ?>
                </div>
            </div>

            <?php if (!empty($account_number) || !empty($acc['bank_name']) || !empty($acc['routing_number'])): ?>
                <div class="meta">
                    <?php if (!empty($acc['bank_name'])): ?>
                        <strong><?= htmlspecialchars($acc['bank_name']) ?></strong><br>
                    <?php endif; ?>
                    <?php if (!empty($account_number)): ?>
                        Acc: <?= htmlspecialchars($account_number) ?>
                    <?php endif; ?>
                    <?php if ($currency_code !== 'ZAR'): ?>
                        &nbsp;·&nbsp; <?= htmlspecialchars($currency_code) ?>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <div class="actions">
                View transactions →
                <?php if (!$is_active): ?>
                    <span class="muted" style="margin-left:auto;font-size:.7rem;text-transform:uppercase;letter-spacing:.04em;">Inactive</span>
                <?php endif; ?>
            </div>
        </a>
    <?php endforeach; ?>
</div>

<?php endif; ?>
<?php endif; ?>

</div>
</section>

<?php require __DIR__ . '/_footer.php'; ?>