<?php
$sidebar_mode = !empty($sidebar_mode) ? $sidebar_mode : 'payment';
$selected_purchase_id = !empty($selected_purchase_id) ? (int) $selected_purchase_id : 0;
$sidebar_page = !empty($sidebar_page) ? (int) $sidebar_page : 1;
$sidebar_pages = !empty($sidebar_pages) ? (int) $sidebar_pages : 1;
$sidebar_total = isset($sidebar_total) ? (int) $sidebar_total : 0;
$sidebar_search = isset($sidebar_search) ? (string) $sidebar_search : '';
$sidebar_purchases = !empty($sidebar_purchases) ? $sidebar_purchases : array();
$sidebar_selected_purchase = !empty($sidebar_selected_purchase) ? $sidebar_selected_purchase : null;
$sidebar_endpoint = base_url('admin/purchase/purchase_sidebar/' . $sidebar_mode . '/' . $selected_purchase_id);
$sidebar_base_path = base_url('admin/purchase/payment/');
$currency = $this->purchase_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');

$render_sidebar_purchase_card = function ($purchase_row, $is_current = false) use ($sidebar_base_path, $currency) {
    if (empty($purchase_row)) {
        return;
    }

    $total_amount = isset($purchase_row->total_amount) ? (float) $purchase_row->total_amount : 0;
    $paid_amount = isset($purchase_row->paid_amount) ? (float) $purchase_row->paid_amount : 0;
    $due_amount = $total_amount - $paid_amount;
    if ($due_amount < 0) {
        $due_amount = 0;
    }

    $status_key = 'not_paid';
    if (!empty($purchase_row->status) && strtolower((string) $purchase_row->status) === 'cancelled') {
        $status_key = 'cancelled';
    } elseif ($paid_amount <= 0) {
        $status_key = 'not_paid';
    } elseif ($due_amount <= 0) {
        $status_key = 'fully_paid';
    } else {
        $status_key = 'partially_paid';
    }

    $status_label = lang($status_key);
    $status_classes = 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-300';
    if ($status_key === 'fully_paid') {
        $status_classes = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-300';
    } elseif ($status_key === 'partially_paid') {
        $status_classes = 'bg-amber-50 text-amber-700 dark:bg-amber-900/30 dark:text-amber-300';
    } elseif ($status_key === 'cancelled') {
        $status_classes = 'bg-slate-200 text-slate-600 dark:bg-slate-700 dark:text-slate-200';
    }

    $is_selected = !empty($is_current) || ((int) $purchase_row->purchase_id === (int) $this->uri->segment(4));
    ?>
    <a href="<?= $sidebar_base_path . (int) $purchase_row->purchase_id ?>"
       class="block rounded-xl border px-3 py-3 transition <?= $is_selected ? '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-start 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(!empty($purchase_row->supplier_name) ? $purchase_row->supplier_name : '-') ?></div>
                <div class="mt-1 truncate text-xs text-slate-500 dark:text-slate-400"><?= html_escape($purchase_row->reference_no) ?></div>
            </div>
            <div class="text-right">
                <div class="text-xs font-semibold text-slate-700 dark:text-slate-200"><?= display_money($due_amount, $currency->symbol) ?></div>
                <div class="mt-1 text-[10px] uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('due') ?></div>
            </div>
        </div>
        <div class="mt-3 flex items-center justify-between gap-2">
            <span class="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-semibold <?= $status_classes ?>"><?= html_escape($status_label) ?></span>
            <span class="text-[11px] text-slate-500 dark:text-slate-400"><?= display_date($purchase_row->purchase_date) ?></span>
        </div>
    </a>
    <?php
};
?>

<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900"
     data-purchase-sidebar
     data-sidebar-endpoint="<?= $sidebar_endpoint ?>">
    <div class="flex items-center justify-between border-b border-slate-200 px-4 py-3 dark:border-slate-700">
        <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('all') . ' ' . lang('purchase') ?></h3>
        <?php if ($this->session->userdata('user_type') == '1') { ?>
            <a href="<?= base_url('admin/purchase/new_purchase') ?>"
               data-original-title="<?= lang('new') . ' ' . lang('purchase') ?>"
               data-toggle="tooltip"
               data-placement="top"
               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>
        <?php } ?>
    </div>

    <div class="border-b border-slate-200 p-3 dark:border-slate-700">
        <div class="relative">
            <i data-lucide="search" class="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400"></i>
            <input type="search"
                   value="<?= html_escape($sidebar_search) ?>"
                   placeholder="<?= lang('search') . ' ' . lang('purchase') . ' / ' . lang('supplier') ?>"
                   class="w-full rounded-lg border border-slate-200 bg-white py-2 pl-9 pr-10 text-sm text-slate-700 outline-none transition focus:border-slate-400 focus:ring-2 focus:ring-slate-200 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100 dark:focus:border-slate-500 dark:focus:ring-slate-700"
                   data-purchase-sidebar-search>
            <?php if ($sidebar_search !== '') { ?>
                <button type="button"
                        class="absolute right-2 top-1/2 inline-flex h-7 w-7 -translate-y-1/2 items-center justify-center rounded-md text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:hover:bg-slate-700 dark:hover:text-slate-200"
                        data-purchase-sidebar-clear>
                    <i data-lucide="x" class="h-4 w-4"></i>
                </button>
            <?php } ?>
        </div>
    </div>

    <div class="space-y-3 p-3">
        <?php if (!empty($sidebar_selected_purchase)) { ?>
            <div class="rounded-xl border border-dashed border-slate-200 bg-slate-50/80 p-2 dark:border-slate-700 dark:bg-slate-800/30">
                <div class="mb-2 px-1 text-[11px] font-semibold uppercase tracking-[0.2em] text-slate-400 dark:text-slate-500"><?= lang('current') ?></div>
                <?php $render_sidebar_purchase_card($sidebar_selected_purchase, true); ?>
            </div>
        <?php } ?>

        <div class="space-y-2">
            <?php if (!empty($sidebar_purchases)) { ?>
                <?php foreach ($sidebar_purchases as $purchase_row) { ?>
                    <?php if ((int) $purchase_row->purchase_id === $selected_purchase_id) {
                        continue;
                    } ?>
                    <?php $render_sidebar_purchase_card($purchase_row); ?>
                <?php } ?>
            <?php } else { ?>
                <div class="rounded-xl border border-dashed border-slate-200 px-4 py-6 text-center text-sm text-slate-500 dark:border-slate-700 dark:text-slate-400">
                    <?= lang('nothing_to_display') ?>
                </div>
            <?php } ?>
        </div>
    </div>

    <div class="flex items-center justify-between border-t border-slate-200 px-4 py-3 text-xs text-slate-500 dark:border-slate-700 dark:text-slate-400">
        <span><?= $sidebar_total ?> <?= strtolower(lang('purchase')) ?></span>
        <div class="flex items-center gap-2">
            <button type="button"
                    class="inline-flex h-8 items-center justify-center rounded-lg border border-slate-200 px-3 font-semibold transition hover:bg-slate-50 disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:hover:bg-slate-800"
                    data-purchase-sidebar-page="<?= max(1, $sidebar_page - 1) ?>"
                    <?= $sidebar_page <= 1 ? 'disabled' : '' ?>>
                <?= lang('previous') ?>
            </button>
            <span><?= $sidebar_page ?> / <?= max(1, $sidebar_pages) ?></span>
            <button type="button"
                    class="inline-flex h-8 items-center justify-center rounded-lg border border-slate-200 px-3 font-semibold transition hover:bg-slate-50 disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:hover:bg-slate-800"
                    data-purchase-sidebar-page="<?= min($sidebar_pages, $sidebar_page + 1) ?>"
                    <?= $sidebar_page >= $sidebar_pages ? 'disabled' : '' ?>>
                <?= lang('next') ?>
            </button>
        </div>
    </div>
</div>
