<?php
if (!empty($project_details->client_id)) {
    $currency = $this->items_model->client_currency_symbol($project_details->client_id);
} else {
    $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
}

$project_invoice_summary = isset($project_invoice_summary) ? $project_invoice_summary : array(
    'rows' => array(),
    'currency_symbol' => '',
    'total_amount' => 0,
    'total_due' => 0,
    'paid_amount' => 0,
    'overdue_count' => 0,
    'invoice_count' => 0,
);
$all_invoice_info = !empty($project_invoice_summary['rows'])
    ? $project_invoice_summary['rows']
    : $this->db->where(array('status !=' => 'draft', 'project_id' => $project_details->project_id))->get('tbl_invoices')->result();

$total_amount = 0;
$total_due = 0;
$overdue_count = 0;
$invoice_count = 0;
if (!empty($all_invoice_info)) {
    foreach ($all_invoice_info as $v_invoices) {
        $invoice_count++;
        $total_amount += (float) (isset($v_invoices->display_total) ? $v_invoices->display_total : $this->invoice_model->calculate_to('invoice_cost', $v_invoices->invoices_id));
        $total_due += (float) (isset($v_invoices->display_due) ? $v_invoices->display_due : $this->invoice_model->calculate_to('invoice_due', $v_invoices->invoices_id));
        $payment_status = !empty($v_invoices->payment_status) ? $v_invoices->payment_status : $this->invoice_model->get_payment_status($v_invoices->invoices_id);
        if (safe_strtotime($v_invoices->due_date) < safe_strtotime(date('Y-m-d')) && $payment_status != lang('fully_paid')) {
            $overdue_count++;
        }
    }
}
?>

<div class="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('invoice') ?></h3>
            <p class="text-sm text-slate-500"><?= lang('invoice') ?> overview</p>
        </div>
    </div>

    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4 shadow-sm">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('invoice') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= $invoice_count ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('all') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4 shadow-sm">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('amount') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($total_amount, $currency->symbol) ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('total') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4 shadow-sm">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('due_amount') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($total_due, $currency->symbol) ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('outstanding') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4 shadow-sm">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('overdue') ?></div>
            <div class="text-2xl font-bold mt-2 text-rose-600 dark:text-rose-300"><?= $overdue_count ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('invoice') ?></div>
        </div>
    </div>

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
        <div class="overflow-x-auto overflow-y-visible">
            <table id="table-invoice" 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('invoice') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('due_date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('due_amount') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($all_invoice_info)) {
                        foreach ($all_invoice_info as $v_invoices) {
                            $payment_status = !empty($v_invoices->payment_status) ? $v_invoices->payment_status : $this->invoice_model->get_payment_status($v_invoices->invoices_id);
                            if ($payment_status == lang('fully_paid')) {
                                $label = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-200';
                            } elseif ($v_invoices->emailed == 'Yes') {
                                $payment_status = lang('sent');
                                $label = 'bg-blue-50 text-blue-700 dark:bg-blue-900/30 dark:text-blue-200';
                            } elseif ($payment_status == lang('partially_paid')) {
                                $label = 'bg-amber-50 text-amber-700 dark:bg-amber-900/30 dark:text-amber-200';
                            } elseif ($payment_status == lang('cancelled')) {
                                $label = 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-200';
                            } else {
                                $label = 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200';
                            }
                    ?>
                        <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() ?>client/invoice/manage_invoice/invoice_details/<?= $v_invoices->invoices_id ?>"><?= $v_invoices->reference_no ?></a>
                            </td>
                            <td class="px-4 py-3">
                                <?= display_date($v_invoices->due_date) ?>
                                <?php
                                if (safe_strtotime($v_invoices->due_date) < safe_strtotime(date('Y-m-d')) && $payment_status != lang('fully_paid')) { ?>
                                    <span class="ml-2 inline-flex rounded-full bg-rose-50 dark:bg-rose-900/30 px-2 py-0.5 text-xs text-rose-700 dark:text-rose-200"><?= lang('overdue') ?></span>
                                <?php } ?>
                            </td>
                            <td class="px-4 py-3"><?= display_money(isset($v_invoices->display_total) ? $v_invoices->display_total : $this->invoice_model->calculate_to('invoice_cost', $v_invoices->invoices_id), $currency->symbol) ?></td>
                            <td class="px-4 py-3"><?= display_money(isset($v_invoices->display_due) ? $v_invoices->display_due : $this->invoice_model->calculate_to('invoice_due', $v_invoices->invoices_id), $currency->symbol) ?></td>
                            <td class="px-4 py-3">
                                <span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>"><?= $payment_status ?></span>
                                <?php if ($v_invoices->recurring == 'Yes') { ?>
                                    <span class="ml-2 inline-flex rounded-full bg-indigo-50 dark:bg-indigo-900/30 px-2 py-0.5 text-xs text-indigo-700 dark:text-indigo-200" data-tooltip="<?= lang('recurring') ?>">
                                        <i data-lucide="repeat"></i>
                                    </span>
                                <?php } ?>
                            </td>
                        </tr>
                    <?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>

