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

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

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

$status_filters = array(
	'' => lang('all'),
	'assigned_to_me' => lang('assigned_to_me'),
	'open' => lang('open'),
	'in_progress' => lang('in_progress'),
	'answered' => lang('answered'),
	'closed' => lang('closed'),
);
if (!empty($ticket_statuses)) {
	foreach ($ticket_statuses as $status_row) {
		if (!empty($status_row->status)) {
			$status_filters[$status_row->status] = lang($status_row->status);
		}
	}
}
?>

<div class="space-y-4">
	<div id="tickets_state_report_div"></div>

	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="table-responsive p-4">
			<table class="table table-striped DataTables bulk_table" id="DataTables" cellspacing="0" width="100%">
				<thead>
					<tr>
						<?php if (!empty($deleted)) { ?>
							<th data-orderable="false">
								<div class="checkbox c-checkbox">
									<label class="needsclick">
										<input id="select_all" type="checkbox">
										<!-- <span data-lucide="check"></span> -->
									</label>
								</div>
							</th>
						<?php } ?>
						<th><?= lang('ticket_code') ?></th>
						<th><?= lang('subject') ?></th>
						<th class="col-date"><?= lang('date') ?></th>
						<?php if ($this->session->userdata('user_type') == '1') { ?>
							<th><?= lang('reporter') ?></th>
						<?php } ?>
						<th><?= lang('department') ?></th>
						<th><?= lang('tags') ?></th>
						<th><?= lang('status') ?></th>
						<?php $show_custom_fields = custom_form_table(7, null);
						if (!empty($show_custom_fields)) {
							foreach ($show_custom_fields as $c_label => $v_fields) {
								if (!empty($c_label)) { ?>
									<th><?= $c_label ?></th>
						<?php }
							}
						}
						?>
						<th class="col-options no-sort text-right"><?= lang('action') ?></th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</div>
	</div>
</div>

<script>
	list = base_url + "admin/tickets/ticketsList";
	window.erpDatatablePreload = window.erpDatatablePreload || {};
	window.erpDatatablePreload.DataTables = window.erpDatatablePreload.DataTables || {};
	<?php if (!empty($datatable_preload)) { ?>
	window.erpDatatablePreload.DataTables["list"] = <?= json_encode($datatable_preload, JSON_UNESCAPED_SLASHES) ?>;
	<?php } ?>
	bulk_url = base_url + "admin/tickets/bulk_delete";

	function reloadTickets(filter, searchType) {
		var url = list;
		if (filter) {
			url += "/" + filter;
			if (searchType) {
				url += "/" + searchType;
			}
		}
		if (typeof table_url !== "undefined") {
			table_url(url);
		} else if (typeof fire_datatable === "function") {
			fire_datatable(url);
		}
	}

	$(function() {
		$(document).on('click', '[data-ticket-filter-action]', function() {
			var $item = $(this);
			var $dropdown = $item.closest('details');
			$dropdown.find('[data-header-filter-item]').removeClass('bg-indigo-50 text-indigo-700 dark:bg-indigo-900/30 dark:text-indigo-300');
			$dropdown.find('[data-header-filter-item]').addClass('text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-800');
			$item.addClass('bg-indigo-50 text-indigo-700 dark:bg-indigo-900/30 dark:text-indigo-300');
			$item.removeClass('text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-800');
			$dropdown.find('[data-header-filter-selected]').text($item.data('ticket-filter-label') || '<?= html_escape(lang('all')); ?>');
			$dropdown.prop('open', false);
			var filter = $item.data('ticket-filter') || '';
			var searchType = $item.data('ticket-search') || '';
			reloadTickets(filter, searchType);
		});

		window.ticketProjectFilter = function(id) {
			reloadTickets(id, 'by_project');
		};
		window.ticketReporterFilter = function(id) {
			reloadTickets(id, 'by_reported');
		};
		window.ticketDepartmentFilter = function(id) {
			reloadTickets(id, 'by_department');
		};

		ins_data(base_url + 'admin/tickets/tickets_state_report');
	});
</script>
