<!DOCTYPE html>
<html>
<head>
    <title><?= lang('income_report') ?></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');
    }

    $curency = $this->report_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
    $report_summary = isset($report_summary) ? $report_summary : array('all_time' => 0, 'this_month' => 0, 'this_week' => 0, 'last_30_days' => 0);
    $all_deposit_info = isset($all_deposit_info) ? $all_deposit_info : array();
    $total_amount = 0;
    $total_credit = 0;
    $total_balance = 0;
    $income_count = !empty($report_count) ? (int) $report_count : (!empty($all_deposit_info) ? count($all_deposit_info) : 0);
    if (!empty($all_deposit_info)) {
        foreach ($all_deposit_info as $v_deposit) {
            $total_amount += $v_deposit->amount;
            $total_credit += $v_deposit->credit;
            $total_balance += $v_deposit->total_balance;
        }
    }
    ?>
    <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;
        }

        .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;
        }

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

        .range-chip {
            display: inline-block;
            padding: 4px 0 0;
            background: transparent;
            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;
        }

        .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 } ?>
        }

        .strong {
            font-weight: 700;
            color: #0f172a;
        }

        .muted {
            color: #64748b;
        }

        .total-row td {
            padding: 14px;
            background: #eef2ff;
            border-top: 1px solid #c7d2fe;
            font-weight: 700;
            color: #1e1b4b;
        }
    </style>
</head>
<body>
    <?php
    $rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
    $img = $rootPath . '/' . config_item('company_logo');
    $a = file_exists($img);
    if (empty($a)) {
        $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="<?= config_item('company_name') ?>">
                    </td>
                    <td style="border: 0;">
                        <p class="brand-title"><?= config_item('company_name') ?></p>
                        <p class="brand-text"><?= lang('income_report') ?></p>
                    </td>
                    <td class="report-meta" style="border:0;">
                        <p class="header-title"><?= lang('income_summary') ?></p>
                        <p class="header-subtitle">Income reporting snapshot of recent inflow, account activity, and running balance.</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('income') ?></div>
                    <p class="metric-value"><?= display_money($report_summary['all_time'], $curency->symbol) ?></p>
                    <div class="metric-note"><?= lang('all_time') ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('this_month') ?></div>
                    <p class="metric-value"><?= display_money($report_summary['this_month'], $curency->symbol) ?></p>
                    <div class="metric-note"><?= lang('month_to_date') ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('this_week') ?></div>
                    <p class="metric-value"><?= display_money($report_summary['this_week'], $curency->symbol) ?></p>
                    <div class="metric-note"><?= lang('current_week') ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('last_30_days') ?></div>
                    <p class="metric-value"><?= display_money($report_summary['last_30_days'], $curency->symbol) ?></p>
                    <div class="metric-note"><?= $income_count ?> <?= lang('records') ?></div>
                </td>
            </tr>
        </table>

        <div class="table-shell">
            <table>
                <thead>
                    <tr class="table-head">
                        <td><?= lang('date') ?></td>
                        <td><?= lang('account') ?></td>
                        <td><?= lang('deposit_category') ?></td>
                        <td><?= lang('paid_by') ?></td>
                        <td><?= lang('description') ?></td>
                        <td class="number"><?= lang('amount') ?></td>
                        <td class="number"><?= lang('credit') ?></td>
                        <td class="number"><?= lang('balance') ?></td>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($all_deposit_info)): foreach ($all_deposit_info as $index => $v_deposit) :
                            $account_info = $this->report_model->check_by(array('account_id' => $v_deposit->account_id), 'tbl_accounts');
                            $client_info = $this->report_model->check_by(array('client_id' => $v_deposit->paid_by), 'tbl_client');
                            $category_info = $this->report_model->check_by(array('income_category_id' => $v_deposit->category_id), 'tbl_income_category');
                            $client_name = !empty($client_info) ? $client_info->name : '-';
                    ?>
                            <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                                <td><?= display_date($v_deposit->date); ?></td>
                                <td><?= !empty($account_info->account_name) ? $account_info->account_name : '-' ?></td>
                                <td><?= !empty($category_info) ? $category_info->income_category : '-' ?></td>
                                <td><?= $client_name ?></td>
                                <td><?= strip_html_tags($v_deposit->notes, true) ?></td>
                                <td class="number"><?= display_money($v_deposit->amount, $curency->symbol) ?></td>
                                <td class="number"><?= display_money($v_deposit->credit, $curency->symbol) ?></td>
                                <td class="number strong"><?= display_money($v_deposit->total_balance, $curency->symbol) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <tr class="total-row">
                            <td colspan="5"><?= lang('total') ?></td>
                            <td class="number"><?= display_money($total_amount, $curency->symbol) ?></td>
                            <td class="number"><?= display_money($total_credit, $curency->symbol) ?></td>
                            <td class="number"><?= display_money($total_balance, $curency->symbol) ?></td>
                        </tr>
                    <?php else: ?>
                        <tr class="row">
                            <td colspan="8" class="muted"><?= lang('nothing_to_display') ?></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>
