<?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('group') . ' ' . lang('list'); ?></h2>
				<p class="text-sm text-muted-foreground"><?= lang('items') . ' ' . lang('group'); ?></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/items_manufacturerlist'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
					<?= lang('manufacturer') . ' ' . 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 grid-cols-12">
					<div class="col-span-4">
						<?php if (!empty($created)) { 
							$id = $this->uri->segment(5);
							if (!empty($id)) {
								$group_info = $this->db->where('customer_group_id', $id)->get('tbl_customer_group')->row();
							}
							
							?>
							<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_group/<?php if (!empty($group_info)) {
																																						echo $group_info->customer_group_id;
																																					} ?>" method="post" class="grid gap-3">
									<input required type="text" name="customer_group" 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') ?>"
									   value="<?php if (!empty($group_info)) {
													echo $group_info->customer_group;
												} ?>">									
									<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'); ?>"><?php if (!empty($group_info)) {
																																	echo $group_info->description;
																																} ?></textarea>

									<?php if(!empty($group_info)){?>
									<div class="flex items-center gap-2 ">
										<?php echo btn_update();
										echo btn_cancel('admin/items/newitems_group/group/', 'cancel', 'md', 'x', array('class' => 'mb-0 h-9'));?>
									</div>	
									<?php } else{?>
									<button type="submit" class="btn btn-sm btn-primary h-9"><?= lang('add'); ?></button>
									<?php } ?>

								</form>
							</div>
						<?php } ?>
					</div>
					<div class="col-span-8 space-y-4">
						<div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
							<?php
							$all_customer_group = $this->db->where('type', 'items')->get('tbl_customer_group')->result();
							if (!empty($all_customer_group)) {
								foreach ($all_customer_group as $customer_group) {									
							?>
									<div class="rounded-xl border border-border bg-background p-4" id="table_items_group_<?= $customer_group->customer_group_id ?>">
										<div class="flex items-start justify-between flex-wrap gap-4">
											<div class="flex-1 space-y-3">
												
													<div>
														<div class="text-sm font-semibold text-foreground"><?= html_escape($customer_group->customer_group); ?></div>
														<div class="text-sm text-muted-foreground mt-1"><?= !empty($customer_group->description) ? html_escape($customer_group->description) : '-'; ?></div>
													</div>												
											</div>
											<?php if ((!empty($edited) || !empty($deleted))) { ?>
												<div class="flex items-center gap-2">
													<?php if (!empty($edited)) { ?>
														<?= btn_edit('admin/items/newitems_group/group/' . $customer_group->customer_group_id); ?>
													<?php } ?>
													<?php if (!empty($deleted)) { ?>
														<?php echo ajax_anchor(base_url("admin/items/delete_group/" . $customer_group->customer_group_id), '', array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_items_group_" . $customer_group->customer_group_id)); ?>
													<?php } ?>
												</div>
											<?php } ?>
										</div>
									</div>
							<?php }
							} ?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
<?php } ?>
