<?php
$sidebar_mode = !empty($credit_note_sidebar_mode) ? $credit_note_sidebar_mode : 'credit_note_history';
$sidebar_page = !empty($credit_note_sidebar_page) ? (int) $credit_note_sidebar_page : 1;
$sidebar_pages = !empty($credit_note_sidebar_pages) ? (int) $credit_note_sidebar_pages : 1;
$sidebar_total = isset($credit_note_sidebar_total) ? (int) $credit_note_sidebar_total : 0;
$sidebar_search = isset($credit_note_sidebar_search) ? (string) $credit_note_sidebar_search : '';
$sidebar_items = !empty($credit_note_sidebar_items) ? $credit_note_sidebar_items : array();
$selected_credit_note_id = !empty($selected_credit_note_id) ? (int) $selected_credit_note_id : 0;
$sidebar_page_url = !empty($credit_note_sidebar_page_url) ? $credit_note_sidebar_page_url : base_url('admin/credit_note/index/' . $sidebar_mode . '/' . $selected_credit_note_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_credit_note') ?></h2>
            <p class="text-xs text-slate-500 dark:text-slate-400"><?= $sidebar_total ?> <?= lang('credit_note') ?></p>
        </div>
        <a href="<?= base_url() ?>admin/credit_note/createcreditnote" 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 credit notes..."
                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 $credit_note_row) { ?>
                <?php
                $status_value = strtolower((string) $credit_note_row->status);
                $item_status_class = 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300';
                if ($status_value === 'open') {
                    $item_status_class = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-300';
                } elseif ($status_value === 'refund') {
                    $item_status_class = 'bg-blue-50 text-blue-700 dark:bg-blue-900/30 dark:text-blue-300';
                } elseif ($status_value === 'void') {
                    $item_status_class = 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-300';
                }
                $is_active = ((int) $credit_note_row->credit_note_id === $selected_credit_note_id);
                $query = array();
                if ($sidebar_search !== '') {
                    $query['q'] = $sidebar_search;
                }
                if ($sidebar_page > 1) {
                    $query['page'] = $sidebar_page;
                }
                $row_url = base_url('admin/credit_note/index/' . $sidebar_mode . '/' . (int) $credit_note_row->credit_note_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($credit_note_row->client_name) ?></div>
                            <div class="mt-1 text-xs text-slate-500 dark:text-slate-400"><?= html_escape($credit_note_row->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($credit_note_row->total_amount, $credit_note_row->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(lang($credit_note_row->status)) ?>
                        </span>
                        <?php if (!empty($credit_note_row->credit_note_date)) { ?>
                            <span class="text-[11px] text-slate-500 dark:text-slate-400"><?= display_date($credit_note_row->credit_note_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
        $prev_page = $sidebar_page > 1 ? $sidebar_page - 1 : null;
        $next_page = $sidebar_page < $sidebar_pages ? $sidebar_page + 1 : null;
        $page_query = array();
        if ($sidebar_search !== '') {
            $page_query['q'] = $sidebar_search;
        }
        ?>
        <div class="flex items-center justify-between gap-3">
            <?php if (!empty($prev_page)) { ?>
                <a href="<?= $sidebar_page_url . '?' . http_build_query(array_merge($page_query, array('page' => $prev_page))) ?>" 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">
                    <i data-lucide="chevron-left" class="h-4 w-4"></i>
                    <span>Prev</span>
                </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">
                    <i data-lucide="chevron-left" class="h-4 w-4"></i>
                    <span>Prev</span>
                </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 (!empty($next_page)) { ?>
                <a href="<?= $sidebar_page_url . '?' . http_build_query(array_merge($page_query, array('page' => $next_page))) ?>" 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">
                    <span>Next</span>
                    <i data-lucide="chevron-right" class="h-4 w-4"></i>
                </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">
                    <span>Next</span>
                    <i data-lucide="chevron-right" class="h-4 w-4"></i>
                </span>
            <?php } ?>
        </div>
    </div>
</div>
