<?php
$reporter_user = !empty($bug_details->reporter)
    ? $this->db->where('user_id', $bug_details->reporter)->get('tbl_users')->row()
    : null;
$reporter_profile = !empty($bug_details->reporter)
    ? $this->db->where('user_id', $bug_details->reporter)->get('tbl_account_details')->row()
    : null;
$project_info = !empty($bug_details->project_id)
    ? $this->db->where('project_id', $bug_details->project_id)->get('tbl_project')->row()
    : null;
$opportunity_info = !empty($bug_details->opportunities_id)
    ? $this->db->where('opportunities_id', $bug_details->opportunities_id)->get('tbl_opportunities')->row()
    : null;

$status_map = array(
    'unconfirmed' => 'bg-amber-50 text-amber-700 ring-amber-200 dark:bg-amber-900/30 dark:text-amber-200 dark:ring-amber-800',
    'confirmed' => 'bg-sky-50 text-sky-700 ring-sky-200 dark:bg-sky-900/30 dark:text-sky-200 dark:ring-sky-800',
    'in_progress' => 'bg-indigo-50 text-indigo-700 ring-indigo-200 dark:bg-indigo-900/30 dark:text-indigo-200 dark:ring-indigo-800',
    'resolved' => 'bg-violet-50 text-violet-700 ring-violet-200 dark:bg-violet-900/30 dark:text-violet-200 dark:ring-violet-800',
    'closed' => 'bg-emerald-50 text-emerald-700 ring-emerald-200 dark:bg-emerald-900/30 dark:text-emerald-200 dark:ring-emerald-800',
);
$priority_map = array(
    'High' => 'bg-rose-50 text-rose-700 ring-rose-200 dark:bg-rose-900/30 dark:text-rose-200 dark:ring-rose-800',
    'Medium' => 'bg-sky-50 text-sky-700 ring-sky-200 dark:bg-sky-900/30 dark:text-sky-200 dark:ring-sky-800',
    'Low' => 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700',
);
$severity_map = array(
    'must_be_fixed' => 'bg-rose-50 text-rose-700 ring-rose-200 dark:bg-rose-900/30 dark:text-rose-200 dark:ring-rose-800',
    'major' => 'bg-amber-50 text-amber-700 ring-amber-200 dark:bg-amber-900/30 dark:text-amber-200 dark:ring-amber-800',
    'minor' => 'bg-sky-50 text-sky-700 ring-sky-200 dark:bg-sky-900/30 dark:text-sky-200 dark:ring-sky-800',
);

$status_class = isset($status_map[$bug_details->bug_status]) ? $status_map[$bug_details->bug_status] : 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';
$priority_class = isset($priority_map[$bug_details->priority]) ? $priority_map[$bug_details->priority] : 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';
$severity_class = isset($severity_map[$bug_details->severity]) ? $severity_map[$bug_details->severity] : 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';

$reporter_name = !empty($reporter_profile->fullname) ? $reporter_profile->fullname : '-';
$update_time = !empty($bug_details->update_time) ? display_date($bug_details->update_time) . ' ' . display_time($bug_details->update_time) : '-';
$created_time = !empty($bug_details->created_time) ? display_date($bug_details->created_time) . ' ' . display_time($bug_details->created_time) : '-';
?>

<div class="space-y-6">
    <div class="overflow-hidden rounded-3xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
        <div class="border-b border-slate-200 bg-slate-50/80 px-6 py-5 dark:border-slate-700 dark:bg-slate-900/70">
            <div class="flex flex-wrap items-start justify-between gap-4">
                <div class="space-y-2">
                    <p class="text-xs font-semibold uppercase tracking-[0.24em] text-slate-400 dark:text-slate-500"><?= lang('bug_details') ?></p>
                    <h2 class="text-2xl font-semibold text-slate-900 dark:text-slate-100">
                        <?= !empty($bug_details->bug_title) ? html_escape($bug_details->bug_title) : '-' ?>
                    </h2>
                    <div class="flex flex-wrap items-center gap-2 text-sm text-slate-500 dark:text-slate-400">
                        <span><?= lang('issue_#') ?>: <span class="font-medium text-slate-700 dark:text-slate-200"><?= !empty($bug_details->issue_no) ? html_escape($bug_details->issue_no) : '-' ?></span></span>
                        <span class="h-1 w-1 rounded-full bg-slate-300 dark:bg-slate-600"></span>
                        <span><?= lang('project_name') ?>: <span class="font-medium text-slate-700 dark:text-slate-200"><?= !empty($project_info->project_name) ? html_escape($project_info->project_name) : '-' ?></span></span>
                    </div>
                </div>

                <div class="flex flex-wrap gap-2">
                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $status_class ?>">
                        <?= !empty($bug_details->bug_status) ? lang($bug_details->bug_status) : '-' ?>
                    </span>
                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $priority_class ?>">
                        <?= !empty($bug_details->priority) ? lang($bug_details->priority) : '-' ?>
                    </span>
                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $severity_class ?>">
                        <?= !empty($bug_details->severity) ? lang($bug_details->severity) : '-' ?>
                    </span>
                </div>
            </div>
        </div>

        <div class="p-6">
            <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
                <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-slate-400 dark:text-slate-500"><?= lang('reporter') ?></p>
                    <p class="mt-2 text-base font-semibold text-slate-900 dark:text-slate-100"><?= html_escape($reporter_name) ?></p>
                </div>
                <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-slate-400 dark:text-slate-500"><?= lang('created_date') ?></p>
                    <p class="mt-2 text-base font-semibold text-slate-900 dark:text-slate-100"><?= $created_time ?></p>
                </div>
                <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-slate-400 dark:text-slate-500"><?= lang('update_on') ?></p>
                    <p class="mt-2 text-base font-semibold text-slate-900 dark:text-slate-100"><?= $update_time ?></p>
                </div>
                <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-slate-400 dark:text-slate-500"><?= lang('opportunity_name') ?></p>
                    <p class="mt-2 text-base font-semibold text-slate-900 dark:text-slate-100"><?= !empty($opportunity_info->opportunity_name) ? html_escape($opportunity_info->opportunity_name) : '-' ?></p>
                </div>
            </div>

            <div class="mt-6 grid gap-6 lg:grid-cols-[minmax(0,1.25fr)_minmax(320px,0.75fr)]">
                <div class="space-y-6">
                    <div class="rounded-2xl border border-slate-200 bg-slate-50/60 p-5 dark:border-slate-700 dark:bg-slate-900/60">
                        <h3 class="text-sm font-semibold uppercase tracking-[0.2em] text-slate-500 dark:text-slate-400"><?= lang('details') ?></h3>
                        <dl class="mt-4 grid gap-4 sm:grid-cols-2">
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('issue_#') ?></dt>
                                <dd class="mt-1 text-sm font-medium text-slate-900 dark:text-slate-100"><?= !empty($bug_details->issue_no) ? html_escape($bug_details->issue_no) : '-' ?></dd>
                            </div>
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('bug_title') ?></dt>
                                <dd class="mt-1 text-sm font-medium text-slate-900 dark:text-slate-100"><?= !empty($bug_details->bug_title) ? html_escape($bug_details->bug_title) : '-' ?></dd>
                            </div>
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('bug_status') ?></dt>
                                <dd class="mt-2">
                                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $status_class ?>">
                                        <?= !empty($bug_details->bug_status) ? lang($bug_details->bug_status) : '-' ?>
                                    </span>
                                </dd>
                            </div>
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('project_name') ?></dt>
                                <dd class="mt-1 text-sm font-medium text-slate-900 dark:text-slate-100"><?= !empty($project_info->project_name) ? html_escape($project_info->project_name) : '-' ?></dd>
                            </div>
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('priority') ?></dt>
                                <dd class="mt-2">
                                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $priority_class ?>">
                                        <?= !empty($bug_details->priority) ? lang($bug_details->priority) : '-' ?>
                                    </span>
                                </dd>
                            </div>
                            <div>
                                <dt class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= lang('severity') ?></dt>
                                <dd class="mt-2">
                                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $severity_class ?>">
                                        <?= !empty($bug_details->severity) ? lang($bug_details->severity) : '-' ?>
                                    </span>
                                </dd>
                            </div>
                        </dl>
                    </div>

                    <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                        <h3 class="text-sm font-semibold uppercase tracking-[0.2em] text-slate-500 dark:text-slate-400"><?= lang('description') ?></h3>
                        <div class="prose prose-sm mt-4 max-w-none text-slate-600 dark:prose-invert dark:text-slate-300">
                            <?php if (!empty($bug_details->bug_description)) { ?>
                                <?= $bug_details->bug_description ?>
                            <?php } else { ?>
                                <p><?= lang('no_data_found') ?></p>
                            <?php } ?>
                        </div>
                    </div>

                    <?php
                    $show_custom_fields = custom_form_label(6, $bug_details->bug_id);
                    if (!empty($show_custom_fields)) {
                    ?>
                        <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                            <h3 class="text-sm font-semibold uppercase tracking-[0.2em] text-slate-500 dark:text-slate-400"><?= lang('custom_field') ?></h3>
                            <div class="mt-4 grid gap-4 sm:grid-cols-2">
                                <?php foreach ($show_custom_fields as $c_label => $v_fields) {
                                    if (empty($v_fields)) {
                                        continue;
                                    }
                                    $field_value = !empty(json_decode($v_fields))
                                        ? implode('<br/>', json_decode($v_fields))
                                        : $v_fields;
                                ?>
                                    <div class="rounded-xl border border-slate-200 bg-slate-50/70 p-4 dark:border-slate-700 dark:bg-slate-800/50">
                                        <p class="text-xs font-semibold uppercase tracking-wide text-slate-400 dark:text-slate-500"><?= html_escape($c_label) ?></p>
                                        <div class="mt-2 text-sm text-slate-700 dark:text-slate-200"><?= $field_value ?></div>
                                    </div>
                                <?php } ?>
                            </div>
                        </div>
                    <?php } ?>
                </div>

                <div class="space-y-6">
                    <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
                        <h3 class="text-sm font-semibold uppercase tracking-[0.2em] text-slate-500 dark:text-slate-400"><?= lang('participants') ?></h3>
                        <div class="mt-4">
                            <?php if ($bug_details->permission != 'all') {
                                $get_permission = json_decode($bug_details->permission);
                                if (!empty($get_permission)) { ?>
                                    <div class="flex flex-wrap gap-3">
                                        <?php foreach ($get_permission as $permission => $v_permission) {
                                            $user_info = $this->db->where(array('user_id' => $permission))->get('tbl_users')->row();
                                            $profile_info = $this->db->where(array('user_id' => $permission))->get('tbl_account_details')->row();
                                            $avatar = !empty($profile_info->avatar) ? base_url() . $profile_info->avatar : base_url('uploads/default_avatar.jpg');
                                            $fullname = !empty($profile_info->fullname) ? $profile_info->fullname : '-';
                                            $role_badge = !empty($user_info) && (int) $user_info->role_id === 1
                                                ? 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-200'
                                                : 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-200';
                                        ?>
                                            <div class="flex items-center gap-3 rounded-2xl border border-slate-200 bg-slate-50/70 px-4 py-3 dark:border-slate-700 dark:bg-slate-800/50">
                                                <img src="<?= $avatar ?>" class="h-10 w-10 rounded-full object-cover ring-2 ring-white dark:ring-slate-800" alt="<?= html_escape($fullname) ?>">
                                                <div>
                                                    <p class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= html_escape($fullname) ?></p>
                                                    <span class="inline-flex rounded-full px-2 py-0.5 text-xs font-medium <?= $role_badge ?>">
                                                        <?= !empty($user_info) && (int) $user_info->role_id === 1 ? lang('admin') : lang('staff') ?>
                                                    </span>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                <?php } else { ?>
                                    <p class="text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></p>
                                <?php }
                            } else { ?>
                                <div class="rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-4 text-sm text-emerald-700 dark:border-emerald-800 dark:bg-emerald-900/20 dark:text-emerald-200">
                                    <strong><?= lang('everyone') ?></strong>
                                    <span class="ml-1 text-emerald-600/80 dark:text-emerald-300/80"><?= lang('permission_for_all') ?></span>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
