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

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('39', 'created');
$edited = can_action('39', 'edited');
$deleted = can_action('39', 'deleted');

if (!empty($created) || !empty($edited)) {
?>
	<div class="space-y-4">
		<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
			<div>
				<h2 class="text-xl font-semibold text-foreground"><?= lang('manufacturer') . ' ' . lang('list'); ?></h2>
				<p class="text-sm text-muted-foreground"><?= lang('items') . ' ' . lang('manufacturer'); ?></p>
			</div>
			<div class="flex flex-wrap gap-2">
				<a href="<?= base_url('admin/items/items_list'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
					<?= lang('all_items'); ?>
				</a>
				<a href="<?= base_url('admin/items/new_items'); ?>" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white shadow hover:bg-primary/90 transition">
					<?= lang('new_items'); ?>
				</a>
				<a href="<?= base_url('admin/items/newitems_group'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
					<?= lang('group') . ' ' . lang('list'); ?>
				</a>
				<a href="<?= base_url('admin/items/import'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
					<?= lang('import') . ' ' . lang('items'); ?>
				</a>
			</div>
		</div>

		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="p-5 space-y-4">
				<div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
					<?php if (!empty($created)) { ?>
						<div class="rounded-xl border border-dashed border-border bg-muted/10 p-4">
							<form role="form" enctype="multipart/form-data" id="form" action="<?php echo base_url(); ?>admin/items/saved_manufacturer" method="post" class="grid gap-3">
								<input required type="text" name="manufacturer" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-secondary/30" placeholder="<?= lang('enter') . ' ' . lang('manufacturer') . ' ' . lang('name') ?>">
								<textarea name="description" rows="2" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-secondary/30" placeholder="<?= lang('description'); ?>"></textarea>
								<button type="submit" class="btn btn-sm btn-primary h-10"><?= lang('add'); ?></button>
							</form>
						</div>
					<?php } ?>

					<?php
					$all_manufacturerInfo = get_result('tbl_manufacturer');
					if (!empty($all_manufacturerInfo)) {
						foreach ($all_manufacturerInfo as $manufacturerInfo) {
							$id = $this->uri->segment(5);
							$is_editing = (!empty($id) && $id == $manufacturerInfo->manufacturer_id);
					?>
							<div class="rounded-xl border border-border bg-background p-4" id="table_manufacturer_<?= $manufacturerInfo->manufacturer_id ?>">
								<div class="flex items-start justify-between flex-wrap gap-4">
									<div class="flex-1 space-y-3">
										<?php if ($is_editing) { ?>
											<form method="post" action="<?= base_url() ?>admin/items/saved_manufacturer/<?php if (!empty($manufacturer_info)) { echo $manufacturer_info->manufacturer_id; } ?>" class="space-y-3">
												<input type="text" name="manufacturer" value="<?php if (!empty($manufacturer_info)) { echo $manufacturer_info->manufacturer; } ?>" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-secondary/30" placeholder="<?= lang('enter') . ' ' . lang('group') . ' ' . lang('name') ?>" required>
												<textarea name="description" rows="2" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-secondary/30"><?php if (!empty($manufacturer_info)) { echo $manufacturer_info->description; } ?></textarea>
												<div class="flex items-center gap-2">
													<?= btn_update(); ?>
													<?= btn_cancel('admin/items/items_manufacturerlist/manufacturer/', 'cancel', 'md', 'x', array('class' => 'mb-0 h-9')); ?>
												</div>
											</form>
										<?php } else { ?>
											<div>
												<div class="text-sm font-semibold text-foreground"><?= html_escape($manufacturerInfo->manufacturer); ?></div>
												<div class="text-sm text-muted-foreground mt-1"><?= !empty($manufacturerInfo->description) ? html_escape($manufacturerInfo->description) : '-'; ?></div>
											</div>
										<?php } ?>
									</div>
									<?php if (!$is_editing && (!empty($edited) || !empty($deleted))) { ?>
										<div class="flex items-center gap-2">
											<?php if (!empty($edited)) { ?>
												<?= btn_edit('admin/items/items_manufacturerlist/manufacturer/' . $manufacturerInfo->manufacturer_id); ?>
											<?php } ?>
											<?php if (!empty($deleted)) { ?>
												<?php echo ajax_anchor(base_url("admin/items/delete_manufacturer/" . $manufacturerInfo->manufacturer_id), '', array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_items_group_" . $manufacturerInfo->manufacturer_id)); ?>
											<?php } ?>
										</div>
									<?php } ?>
								</div>
							</div>
						<?php }
					} ?>
				</div>
			</div>
		</div>
	</div>
<?php } ?>
