<?php
$project_currency = !empty($project_details->client_id)
    ? $this->items_model->client_currency_sambol($project_details->client_id)
    : $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$project_currency_symbol = !empty($project_currency->symbol) ? $project_currency->symbol : '$';

$deposit_rows = $this->db
    ->select('tbl_transactions.*, tbl_accounts.account_name, tbl_client.name AS client_name')
    ->from('tbl_transactions')
    ->join('tbl_accounts', 'tbl_accounts.account_id = tbl_transactions.account_id', 'left')
    ->join('tbl_client', 'tbl_client.client_id = tbl_transactions.paid_by', 'left')
    ->where(array(
        'tbl_transactions.project_id' => $project_details->project_id,
        'tbl_transactions.type' => 'Income',
    ))
    ->order_by('tbl_transactions.transactions_id', 'DESC')
    ->get()
    ->result();

$deposit_count = count($deposit_rows);
$deposit_total = 0;
$deposit_accounts = array();
foreach ($deposit_rows as $deposit_row) {
    $deposit_total += (float) $deposit_row->amount;
    if (!empty($deposit_row->account_id)) {
        $deposit_accounts[(int) $deposit_row->account_id] = true;
    }
}
?>

<div class="project-overview-shell space-y-5">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <div>
            <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('deposit') ?></h3>
            <p class="text-sm text-slate-500">Project-linked income records and posted deposit activity.</p>
        </div>
        <?php if (can_action('30', 'created') || can_action('30', 'edited')) { ?>
            <a href="<?= base_url('admin/transactions/create_deposit/project_deposit/' . $project_details->project_id) ?>"
               class="inline-flex items-center gap-2 rounded-2xl bg-slate-900 px-3.5 py-2 text-xs font-semibold text-white shadow-sm hover:bg-slate-800 dark:hover:bg-slate-700">
                <i data-lucide="plus" class="h-4 w-4"></i>
                <?= lang('new_deposit') ?>
            </a>
        <?php } ?>
    </div>

    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-3">
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
                    <i data-lucide="arrow-down-circle" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= $deposit_count ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('deposit') ?></div>
                </div>
            </div>
        </div>
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-blue-50 p-2.5 text-blue-600 dark:bg-blue-900/30 dark:text-blue-400">
                    <i data-lucide="wallet" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= display_money($deposit_total, $project_currency_symbol) ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('amount') ?></div>
                </div>
            </div>
        </div>
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-violet-50 p-2.5 text-violet-600 dark:bg-violet-900/30 dark:text-violet-400">
                    <i data-lucide="landmark" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= count($deposit_accounts) ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('account') ?></div>
                </div>
            </div>
        </div>
    </div>

    <div class="project-section-card overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
        <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-3">
            <div>
                <h4 class="text-sm font-semibold text-foreground">Deposit Ledger</h4>
                <p class="text-xs text-muted-foreground">Income transactions linked back to this project.</p>
            </div>
            <div class="flex flex-wrap gap-2 text-xs">
                <span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-emerald-500"></span><?= display_money($deposit_total, $project_currency_symbol) ?></span>
            </div>
        </div>
        <div class="overflow-x-auto overflow-y-visible">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-slate-500 dark:bg-slate-800 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('deposit') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('account') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('paid_by') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($deposit_rows)) { ?>
                        <?php foreach ($deposit_rows as $deposit_row) { ?>
                            <tr class="text-slate-700 transition hover:bg-slate-50/70 dark:text-slate-200 dark:hover:bg-slate-800/50">
                                <td class="px-4 py-3">
                                    <a class="font-medium text-primary hover:underline dark:text-sky-300" href="<?= base_url('admin/transactions/view_deposit/' . $deposit_row->transactions_id) ?>">
                                        <?= !empty($deposit_row->name) ? html_escape($deposit_row->name) : (!empty($deposit_row->transaction_prefix) ? html_escape($deposit_row->transaction_prefix) : '#' . $deposit_row->transactions_id) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-3"><?= !empty($deposit_row->date) ? display_date($deposit_row->date) : '-' ?></td>
                                <td class="px-4 py-3"><?= !empty($deposit_row->account_name) ? html_escape($deposit_row->account_name) : '-' ?></td>
                                <td class="px-4 py-3"><?= !empty($deposit_row->client_name) ? html_escape($deposit_row->client_name) : '-' ?></td>
                                <td class="px-4 py-3"><?= display_money($deposit_row->amount, $project_currency_symbol) ?></td>
                            </tr>
                        <?php } ?>
                    <?php } else { ?>
                        <tr>
                            <td colspan="5" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
