<?php defined('BASEPATH') or exit('No direct script access allowed');

$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();
$warehouse_items = !empty($warehouse_items) ? $warehouse_items : array();
$warehouse_staff = !empty($warehouse_staff) ? $warehouse_staff : array();
$warehouse_item_count = isset($warehouse_item_count) ? (int) $warehouse_item_count : count($warehouse_items);
$warehouse_staff_count = isset($warehouse_staff_count) ? (int) $warehouse_staff_count : count($warehouse_staff);
$warehouse_stock_total = isset($warehouse_stock_total) ? (float) $warehouse_stock_total : 0;
$warehouse_custom_fields = custom_form_table(21, !empty($warehouse_info->warehouse_id) ? $warehouse_info->warehouse_id : null);

$warehouse_name = !empty($warehouse_info->warehouse_name) ? $warehouse_info->warehouse_name : lang('warehouse');
$warehouse_initials = '';
foreach (preg_split('/\s+/', trim((string) $warehouse_name)) as $part) {
	if ($part === '') {
		continue;
	}
	$warehouse_initials .= strtoupper(substr($part, 0, 1));
	if (strlen($warehouse_initials) >= 2) {
		break;
	}
}
if ($warehouse_initials === '') {
	$warehouse_initials = 'WH';
}

$warehouse_status_label = !empty($warehouse_info->status) ? ucfirst($warehouse_info->status) : lang('status');
$warehouse_status_class = !empty($warehouse_info->status) && $warehouse_info->status === 'published'
	? 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-900/20 dark:text-emerald-300'
	: 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/60 dark:bg-amber-900/20 dark:text-amber-300';

$contact_rows = array(
	array('label' => lang('warehouse') . ' ' . lang('code'), 'value' => !empty($warehouse_info->warehouse_code) ? $warehouse_info->warehouse_code : '-'),
	array('label' => lang('phone'), 'value' => !empty($warehouse_info->phone) ? $warehouse_info->phone : '-'),
	array('label' => lang('mobile'), 'value' => !empty($warehouse_info->mobile) ? $warehouse_info->mobile : '-'),
	array('label' => lang('email'), 'value' => !empty($warehouse_info->email) ? $warehouse_info->email : '-'),
	array('label' => lang('address'), 'value' => !empty($warehouse_info->address) ? nl2br(html_escape($warehouse_info->address)) : '-'),
);
?>

<div class="space-y-6">
	<div class="grid gap-6 xl:grid-cols-[340px_minmax(0,1fr)]">
		<aside class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
			<div class="border-b border-border px-6 py-6">
				<div class="mx-auto mb-4 flex h-24 w-24 items-center justify-center rounded-2xl bg-slate-900 text-3xl font-bold text-white shadow-lg dark:bg-slate-100 dark:text-slate-900">
					<?= html_escape($warehouse_initials) ?>
				</div>
				<h2 class="text-center text-2xl font-semibold tracking-tight text-foreground"><?= html_escape($warehouse_name) ?></h2>
				<div class="mt-4 flex items-center justify-center gap-2">
					<span class="rounded-full border px-3 py-1 text-xs font-semibold <?= $warehouse_status_class ?>"><?= html_escape($warehouse_status_label) ?></span>
					<?php if (!empty($warehouse_info->warehouse_code)) { ?>
						<span class="rounded-full border border-border bg-background px-3 py-1 text-xs font-semibold text-muted-foreground"><?= html_escape($warehouse_info->warehouse_code) ?></span>
					<?php } ?>
				</div>
			</div>

			<div class="space-y-4 px-6 py-6">
				<p class="text-xs font-bold uppercase tracking-[0.24em] text-muted-foreground">Warehouse Profile</p>
				<?php foreach ($contact_rows as $row) { ?>
					<div class="border-t border-border pt-4">
						<p class="text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground"><?= html_escape($row['label']) ?></p>
						<div class="mt-2 text-sm font-medium text-foreground"><?= $row['value'] ?></div>
					</div>
				<?php } ?>

				<?php if (!empty($warehouse_custom_fields)) { ?>
					<?php foreach ($warehouse_custom_fields as $label => $value) { ?>
						<?php if (!empty($label)) { ?>
							<div class="border-t border-border pt-4">
								<p class="text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground"><?= html_escape($label) ?></p>
								<div class="mt-2 text-sm font-medium text-foreground"><?= $value ?></div>
							</div>
						<?php } ?>
					<?php } ?>
				<?php } ?>
			</div>
		</aside>

		<section class="space-y-6">
			<div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
				<article class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<div class="mb-4 inline-flex h-11 w-11 items-center justify-center rounded-xl bg-blue-50 text-blue-600 dark:bg-blue-950/30 dark:text-blue-300">
						<i data-lucide="package-search" class="h-5 w-5"></i>
					</div>
					<p class="text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground">Tracked Items</p>
					<p class="mt-3 text-3xl font-semibold tracking-tight text-foreground"><?= $warehouse_item_count ?></p>
					<p class="mt-1 text-sm text-muted-foreground">Products linked to this warehouse.</p>
				</article>

				<article class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<div class="mb-4 inline-flex h-11 w-11 items-center justify-center rounded-xl bg-emerald-50 text-emerald-600 dark:bg-emerald-950/30 dark:text-emerald-300">
						<i data-lucide="boxes" class="h-5 w-5"></i>
					</div>
					<p class="text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground">Stock Units</p>
					<p class="mt-3 text-3xl font-semibold tracking-tight text-foreground"><?= number_format($warehouse_stock_total, 2) ?></p>
					<p class="mt-1 text-sm text-muted-foreground">Combined quantity across warehouse items.</p>
				</article>

				<article class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<div class="mb-4 inline-flex h-11 w-11 items-center justify-center rounded-xl bg-violet-50 text-violet-600 dark:bg-violet-950/30 dark:text-violet-300">
						<i data-lucide="users" class="h-5 w-5"></i>
					</div>
					<p class="text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground">Assigned Staff</p>
					<p class="mt-3 text-3xl font-semibold tracking-tight text-foreground"><?= $warehouse_staff_count ?></p>
					<p class="mt-1 text-sm text-muted-foreground">Team members currently mapped to this warehouse.</p>
				</article>
			</div>

			<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<div>
						<h3 class="text-base font-semibold text-foreground">Warehouse Items</h3>
						<p class="mt-1 text-sm text-muted-foreground">Inventory currently mapped to this warehouse.</p>
					</div>
					<span class="rounded-full border border-border bg-background px-3 py-1 text-xs font-semibold text-muted-foreground"><?= $warehouse_item_count ?> items</span>
				</div>
				<?php if (!empty($warehouse_items)) { ?>
					<div class="overflow-x-auto">
						<table class="min-w-full text-sm">
							<thead class="bg-muted/30 text-[11px] uppercase tracking-wide text-muted-foreground">
								<tr>
									<th class="px-5 py-3 text-left"><?= lang('item') ?></th>
									<th class="px-5 py-3 text-left"><?= lang('code') ?></th>
									<th class="px-5 py-3 text-left"><?= lang('group') ?></th>
									<th class="px-5 py-3 text-left"><?= lang('unit') ?></th>
									<th class="px-5 py-3 text-right"><?= lang('unit_price') ?></th>
									<th class="px-5 py-3 text-right"><?= lang('quantity') ?></th>
									<th class="px-5 py-3 text-right"><?= lang('status') ?></th>
								</tr>
							</thead>
							<tbody class="divide-y divide-border">
								<?php foreach ($warehouse_items as $item) { ?>
									<?php
									$item_status = !empty($item->status) ? strtolower((string) $item->status) : 'active';
									$item_status_class = $item_status === 'active'
										? 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-900/20 dark:text-emerald-300'
										: 'border-slate-200 bg-slate-50 text-slate-700 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-200';
									?>
									<tr>
										<td class="px-5 py-4">
											<a href="<?= base_url('admin/items/items_details/' . $item->saved_items_id) ?>" class="font-semibold text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300"><?= html_escape($item->item_name) ?></a>
										</td>
										<td class="px-5 py-4 text-foreground"><?= !empty($item->code) ? html_escape($item->code) : '-' ?></td>
										<td class="px-5 py-4 text-foreground"><?= !empty($item->customer_group) ? html_escape($item->customer_group) : '-' ?></td>
										<td class="px-5 py-4 text-foreground"><?= !empty($item->unit_type) ? html_escape($item->unit_type) : '-' ?></td>
										<td class="px-5 py-4 text-right font-medium text-foreground"><?= display_money($item->unit_cost, $currency_symbol) ?></td>
										<td class="px-5 py-4 text-right font-semibold text-foreground"><?= number_format((float) $item->warehouse_quantity, 2) ?></td>
										<td class="px-5 py-4 text-right">
											<span class="inline-flex items-center rounded-full border px-2.5 py-1 text-xs font-semibold <?= $item_status_class ?>"><?= html_escape(ucfirst($item_status)) ?></span>
										</td>
									</tr>
								<?php } ?>
							</tbody>
						</table>
					</div>
				<?php } else { ?>
					<div class="px-5 py-12 text-center">
						<div class="mx-auto mb-4 inline-flex h-14 w-14 items-center justify-center rounded-full border border-border text-muted-foreground">
							<i data-lucide="package-x" class="h-6 w-6"></i>
						</div>
						<h4 class="text-lg font-semibold text-foreground">No items found</h4>
						<p class="mt-2 text-sm text-muted-foreground">This warehouse does not have any tracked items yet.</p>
					</div>
				<?php } ?>
			</div>

			<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
				<div class="border-b border-border px-5 py-4">
					<h3 class="text-base font-semibold text-foreground">Assigned Team</h3>
					<p class="mt-1 text-sm text-muted-foreground">Staff members currently linked to this warehouse.</p>
				</div>
				<div class="p-5">
					<?php if (!empty($warehouse_staff)) { ?>
						<div class="grid gap-3 md:grid-cols-2 xl:grid-cols-3">
							<?php foreach ($warehouse_staff as $staff_member) { ?>
								<?php
								$staff_name = !empty($staff_member->fullname) ? $staff_member->fullname : (!empty($staff_member->name) ? $staff_member->name : (!empty($staff_member->username) ? $staff_member->username : lang('staff')));
								$staff_role = !empty($staff_member->designations) ? $staff_member->designations : (!empty($staff_member->job_title) ? $staff_member->job_title : lang('staff'));
								$staff_initials = '';
								foreach (preg_split('/\s+/', trim((string) $staff_name)) as $staff_part) {
									if ($staff_part === '') {
										continue;
									}
									$staff_initials .= strtoupper(substr($staff_part, 0, 1));
									if (strlen($staff_initials) >= 2) {
										break;
									}
								}
								if ($staff_initials === '') {
									$staff_initials = 'ST';
								}
								?>
								<div class="flex items-center gap-3 rounded-xl border border-border bg-background p-4">
									<div class="flex h-11 w-11 items-center justify-center rounded-full bg-slate-900 text-sm font-bold text-white dark:bg-slate-100 dark:text-slate-900">
										<?= html_escape($staff_initials) ?>
									</div>
									<div class="min-w-0">
										<p class="truncate text-sm font-semibold text-foreground"><?= html_escape($staff_name) ?></p>
										<p class="truncate text-xs text-muted-foreground"><?= html_escape($staff_role) ?></p>
									</div>
								</div>
							<?php } ?>
						</div>
					<?php } else { ?>
						<div class="rounded-xl border border-dashed border-border bg-background px-4 py-10 text-center text-sm text-muted-foreground">
							No staff members are assigned to this warehouse yet.
						</div>
					<?php } ?>
				</div>
			</div>
		</section>
	</div>
</div>
