<!DOCTYPE html>
<html>
<head>
    <title><?= lang('balance_sheet') ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }

    $currency_symbol = !empty($currency_symbol) ? $currency_symbol : default_currency();
    $all_account = !empty($all_account) ? $all_account : array();
    ?>
    <style>
        body {
            margin: 0;
            color: #172033;
            background: #ffffff;
            font: 12px/1.55 DejaVu Sans, Arial, Helvetica, sans-serif;
            min-width: 100%;
            min-height: 100%;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }

        .page-wrap {
            padding: 18px 22px 24px;
        }

        .brand-shell {
            border: 1px solid #d7deea;
            padding: 18px 20px;
            background: #f8fafc;
            margin-bottom: 18px;
        }

        .brand-logo {
            width: 60px;
            height: 60px;
            background: #ffffff;
            border: 1px solid #d7deea;
            padding: 6px;
        }

        .brand-title {
            font-size: 24px;
            font-weight: 500;
            color: #0f172a;
            margin: 0 0 4px;
        }

        .brand-text {
            font-size: 12px;
            color: #64748b;
            margin: 0;
        }

        .report-meta {
            width: 300px;
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }

        .header-title {
            font-size: 20px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 4px;
            line-height: 1.15;
        }

        .header-subtitle {
            font-size: 10px;
            color: #64748b;
            margin: 0;
            line-height: 1.35;
        }

        .range-chip {
            display: inline-block;
            padding-top: 4px;
            color: #475569;
            font-size: 9px;
            font-weight: 700;
            letter-spacing: .08em;
            text-transform: uppercase;
        }

        .metrics {
            margin: 0 0 18px;
        }

        .metric-card {
            width: 23.5%;
            border: 1px solid #d7deea;
            background: #ffffff;
            padding: 14px 16px;
            vertical-align: top;
        }

        .metric-gap {
            width: 2%;
        }

        .metric-label {
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .12em;
            color: #64748b;
            margin-bottom: 6px;
        }

        .metric-value {
            font-size: 21px;
            font-weight: 700;
            color: #0f172a;
            margin: 0;
        }

        .metric-note {
            font-size: 11px;
            color: #94a3b8;
            margin-top: 4px;
        }

        .table-shell {
            border: 1px solid #d7deea;
            overflow: hidden;
            background: #ffffff;
            margin-bottom: 18px;
        }

        .table-shell table {
            border-collapse: separate;
            border-spacing: 0;
        }

        .table-head td {
            background: #1e293b;
            color: #f8fafc;
            font-size: 11px;
            font-weight: 700;
            letter-spacing: .06em;
            text-transform: uppercase;
            padding: 12px 14px;
            border-bottom: 1px solid #1e293b;
        }

        .row td {
            padding: 12px 14px;
            border-bottom: 1px solid #e2e8f0;
            color: #1e293b;
            vertical-align: top;
        }

        .row-alt td {
            background: #f8fafc;
        }

        .number {
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }

        .muted {
            color: #64748b;
        }

        .total-row td {
            padding: 14px;
            background: #eef2ff;
            border-top: 1px solid #c7d2fe;
            font-weight: 700;
            color: #1e1b4b;
        }

        .status {
            display: inline-block;
            padding: 3px 8px;
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
        }

        .status-positive {
            background: #dcfce7;
            color: #166534;
        }

        .status-negative {
            background: #fee2e2;
            color: #991b1b;
        }

        .key {
            width: 35%;
            color: #64748b;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
        }
    </style>
</head>
<body>
<?php
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . ltrim(config_item('company_logo'), '/');
if (!file_exists($img)) {
    $img = base_url() . config_item('company_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/uploads/default_logo.png';
}
?>
<div class="page-wrap">
    <div class="brand-shell">
        <table>
            <tr>
                <td style="width: 84px; border: 0;"><img class="brand-logo" src="<?= $img ?>" alt="<?= html_escape(config_item('company_name')) ?>"></td>
                <td style="border: 0;"><p class="brand-title"><?= html_escape(config_item('company_name')) ?></p><p class="brand-text"><?= lang('balance_sheet') ?></p></td>
                <td class="report-meta" style="border: 0;"><p class="header-title"><?= lang('balance_sheet') ?></p><p class="header-subtitle">Account position summary with strongest, lowest, and aggregate balances across the ledger.</p><span class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></span></td>
            </tr>
        </table>
    </div>

    <table class="metrics">
        <tr>
            <td class="metric-card"><div class="metric-label"><?= lang('total') ?> <?= lang('balance') ?></div><p class="metric-value"><?= display_money($total_amount, $currency_symbol) ?></p><div class="metric-note"><?= lang('accounts') ?>: <?= (int) $total_accounts ?></div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label"><?= lang('accounts') ?></div><p class="metric-value"><?= (int) $total_accounts ?></p><div class="metric-note"><?= lang('balance_sheet') ?></div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label">Positive</div><p class="metric-value"><?= (int) $positive_accounts ?></p><div class="metric-note">Accounts above zero</div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label">Negative</div><p class="metric-value"><?= (int) $negative_accounts ?></p><div class="metric-note">Accounts below zero</div></td>
        </tr>
    </table>

    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td colspan="2">Highlights</td><td colspan="2">Balance</td></tr></thead>
            <tbody>
                <tr class="row">
                    <td class="key">Strongest Account</td>
                    <td><?= !empty($top_account->account_name) ? html_escape($top_account->account_name) : '-' ?></td>
                    <td class="key">Balance</td>
                    <td class="number"><?= !empty($top_account) ? display_money($top_account->balance, $currency_symbol) : '-' ?></td>
                </tr>
                <tr class="row row-alt">
                    <td class="key">Lowest Account</td>
                    <td><?= !empty($lowest_account->account_name) ? html_escape($lowest_account->account_name) : '-' ?></td>
                    <td class="key">Balance</td>
                    <td class="number"><?= !empty($lowest_account) ? display_money($lowest_account->balance, $currency_symbol) : '-' ?></td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="table-shell">
        <table>
            <thead>
                <tr class="table-head">
                    <td><?= lang('account') ?></td>
                    <td class="number"><?= lang('balance') ?></td>
                    <td><?= lang('status') ?></td>
                </tr>
            </thead>
            <tbody>
            <?php if (!empty($all_account)): foreach ($all_account as $index => $account): ?>
                <?php $balance = (float) $account->balance; ?>
                <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                    <td><?= !empty($account->account_name) ? html_escape($account->account_name) : '-' ?></td>
                    <td class="number"><?= display_money($balance, $currency_symbol) ?></td>
                    <td><?php if ($balance >= 0) { ?><span class="status status-positive">Positive</span><?php } else { ?><span class="status status-negative">Negative</span><?php } ?></td>
                </tr>
            <?php endforeach; ?>
                <tr class="total-row">
                    <td><?= lang('total') ?></td>
                    <td class="number"><?= display_money($total_amount, $currency_symbol) ?></td>
                    <td><?= (int) $total_accounts ?> <?= lang('accounts') ?></td>
                </tr>
            <?php else: ?>
                <tr class="row"><td colspan="3" class="muted">No balance sheet records available.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
