<?php
$contract_rows = array();
if ($this->db->table_exists('tbl_contracts')) {
    if ($this->db->field_exists('project_id', 'tbl_contracts')) {
        $contract_rows = $this->db->where('project_id', $project_details->project_id)->order_by('contract_id', 'DESC')->get('tbl_contracts')->result();
    } elseif (!empty($project_details->client_id) && $this->db->field_exists('client_id', 'tbl_contracts')) {
        $contract_rows = $this->db->where('client_id', $project_details->client_id)->order_by('contract_id', 'DESC')->get('tbl_contracts')->result();
    }
}

$contract_count = count($contract_rows);
$active_count = 0;
$expired_count = 0;
foreach ($contract_rows as $contract_row) {
    $status = strtolower((string) ($contract_row->status ?? ''));
    if ($status === 'active') {
        $active_count++;
    } elseif ($status === 'expired') {
        $expired_count++;
    }
}

$status_badge = function ($status) {
    $status = strtolower((string) $status);
    if ($status === 'active') {
        return 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
    }
    if ($status === 'expired') {
        return 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
    }
    if ($status === 'terminated' || $status === 'cancelled') {
        return 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
    }
    return 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300';
};
?>

<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('contracts') ?></h3>
            <p class="text-sm text-slate-500">Client contract records associated with this project context.</p>
        </div>
    </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 text-slate-900 dark:text-slate-100"><?= $contract_count ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('contracts') ?></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" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= $active_count ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('active') ?></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-rose-50 p-2.5 text-rose-600 dark:bg-rose-900/30 dark:text-rose-400">
                    <i data-lucide="x-circle" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= $expired_count ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('expired') ?></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">Contract Register</h4>
                <p class="text-xs text-muted-foreground">Start dates, end dates, and status of related contracts.</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('contract') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('start_date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('end_date') ?></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($contract_rows)) { ?>
                        <?php foreach ($contract_rows as $contract_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 font-medium text-slate-800 dark:text-slate-100"><?= !empty($contract_row->subject) ? html_escape($contract_row->subject) : '-' ?></td>
                                <td class="px-4 py-3"><?= !empty($contract_row->start_date) ? display_date($contract_row->start_date) : '-' ?></td>
                                <td class="px-4 py-3"><?= !empty($contract_row->end_date) ? display_date($contract_row->end_date) : '-' ?></td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_badge($contract_row->status ?? '') ?>">
                                        <?= !empty($contract_row->status) ? lang($contract_row->status) : '-' ?>
                                    </span>
                                </td>
                            </tr>
                        <?php } ?>
                    <?php } else { ?>
                        <tr>
                            <td colspan="4" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_record_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
