<?php
$sidebar_mode = !empty($estimate_sidebar_mode) ? $estimate_sidebar_mode : 'estimates_history';
$sidebar_page = !empty($estimate_sidebar_page) ? (int) $estimate_sidebar_page : 1;
$sidebar_pages = !empty($estimate_sidebar_pages) ? (int) $estimate_sidebar_pages : 1;
$sidebar_total = isset($estimate_sidebar_total) ? (int) $estimate_sidebar_total : 0;
$sidebar_search = isset($estimate_sidebar_search) ? (string) $estimate_sidebar_search : '';
$sidebar_items = !empty($estimate_sidebar_items) ? $estimate_sidebar_items : array();
$selected_estimate_id = !empty($selected_estimate_id) ? (int) $selected_estimate_id : 0;
$sidebar_page_url = !empty($estimate_sidebar_page_url) ? $estimate_sidebar_page_url : base_url('admin/estimates/create/' . $sidebar_mode . '/' . $selected_estimate_id);
?>

<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
    <div class="flex items-center justify-between border-b border-slate-200 px-4 py-3 dark:border-slate-700">
        <div>
            <h2 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('all_estimates') ?></h2>
            <p class="text-xs text-slate-500 dark:text-slate-400"><?= $sidebar_total ?> <?= lang('estimate') ?></p>
        </div>
        <a href="<?= base_url() ?>admin/estimates/create/edit_estimates" class="inline-flex h-8 w-8 items-center justify-center rounded-lg border border-slate-200 bg-white text-slate-600 transition hover:bg-slate-100 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-300 dark:hover:bg-slate-700">
            <i data-lucide="plus" class="h-4 w-4"></i>
        </a>
    </div>

    <div class="border-b border-slate-200 px-3 py-3 dark:border-slate-700">
        <form action="<?= $sidebar_page_url ?>" method="get" class="relative">
            <span class="pointer-events-none absolute left-4 top-1/2 -translate-y-1/2 text-slate-400">
                <i data-lucide="search" class="h-4 w-4"></i>
            </span>
            <input type="search" name="q" value="<?= html_escape($sidebar_search) ?>" placeholder="Search estimates..." class="w-full rounded-xl border border-slate-300 bg-slate-100 py-3 pl-11 pr-4 text-sm text-slate-900 outline-none transition placeholder:text-slate-400 focus:border-primary focus:bg-white focus:ring-2 focus:ring-primary/20 dark:border-slate-600 dark:bg-slate-900 dark:text-white">
        </form>
    </div>

    <div class="max-h-[70vh] overflow-y-auto p-2">
        <?php if (!empty($sidebar_items)) { ?>
            <?php foreach ($sidebar_items as $estimate) { ?>
                <?php
                if ($estimate->invoiced == 'Yes') {
                    $item_status = strtoupper(lang('invoiced'));
                    $item_status_class = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-300';
                } elseif ($estimate->emailed == 'Yes') {
                    $item_status = strtoupper(lang('sent'));
                    $item_status_class = 'bg-blue-50 text-blue-700 dark:bg-blue-900/30 dark:text-blue-300';
                } else {
                    $item_status = lang($estimate->status);
                    $item_status_value = strtolower((string) $estimate->status);
                    $item_status_class = 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300';
                    if ($item_status_value === 'accepted') {
                        $item_status_class = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-300';
                    } elseif ($item_status_value === 'declined') {
                        $item_status_class = 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-300';
                    } elseif ($item_status_value === 'pending') {
                        $item_status_class = 'bg-amber-50 text-amber-700 dark:bg-amber-900/30 dark:text-amber-300';
                    }
                }
                $is_active = ((int) $estimate->estimates_id === $selected_estimate_id);
                $query = array();
                if ($sidebar_search !== '') {
                    $query['q'] = $sidebar_search;
                }
                if ($sidebar_page > 1) {
                    $query['page'] = $sidebar_page;
                }
                $row_url = base_url('admin/estimates/create/' . $sidebar_mode . '/' . (int) $estimate->estimates_id);
                if (!empty($query)) {
                    $row_url .= '?' . http_build_query($query);
                }
                ?>
                <a href="<?= $row_url ?>" class="mb-2 block rounded-xl border px-3 py-3 transition <?= $is_active ? 'border-slate-400 bg-slate-100 dark:border-slate-500 dark:bg-slate-800/70' : 'border-slate-200 hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800/40' ?>">
                    <div class="flex items-center justify-between gap-3">
                        <div class="min-w-0">
                            <div class="truncate text-sm font-semibold text-slate-800 dark:text-slate-100"><?= html_escape($estimate->client_name) ?></div>
                            <div class="mt-1 text-xs text-slate-500 dark:text-slate-400"><?= html_escape($estimate->reference_no) ?></div>
                        </div>
                        <div class="shrink-0 text-right">
                            <div class="text-xs font-semibold text-slate-700 dark:text-slate-200"><?= display_money($estimate->total_amount, $estimate->currency_symbol) ?></div>
                        </div>
                    </div>
                    <div class="mt-2 flex items-center justify-between gap-2">
                        <span class="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-semibold <?= $item_status_class ?>"><?= html_escape($item_status) ?></span>
                        <?php if (!empty($estimate->estimate_date)) { ?>
                            <span class="text-[11px] text-slate-500 dark:text-slate-400"><?= display_date($estimate->estimate_date) ?></span>
                        <?php } ?>
                    </div>
                </a>
            <?php } ?>
        <?php } else { ?>
            <div class="rounded-xl border border-dashed border-slate-200 p-6 text-center text-sm text-slate-500 dark:border-slate-700 dark:text-slate-400"><?= lang('no_record_found') ?></div>
        <?php } ?>
    </div>

    <div class="border-t border-slate-200 px-4 py-3 dark:border-slate-700">
        <?php $page_query = $sidebar_search !== '' ? array('q' => $sidebar_search) : array(); ?>
        <div class="flex items-center justify-between gap-3">
            <?php if ($sidebar_page > 1) { ?>
                <a href="<?= $sidebar_page_url . '?' . http_build_query(array_merge($page_query, array('page' => $sidebar_page - 1))) ?>" class="inline-flex items-center gap-2 rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-600 transition hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200">Prev</a>
            <?php } else { ?>
                <span class="inline-flex items-center gap-2 rounded-lg border border-slate-200 bg-slate-50 px-4 py-2 text-sm font-medium text-slate-400 dark:border-slate-700 dark:bg-slate-700 dark:text-slate-500">Prev</span>
            <?php } ?>
            <div class="text-center text-sm font-medium text-slate-500 dark:text-slate-300"><?= $sidebar_page ?> of <?= $sidebar_pages ?></div>
            <?php if ($sidebar_page < $sidebar_pages) { ?>
                <a href="<?= $sidebar_page_url . '?' . http_build_query(array_merge($page_query, array('page' => $sidebar_page + 1))) ?>" class="inline-flex items-center gap-2 rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-600 transition hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200">Next</a>
            <?php } else { ?>
                <span class="inline-flex items-center gap-2 rounded-lg border border-slate-200 bg-slate-50 px-4 py-2 text-sm font-medium text-slate-400 dark:border-slate-700 dark:bg-slate-700 dark:text-slate-500">Next</span>
            <?php } ?>
        </div>
    </div>
</div>
