<?php
$active_return_stock_id = isset($active_return_stock_id) ? $active_return_stock_id : null;
$list_title = !empty($list_title) ? $list_title : lang('all') . ' ' . lang('return_stock');
$item_url = !empty($item_url) ? $item_url : base_url('admin/return_stock/payment/');
$show_new = !empty($show_new);
$new_url = !empty($new_url) ? $new_url : base_url('admin/return_stock/create_returnstock');
$sidebar_items = isset($sidebar_items) ? $sidebar_items : null;
$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 : null;
$sidebar_search = isset($sidebar_search) ? (string) $sidebar_search : '';
$sidebar_page_url = !empty($sidebar_page_url) ? $sidebar_page_url : rtrim($item_url, '/') . '/' . (int) $active_return_stock_id;

$currency = $this->return_stock_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="flex items-center justify-between border-b border-border px-4 py-4">
		<div class="flex items-center gap-2 text-sm font-semibold text-foreground">
			<i data-lucide="list" class="h-5 w-5"></i>
			<span><?= $list_title ?></span>
		</div>
		<?php if ($show_new) { ?>
			<a href="<?= $new_url ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-2 py-1 text-xs font-semibold text-foreground hover:bg-muted">
				<i data-lucide="plus" class="h-4 w-4"></i>
			</a>
		<?php } ?>
	</div>
	<?php if ($sidebar_items !== null) { ?>
	<div class="border-b border-border px-3 py-3">
		<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 return stock..." 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>
	<?php } ?>
	<div class="max-h-[70vh] overflow-y-auto divide-y divide-border">
		<?php if ($sidebar_items !== null && !empty($sidebar_items)) {
			foreach ($sidebar_items as $v_return_stock) {
				$badge_class = 'bg-rose-50 text-rose-700 border-rose-200';
				$status = $v_return_stock->status;
				if ($status == 'fully_paid') {
					$badge_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
				} elseif ($status == 'partially_paid') {
					$badge_class = 'bg-amber-50 text-amber-700 border-amber-200';
				} elseif ($status == 'cancelled') {
					$badge_class = 'bg-slate-100 text-slate-700 border-slate-200';
				} elseif (!empty($v_return_stock->emailed) && $v_return_stock->emailed == 'Yes') {
					$badge_class = 'bg-blue-50 text-blue-700 border-blue-200';
					$status = 'sent';
				}
				$query = array();
				if ($sidebar_search !== '') {
					$query['q'] = $sidebar_search;
				}
				if ($sidebar_page > 1) {
					$query['page'] = $sidebar_page;
				}
				$row_url = $item_url . $v_return_stock->return_stock_id;
				if (!empty($query)) {
					$row_url .= '?' . http_build_query($query);
				}
				?>
				<a href="<?= $row_url ?>" class="block px-4 py-3 hover:bg-muted <?php if ($v_return_stock->return_stock_id == $active_return_stock_id) { echo 'bg-muted'; } ?>">
					<div class="flex items-center justify-between gap-2">
						<div class="font-medium text-foreground truncate"><?= html_escape($v_return_stock->label) ?></div>
						<div class="text-xs text-muted-foreground whitespace-nowrap"><?= display_money($v_return_stock->due_amount, $currency->symbol) ?></div>
					</div>
					<div class="mt-2 flex items-center gap-2 text-xs text-muted-foreground">
						<span class="truncate"><?= html_escape($v_return_stock->reference_no) ?></span>
						<span class="inline-flex items-center rounded-full border px-2 py-0.5 <?= $badge_class ?>">
							<?= lang($status) ?>
						</span>
					</div>
				</a>
			<?php }
		} elseif (!empty($all_return_stocks)) {
			$all_return_stocks = array_reverse($all_return_stocks);
			foreach ($all_return_stocks as $v_return_stock) {
				$status = $this->return_stock_model->get_payment_status($v_return_stock->return_stock_id);
				$badge_class = 'bg-rose-50 text-rose-700 border-rose-200';
				if ($status == ('fully_paid')) {
					$badge_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
				} elseif ($status == ('partially_paid')) {
					$badge_class = 'bg-amber-50 text-amber-700 border-amber-200';
				} elseif ($status == ('cancelled')) {
					$badge_class = 'bg-slate-100 text-slate-700 border-slate-200';
				} elseif ($v_return_stock->emailed == 'Yes') {
					$badge_class = 'bg-blue-50 text-blue-700 border-blue-200';
					$status = 'sent';
				}
				$label = '-';
				if ($v_return_stock->module == 'client') {
					$client_info = $this->return_stock_model->check_by(array('client_id' => $v_return_stock->module_id), 'tbl_client');
				} else {
					$client_info = $this->return_stock_model->check_by(array('supplier_id' => $v_return_stock->module_id), 'tbl_suppliers');
				}
				if (!empty($client_info)) {
					$label = lang($v_return_stock->module) . ': ' . $client_info->name;
				}
				$due = $this->return_stock_model->calculate_to('return_stock_due', $v_return_stock->return_stock_id);
				?>
				<a href="<?= $item_url . $v_return_stock->return_stock_id ?>" class="block px-4 py-3 hover:bg-muted <?php if ($v_return_stock->return_stock_id == $active_return_stock_id) { echo 'bg-muted'; } ?>">
					<div class="flex items-center justify-between gap-2">
						<div class="font-medium text-foreground truncate"><?= html_escape($label) ?></div>
						<div class="text-xs text-muted-foreground whitespace-nowrap"><?= display_money($due, $currency->symbol) ?></div>
					</div>
					<div class="mt-2 flex items-center gap-2 text-xs text-muted-foreground">
						<span class="truncate"><?= html_escape($v_return_stock->reference_no) ?></span>
						<span class="inline-flex items-center rounded-full border px-2 py-0.5 <?= $badge_class ?>">
							<?= lang($status) ?>
						</span>
					</div>
				</a>
			<?php }
		} else { ?>
			<div class="p-4 text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></div>
		<?php } ?>
	</div>
	<?php if ($sidebar_items !== null) { ?>
	<div class="border-t border-border px-4 py-3">
		<?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>
	<?php } ?>
</div>
