<?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();

$this->load->helper('string');
$bug_id = !empty($bug_info->bug_id) ? $bug_info->bug_id : null;
$issue_no = !empty($bug_info->issue_no) ? $bug_info->issue_no : strtoupper(random_string('alnum', 7));

$project_id = !empty($bug_info->project_id) ? $bug_info->project_id : (!empty($project_id) ? $project_id : '');
$opportunities_id = !empty($bug_info->opportunities_id) ? $bug_info->opportunities_id : (!empty($opportunities_id) ? $opportunities_id : '');
$related_selected = !empty($project_id) ? 'project' : (!empty($opportunities_id) ? 'opportunities' : '0');

$type = $this->uri->segment(4);
$primary_contact = null;
if (!empty($type) && !is_numeric($type)) {
	$ex = explode('_', $type);
	if (!empty($ex[0]) && $ex[0] == 'c') {
		$primary_contact = $ex[1];
	}
}
$reporter_id = !empty($bug_info->reporter) ? $bug_info->reporter : (!empty($primary_contact) ? $primary_contact : '');
$reporter_label = !empty($reporter_label) ? $reporter_label : null;
if (!empty($reporter_id) && empty($reporter_label)) {
	$reporter_profile = $this->db->where('user_id', $reporter_id)->get('tbl_account_details')->row();
	$reporter_label = !empty($reporter_profile->fullname) ? $reporter_profile->fullname : null;
}

$permissionL = !empty($bug_info->permission) ? $bug_info->permission : null;
$permission_user = !empty($assign_user) ? $assign_user : array();

$priority_options = array();
$priorities = $this->db->get('tbl_priority')->result();
if (!empty($priorities)) {
	foreach ($priorities as $v_priorities) {
		$priority_options[$v_priorities->priority] = $v_priorities->priority;
	}
}

$severity_options = array(
	'minor' => lang('minor'),
	'major' => lang('major'),
	'show_stopper' => lang('show_stopper'),
	'must_be_fixed' => lang('must_be_fixed'),
);

$status_options = array();
if (!empty($bug_statuses)) {
	foreach ($bug_statuses as $status) {
		$status_options[$status['value']] = $status['name'];
	}
}

if (!empty($created) || !empty($edited)) {
?>
	<div class="space-y-6">
		<?= message_box('success'); ?>
		<?= message_box('error'); ?>

		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="px-6 py-4 border-b border-border flex flex-col gap-1 md:flex-row md:items-center md:justify-between">
				<div>
					<h2 class="text-lg font-semibold text-foreground"><?= !empty($bug_id) ? lang('updates') : lang('new_bugs'); ?></h2>
					<p class="text-sm text-muted-foreground"><?= lang('bug_management'); ?></p>
				</div>
				<div class="text-xs text-muted-foreground">
					<?= lang('issue_#'); ?> <span class="font-semibold text-foreground"><?= html_escape($issue_no); ?></span>
				</div>
			</div>
			<div class="p-6">
				<form data-parsley-validate action="<?= base_url('admin/bugs/save_bug/' . $bug_id); ?>" method="post" class="space-y-6">
					<?php if (!empty($project_id) && empty($bug_info->project_id)) { ?>
						<input type="hidden" name="un_project_id" value="<?= html_escape($project_id); ?>">
					<?php } ?>
					<?php if (!empty($opportunities_id) && empty($bug_info->opportunities_id)) { ?>
						<input type="hidden" name="un_opportunities_id" value="<?= html_escape($opportunities_id); ?>">
					<?php } ?>

					<div class="grid grid-cols-1 gap-6 xl:grid-cols-12">
						<div class="space-y-6 xl:col-span-8">
							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('bug_details'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
										<?= form_input_group('issue_no', lang('issue_#'), $issue_no, 'text', true); ?>
										<?= form_input_group('bug_title', lang('bug_title'), (!empty($bug_info->bug_title) ? $bug_info->bug_title : ''), 'text', true); ?>
										<div class="md:col-span-2">
											<?= form_select_group('related_to', lang('related_to'), array(
												'0' => lang('none'),
												'project' => lang('project'),
												'opportunities' => lang('opportunities'),
											), $related_selected, false, array('id' => 'check_related', 'onchange' => 'toggleBugRelated(this.value);')); ?>
										</div>
										<div id="related_project" class="md:col-span-2 <?= $related_selected === 'project' ? '' : 'hidden'; ?>">
											<?= form_lookup_group('project_id', lang('project'), $project_id, $project_label ?? '', false, array(), '', array('type' => 'project')); ?>
										</div>
										<div id="related_opportunity" class="md:col-span-2 <?= $related_selected === 'opportunities' ? '' : 'hidden'; ?>">
											<?= form_lookup_group('opportunities_id', lang('opportunities'), $opportunities_id, $opportunity_label ?? '', false, array(), '', array('type' => 'opportunity')); ?>
										</div>
										<div class="md:col-span-2">
											<?= form_lookup_group('reporter', lang('reporter'), $reporter_id, $reporter_label, true, array(), '', array('type' => 'user')); ?>
										</div>
									</div>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('description'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_editor('bug_description', lang(''), (!empty($bug_info->bug_description) ? $bug_info->bug_description : ''), false, array('class' => 'textarea_', 'data-height' => '260px')); ?>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('reproducibility'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_textarea_group('reproducibility', lang('reproducibility'), (!empty($bug_info->reproducibility) ? $bug_info->reproducibility : ''), 4, false, array('class' => 'textarea')); ?>
								</div>
							</div>
						</div>

						<div class="space-y-6 xl:col-span-4">
							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('status'); ?></h3>
								</div>
								<div class="p-5 space-y-4">
									<?= form_select_group('priority', lang('priority'), $priority_options, (!empty($bug_info->priority) ? $bug_info->priority : ''), true); ?>
									<?= form_select_group('severity', lang('severity'), $severity_options, (!empty($bug_info->severity) ? $bug_info->severity : ''), true); ?>
									<?= form_select_group('bug_status', lang('bug_status'), $status_options, (!empty($bug_info->bug_status) ? $bug_info->bug_status : 'unconfirmed'), true); ?>
									<?php if (!empty($project_id)) { ?>
										<div class="flex items-center justify-between rounded-lg border border-border bg-muted/40 px-3 py-2">
											<span class="text-sm text-foreground"><?= lang('visible_to_client'); ?></span>
											<input data-toggle="toggle" name="client_visible" value="Yes" <?php
											if (!empty($bug_info) && $bug_info->client_visible == 'Yes') {
												echo 'checked';
											}
											?> data-on="<?= lang('yes') ?>" data-off="<?= lang('no') ?>" data-onstyle="success" data-offstyle="danger" type="checkbox">
										</div>
									<?php } ?>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('custom_field'); ?></h3>
								</div>
								<div class="p-5">
									<?= custom_form_Fields(6, $bug_id); ?>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('assined_to'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_permission_modern($permission_user, $permissionL, lang('assined_to'), true); ?>
								</div>
							</div>
						</div>
					</div>

					<div class="flex items-center justify-end gap-2">
						<?php if (!empty($bug_info)) { ?>
							<?= btn_update(lang('updates'), 'md'); ?>
							<?= btn_cancel('admin/bugs', 'cancel', 'md', 'x', array('class' => 'mb-0 h-9')); ?>
						<?php } else { ?>
							<?= btn_update(lang('save'), 'md'); ?>
						<?php } ?>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php } ?>

<script>
	function toggleBugRelated(value) {
		var projectWrap = document.getElementById('related_project');
		var oppWrap = document.getElementById('related_opportunity');
		if (projectWrap) {
			projectWrap.classList.toggle('hidden', value !== 'project');
			if (value !== 'project') {
				var projectHidden = projectWrap.querySelector('[data-lookup-hidden]');
				var projectInput = projectWrap.querySelector('[data-lookup-input]');
				if (projectHidden) projectHidden.value = '';
				if (projectInput) projectInput.value = '';
			}
		}
		if (oppWrap) {
			oppWrap.classList.toggle('hidden', value !== 'opportunities');
			if (value !== 'opportunities') {
				var oppHidden = oppWrap.querySelector('[data-lookup-hidden]');
				var oppInput = oppWrap.querySelector('[data-lookup-input]');
				if (oppHidden) oppHidden.value = '';
				if (oppInput) oppInput.value = '';
			}
		}
	}

	document.addEventListener('DOMContentLoaded', function() {
		var related = document.getElementById('check_related');
		if (related) {
			toggleBugRelated(related.value);
		}
		if (window.initEditors) {
			window.initEditors(document);
		}
		ins_data(base_url + 'admin/bugs/bugs_state_report');
	});
</script>
