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

<?php
$created = can_action('58', 'created');
$edited = can_action('58', 'edited');
$deleted = can_action('58', 'deleted');
$bug_statuses = isset($bug_statuses) ? $bug_statuses : $this->bugs_model->get_statuses();

$status_filters = array(
	'' => lang('all'),
	'assigned_to_me' => lang('assigned_to_me'),
);
if (!empty($bug_statuses)) {
	foreach ($bug_statuses as $status) {
		$status_filters[$status['value']] = $status['name'];
	}
}
?>

<div class="space-y-4">
	<?= message_box('success'); ?>
	<?= message_box('error'); ?>

	<div id="bugs_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" id="DataTables" cellspacing="0" width="100%">
				<thead>
					<tr>
						<th><?= lang('bug_title') ?></th>
						<th><?= lang('date') ?></th>
						<th><?= lang('status') ?></th>
						<th><?= lang('severity') ?></th>
						<?php if ($this->session->userdata('user_type') == '1') { ?>
							<th><?= lang('reporter') ?></th>
						<?php } ?>
						<th><?= lang('assigned_to') ?></th>
						<?php $show_custom_fields = custom_form_table(6, null);
						if (!empty($show_custom_fields)) {
							foreach ($show_custom_fields as $c_label => $v_fields) {
								if (!empty($c_label)) { ?>
									<th><?= $c_label ?> </th>
							<?php }
							}
						}
						?>
						<?php if (!empty($edited) || !empty($deleted)) { ?>
							<th class="col-options no-sort text-right"><?= lang('action') ?></th>
						<?php } ?>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</div>
	</div>
</div>

<script>
	list = base_url + "admin/bugs/bugsList";
	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 } ?>

	function reloadWith(filter, searchType) {
		var url = list;
		if (filter) {
			url += "/" + filter;
			if (searchType && searchType !== 'status') {
				url += "/" + searchType;
			} else if (searchType === 'status' && filter !== 'assigned_to_me') {
				url += "";
			}
		}
		if (typeof table_url !== "undefined") {
			table_url(url);
		} else if (typeof fire_datatable === "function") {
			fire_datatable(url);
		}
	}

	$(function() {
		$(document).on('click', '[data-bug-filter-action]', function() {
			if ($(this).is('[data-bug-status-card]')) {
				var statusFilter = $(this).data('bug-filter') || '';
				reloadWith(statusFilter, '');
				return;
			}
			var $item = $(this);
			var $dropdown = $item.closest('details');
			if ($dropdown.length) {
				$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('bug-filter-label') || '<?= html_escape(lang('all')); ?>');
				$dropdown.prop('open', false);
			}
			var filter = $item.data('bug-filter') || '';
			var searchType = $item.data('bug-search') || '';
			if (searchType === 'status') {
				searchType = '';
			}
			reloadWith(filter, searchType);
		});

		window.bugProjectFilter = function(id) {
			reloadWith(id, 'by_project');
		};
		window.bugOpportunityFilter = function(id) {
			reloadWith(id, 'by_opportunity');
		};
		window.bugReporterFilter = function(id) {
			reloadWith(id, 'from_reporter');
		};
		window.bugStaffFilter = function(id) {
			reloadWith(id, 'by_staff');
		};
	});

	$(document).ready(function() {
		ins_data(base_url + 'admin/bugs/bugs_state_report');
	});
</script>
