<?php if (isset(restaurant()->is_multi_lang)): ?>
	<div class="row">
		<?php $multilang = isset(restaurant()->is_multi_lang) && restaurant()->is_multi_lang == 1 ? 1 : 0; ?>
		<?php if ($multilang == 0): ?>
			<div class="col-md-8">
				<div class="card">
					<div class="card-body flex_between">
						<h4><?= lang('enable_multi_lang_category_items'); ?></h4> <a href="<?= base_url("admin/auth/enable_category"); ?>" class="btn btn-secondary action_btn"><?= lang('enable'); ?></a>
					</div>
				</div>
			</div>
			<?php $add_new_url = base_url("admin/menu/create_item"); ?>
			<?php $lang = isset($_GET['lang']) ? $_GET['lang'] : "english"; ?>
		<?php else: ?>
			<?php
			$controller = $this->uri->rsegment(1); // The Controller
			$function = $this->uri->rsegment(2);
			$params = $this->uri->rsegment(3);
			$lang = isset($_GET['lang']) ? $_GET['lang'] : site_lang();
			?>
			<?php if (isset($is_create) && $is_create == 0): ?>
				<?php //include 'language_dropdown.php'; 
				?>
			<?php endif; ?>

			<?php $add_new_url = base_url("admin/menu/create_item/?lang={$lang}"); ?>
		<?php endif; ?>
	</div>
<?php else: ?>
	<?php $lang = isset($_GET['lang']) ? $_GET['lang'] : site_lang(); ?>
<?php endif; ?>

<div class="row">
	<?php include 'inc/limit_counter.php'; ?>
</div>


<div class="row ">
	<div class="col-md-12">
		<div class="card category-grid-card">
			<div class="card-header global-search-container border-0">
				<div class="modern-search-wrapper">
					<i class="icofont-search-1 global-search-icon"></i>
					<input type="text" class="form-control" id="search_items_global" placeholder="<?= lang('search_items_across_categories'); ?>..." value="<?= $_GET['q'] ?? ''; ?>">
				</div>
				<div class="box-tools text-center mt-10">
					<?php if (isset($active) && $active == 1): ?>
						<?php if (is_access('add') == 1): ?>
							<a href="<?= $add_new_url ?? ''; ?>" class="btn btn-secondary action_btn"><i class="fa fa-plus"></i> &nbsp;<?= !empty(lang('add_new_item')) ? lang('add_new_item') : "Add New Item"; ?> </a>
						<?php endif; ?>
					<?php endif; ?>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="card-body bg_gray">
				<div id="itemSearchResults" style="display: none;" class="mb-20">
					<div class="card">
						<div class="card-header flex_between">
							<h4></h4>
							<a href="javascript:;" class="btn btn-danger btn-sm closeSearch"><i class="fa fa-close"></i></a>
						</div>
						<div class="card-body" id="searchLoadData">

						</div>
					</div>
				</div>

				<div class="table-responsives categoryListArea">
					<div class="row">
						<?php if (!empty($menu_type)): ?>
							<?php foreach ($menu_type as $key => $cat): ?>
								<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 category-card-wrapper <?= multi_lang(auth('id'), $cat); ?>">
									<a href="<?= base_url("admin/menu/item_list/" . multi_lang(auth('id'), $cat) . "?lang={$lang}"); ?>" class="cat-link">
										<div class="single_cat">
											<div class="cat-image-container">
												<img src="<?= get_img($cat['thumb'], '', 1); ?>" alt="<?= $cat['name']; ?>">
											</div>
											<div class="catDetails">
												<h4><?= $cat['name']; ?></h4>

												<div class="cat-footer">
													<div class="item-count-badge">
														<?php if (isset(restaurant()->is_multi_lang) && restaurant()->is_multi_lang == 1): ?>
															<?= $this->admin_m->get_total_item_by_cat_id_ln(multi_lang(auth('id'), $cat), $lang); ?>
														<?php else: ?>
															<?= $this->admin_m->get_total_item_by_cat_id($cat['id']); ?>
														<?php endif; ?>
														<span><?= lang('items'); ?></span>
														<i class="icofont-cherry"></i>
													</div>
												</div>
											</div>

											<?php $top_items = $this->admin_m->get_top_items_by_cat_id(multi_lang(auth('id'), $cat)); ?>
											<?php if (!empty($top_items)): ?>
												<div class="cat-items-preview">
													<?php foreach ($top_items as $item): ?>
														<div class="preview-item-column">
															<div class="preview-item-square">
																<img src="<?= get_img($item['thumb'], $item['img_url'], $item['img_type']); ?>" alt="">
															</div>
															<span class="preview-item-name"><?= character_limiter($item['title'], 10); ?></span>
														</div>
													<?php endforeach; ?>
												</div>
											<?php endif; ?>
										</div>
									</a>
								</div>
							<?php endforeach ?>
						<?php else: ?>
							<div class="row mt-10 justify-content-center">
								<div class="col-md-6 col-sm-10">
									<div class="card modern-import-card">
										<div class="card-body">
											<div class="import-icon mb-15">
												<i class="icofont-download fa-3x text-primary"></i>
											</div>
											<h4 class="mb-20"><?= !empty(lang('import_default_items')) ? lang('import_default_items') : "No Categories Found. Import defaults?"; ?></h4>
											<a href="<?= base_url("admin/menu/import_default_items"); ?>" class="btn btn-primary action_btn btn-lg"><?= __('import_now'); ?></a>
										</div>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</div>

				</div>
			</div><!-- /.box-body -->
		</div>
	</div>

	<script>
		$(document).ready(function() {
			var searchTimer;
			$(document).on('keyup', '#search_items_global', function() {
				var q = $(this).val();
				clearTimeout(searchTimer);

				if (q.length > 2) {
					searchTimer = setTimeout(function() {
						$('#itemSearchResults').show();
						$('.categoryListArea').hide();
						$('#searchLoadData').html('<div class="text-center p-20"><i class="fa fa-spinner fa-spin fa-2x"></i></div>');

						var url = `<?= asset_url('admin/menu/search_item'); ?>?q=${q}&lang=<?= $lang; ?>`;
						$.get(url, function(json) {
							if (json.st == 1) {
								$('#searchLoadData').html(json.load_data);
							}
						}, 'json');
					}, 250);
				} else {
					$('.categoryListArea').show();
					$('#itemSearchResults').hide();
				}
			});

			$(document).on('click', '.closeSearch', function() {
				$('#search_items_global').val('');
				$('.categoryListArea').show();
				$('#itemSearchResults').hide();
			});
		});
	</script>
</div>