<?php
$default_currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$purchase_currency_symbol = !empty($default_currency->symbol) ? $default_currency->symbol : '$';
$purchase_rows = array();
if ($this->db->table_exists('tbl_purchases') && $this->db->field_exists('project_id', 'tbl_purchases')) {
    $purchase_rows = $this->db
        ->select('tbl_purchases.*, tbl_suppliers.name AS supplier_name')
        ->from('tbl_purchases')
        ->join('tbl_suppliers', 'tbl_suppliers.supplier_id = tbl_purchases.supplier_id', 'left')
        ->where('tbl_purchases.project_id', $project_details->project_id)
        ->order_by('tbl_purchases.purchase_id', 'DESC')
        ->get()
        ->result();
}

$purchase_count = count($purchase_rows);
$purchase_total = 0;
$purchase_due = 0;
foreach ($purchase_rows as $purchase_row) {
    $purchase_total += (float) $this->purchase_model->calculate_to('purchase_cost', $purchase_row->purchase_id);
    $purchase_due += (float) $this->purchase_model->calculate_to('purchase_due', $purchase_row->purchase_id);
}
?>

<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('purchase') ?></h3>
            <p class="text-sm text-slate-500">Supplier purchases linked directly to this project.</p>
        </div>
        <?php if (can_action('152', 'created') || can_action('152', 'edited')) { ?>
            <a href="<?= base_url('admin/purchase/new_purchase?module=project&module_id=' . $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('purchase') ?>
            </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="shopping-cart" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= $purchase_count ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('purchase') ?></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 text-slate-900 dark:text-slate-100"><?= display_money($purchase_total, $purchase_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-amber-50 p-2.5 text-amber-600 dark:bg-amber-900/30 dark:text-amber-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($purchase_due, $purchase_currency_symbol) ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('due_amount') ?></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">Purchase Ledger</h4>
                <p class="text-xs text-muted-foreground">Purchases, suppliers, due dates, and payment state.</p>
            </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('reference_no') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('supplier') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('purchase_date') ?></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($purchase_rows)) { ?>
                        <?php foreach ($purchase_rows as $purchase_row) { ?>
                            <?php $payment_status = $this->purchase_model->get_payment_status($purchase_row->purchase_id); ?>
                            <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/purchase/purchase_details/' . $purchase_row->purchase_id) ?>">
                                        <?= html_escape($purchase_row->reference_no) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-3"><?= !empty($purchase_row->supplier_name) ? html_escape($purchase_row->supplier_name) : '-' ?></td>
                                <td class="px-4 py-3"><?= !empty($purchase_row->purchase_date) ? display_date($purchase_row->purchase_date) : '-' ?></td>
                                <td class="px-4 py-3"><?= display_money($this->purchase_model->calculate_to('purchase_due', $purchase_row->purchase_id), $purchase_currency_symbol) ?></td>
                                <td class="px-4 py-3"><span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-xs text-slate-700 dark:bg-slate-700 dark:text-slate-200"><?= lang($payment_status) ?></span></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>
