<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
if (empty($currency)) {
    if (!empty($project_details->client_id)) {
        $currency = $this->items_model->client_currency_sambol($project_details->client_id);
    } else {
        $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
    }
}
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : '$';
$all_credit_note_info = get_result('tbl_credit_note', array('project_id' => $project_details->project_id));

$credit_total = 0;
$credit_open = 0;
$credit_refund = 0;
if (!empty($all_credit_note_info)) {
    foreach ($all_credit_note_info as $v_credit_note) {
        $credit_total += (float) $this->credit_note_model->credit_note_calculation('total', $v_credit_note->credit_note_id);
        if ($v_credit_note->status == 'refund') {
            $credit_refund++;
        } elseif ($v_credit_note->status == 'open') {
            $credit_open++;
        }
    }
}
?>

<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('credit_note') ?></h3>
            <p class="text-sm text-slate-500">Credit balances, refund state, and adjustment history.</p>
        </div>
        <?php if (!empty($edited)) { ?>
            <a href="<?= base_url() ?>admin/credit_note/createcreditnote/project/<?= $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') . ' ' . lang('credit_note') ?>
            </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-blue-50 p-2.5 text-blue-600 dark:bg-blue-900/30 dark:text-blue-400">
                    <i data-lucide="file-text" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= count($all_credit_note_info) ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('credit_note') ?></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="wallet" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($credit_total, $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-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
                    <i data-lucide="rotate-ccw" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= $credit_open ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('open') ?> | <?= $credit_refund ?> <?= lang('refund') ?></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">Credit Ledger</h4>
                <p class="text-xs text-muted-foreground">Adjustments, refunds, and remaining credit balances.</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><?= $credit_open ?> <?= lang('open') ?></span>
                <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-violet-500"></span><?= $credit_refund ?> <?= lang('refund') ?></span>
            </div>
        </div>
        <div class="overflow-x-auto overflow-y-visible">
            <table id="table-credit_note" 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('credit_note') ?> #</th>
                        <th class="px-4 py-3 text-left"><?= lang('credit_note') . ' ' . lang('date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('client_name') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('remaining') . ' ' . lang('amount') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($all_credit_note_info)) {
                        foreach ($all_credit_note_info as $v_credit_note) {
                            if ($v_credit_note->status == 'refund') {
                                $label = 'bg-blue-50 text-blue-700';
                            } elseif ($v_credit_note->status == 'open') {
                                $label = 'bg-emerald-50 text-emerald-700';
                            } else {
                                $label = 'bg-rose-50 text-rose-700';
                            }
                    ?>
                        <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/credit_note/index/credit_note_details/<?= $v_credit_note->credit_note_id ?>"><?= $v_credit_note->reference_no ?></a>
                            </td>
                            <td class="px-4 py-3"><?= display_date($v_credit_note->credit_note_date) ?></td>
                            <td class="px-4 py-3"><?= client_name($v_credit_note->client_id) ?></td>
                            <td class="px-4 py-3"><span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>"><?= lang($v_credit_note->status) ?></span></td>
                            <td class="px-4 py-3"><?= display_money($this->credit_note_model->credit_note_calculation('total', $v_credit_note->credit_note_id), client_currency($v_credit_note->client_id)) ?></td>
                            <td class="px-4 py-3"></td>
                        </tr>
                    <?php }
                    } else { ?>
                        <tr>
                            <td colspan="6" 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>
