<?php
use function App\Core\{e, logo_asset, url};

$zpanelLogo = logo_asset('zpanel');
?>
<div class="panel zpanel finance-page" data-zpanel-site-banks data-csrf="<?= e($csrfToken ?? '') ?>">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body">
        <div class="finance-head">
            <div><h1>Site Banka Hesaplari</h1><p>Zalorox adina kullanilacak aktif/default banka hesaplari.</p></div>
        </div>
        <?php if (!empty($loadError)): ?><div class="finance-alert"><?= e($loadError) ?></div><?php endif; ?>
        <div class="finance-grid">
            <section class="finance-card">
                <div class="finance-card-head"><h2>Yeni Hesap</h2></div>
                <form class="finance-form" data-zpanel-finance-form>
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                    <label>Banka adi<input name="bank_name" required></label>
                    <label>Hesap sahibi<input name="account_holder" required></label>
                    <label>IBAN<input name="iban" required placeholder="TR000000000000000000000000"></label>
                    <label>Sube kodu<input name="branch_code"></label>
                    <label>Hesap no<input name="account_no"></label>
                    <label>Para birimi<input name="currency" value="TRY" maxlength="3"></label>
                    <label>Aciklama<textarea name="description" maxlength="255"></textarea></label>
                    <label class="check-row"><input type="checkbox" name="is_default" value="1"> Default yap</label>
                    <label class="check-row"><input type="checkbox" name="is_active" value="1" checked> Aktif</label>
                    <button class="pill primary" type="button" data-zpanel-endpoint="/api/zpanel/site-bank-accounts/create">Kaydet</button>
                </form>
            </section>
            <section class="finance-card">
                <div class="finance-card-head"><h2>Hesaplar</h2><span><?= count($accounts ?? []) ?> kayit</span></div>
                <div class="bank-account-list">
                    <?php foreach (($accounts ?? []) as $account): ?>
                        <article>
                            <form class="finance-form compact" data-zpanel-finance-form>
                                <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                                <input type="hidden" name="id" value="<?= e($account['id'] ?? '') ?>">
                                <div class="bank-card-head"><strong><?= e($account['bank_name'] ?? '-') ?></strong><span class="status-pill <?= !empty($account['is_active']) ? 'status-safe' : 'status-rejected' ?>"><?= !empty($account['is_active']) ? 'Aktif' : 'Pasif' ?></span></div>
                                <label>Banka adi<input name="bank_name" value="<?= e($account['bank_name'] ?? '') ?>" required></label>
                                <label>Hesap sahibi<input name="account_holder" value="<?= e($account['account_holder'] ?? '') ?>" required></label>
                                <label>IBAN<input name="iban" value="<?= e($account['iban'] ?? '') ?>" required></label>
                                <label>Sube kodu<input name="branch_code" value="<?= e($account['branch_code'] ?? '') ?>"></label>
                                <label>Hesap no<input name="account_no" value="<?= e($account['account_no'] ?? '') ?>"></label>
                                <label>Para birimi<input name="currency" value="<?= e($account['currency'] ?? 'TRY') ?>" maxlength="3"></label>
                                <label>Aciklama<textarea name="description" maxlength="255"><?= e($account['description'] ?? '') ?></textarea></label>
                                <label class="check-row"><input type="checkbox" name="is_default" value="1" <?= !empty($account['is_default']) ? 'checked' : '' ?>> Default</label>
                                <label class="check-row"><input type="checkbox" name="is_active" value="1" <?= !empty($account['is_active']) ? 'checked' : '' ?>> Aktif</label>
                                <div class="toolbar">
                                    <button class="pill primary" type="button" data-zpanel-endpoint="/api/zpanel/site-bank-accounts/update">Guncelle</button>
                                    <button class="pill danger" type="button" data-zpanel-endpoint="/api/zpanel/site-bank-accounts/delete" data-confirm="Bu hesap pasife alinsin mi?">Pasife Al</button>
                                </div>
                            </form>
                        </article>
                    <?php endforeach; ?>
                    <?php if (empty($accounts)): ?><div class="empty-state small"><strong>Site hesabi yok</strong><p>Ilk aktif hesap default olarak kullanilir.</p></div><?php endif; ?>
                </div>
            </section>
        </div>
    </section>
</div>
