<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
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();
}
$all_estimates_info = get_result('tbl_estimates', array('project_id' => $project_details->project_id));
$estimate_total = 0;
$estimate_pending = 0;
$estimate_accepted = 0;
$estimate_declined = 0;
if (!empty($all_estimates_info)) {
    foreach ($all_estimates_info as $v_estimates) {
        $estimate_total += (float) $this->estimates_model->estimate_calculation('estimate_amount', $v_estimates->estimates_id);
        if ($v_estimates->status == 'Pending') {
            $estimate_pending++;
        } elseif ($v_estimates->status == 'Accepted') {
            $estimate_accepted++;
        } else {
            $estimate_declined++;
        }
    }
}
?>

<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('estimates') ?></h3>
            <p class="text-sm text-slate-500">Proposal values, approval state, and client estimate pipeline.</p>
        </div>
        <?php if (!empty($edited)) { ?>
            <a href="<?= base_url() ?>admin/estimates/create/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_estimate') ?>
            </a>
        <?php } ?>
    </div>

    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <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="calculator" 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_estimates_info) ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('estimates') ?></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($estimate_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-amber-50 p-2.5 text-amber-600 dark:bg-amber-900/30 dark:text-amber-400">
                    <i data-lucide="clock-3" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold mt-2 text-amber-600 dark:text-amber-300"><?= $estimate_pending ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('pending') ?></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="check-circle-2" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold mt-2 text-emerald-600 dark:text-emerald-300"><?= $estimate_accepted ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('accepted') ?></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">Estimate Ledger</h4>
                <p class="text-xs text-muted-foreground">Estimate references, due dates, and approval status.</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-amber-500"></span><?= $estimate_pending ?> <?= lang('pending') ?></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-emerald-500"></span><?= $estimate_accepted ?> <?= lang('accepted') ?></span>
            </div>
        </div>
        <div class="overflow-x-auto overflow-y-visible">
            <table id="table-estimates" 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('estimate') ?></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('status') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($all_estimates_info)) {
                        foreach ($all_estimates_info as $v_estimates) {
                            if ($v_estimates->status == 'Pending') {
                                $label = 'bg-blue-50 text-blue-700';
                            } elseif ($v_estimates->status == 'Accepted') {
                                $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/estimates/create/estimates_details/<?= $v_estimates->estimates_id ?>"><?= $v_estimates->reference_no ?></a>
                            </td>
                            <td class="px-4 py-3">
                                <?= display_date($v_estimates->due_date) ?>
                                <?php if (safe_strtotime($v_estimates->due_date) < safe_strtotime(date('Y-m-d')) && $v_estimates->status == 'Pending') { ?>
                                    <span class="ml-2 inline-flex rounded-full bg-rose-50 px-2 py-0.5 text-xs text-rose-700"><?= lang('expired') ?></span>
                                <?php } ?>
                            </td>
                            <td class="px-4 py-3"><?= display_money($this->estimates_model->estimate_calculation('estimate_amount', $v_estimates->estimates_id), $currency->symbol); ?></td>
                            <td class="px-4 py-3"><span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>"><?= lang(strtolower($v_estimates->status)) ?></span></td>
                        </tr>
                    <?php }
                    } else { ?>
                        <tr>
                            <td colspan="4" 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>

