<?php
$edited = can_action('56', 'edited');
$all_bugs_info = get_result('tbl_bug', array('opportunities_id' => $opportunity_details->opportunities_id));

?>

<div class="space-y-4 pt-4">
    <div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/50">
        <div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
            <div>
                <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('all_bugs') ?></h3>
                <p class="text-xs text-slate-500 dark:text-slate-400"><?= count((array) $all_bugs_info) ?> <?= lang('all_bugs') ?></p>
            </div>
            <?php if (!empty($edited)) { ?>
                <a href="<?= base_url() ?>admin/bugs/create/opportunities/<?= $opportunity_details->opportunities_id ?>"
                   class="inline-flex items-center gap-2 rounded-lg bg-slate-900 px-3 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
                    <i data-lucide="plus" class="h-4 w-4"></i>
                    <?= lang('new_bugs') ?>
                </a>
            <?php } ?>
        </div>

        <div class="overflow-x-auto">
            <table id="table-milestones" class="min-w-full text-sm">
                <thead class="bg-slate-200 text-slate-500 dark:bg-slate-800/60 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('bug_title') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('priority') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('reporter') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('action') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($all_bugs_info)) : foreach ($all_bugs_info as $v_bugs) : ?>
                            <?php
                            $reporter = $this->db->where('user_id', $v_bugs->reporter)->get('tbl_users')->row();
                            if (!empty($reporter) && $reporter->role_id == '1') {
                                $reporter_class = 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
                            } elseif (!empty($reporter) && $reporter->role_id == '2') {
                                $reporter_class = 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
                            } else {
                                $reporter_class = 'bg-indigo-100 text-indigo-700 dark:bg-indigo-900/40 dark:text-indigo-300';
                            }

                            if ($v_bugs->bug_status == 'unconfirmed') {
                                $status_class = 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
                            } elseif ($v_bugs->bug_status == 'confirmed') {
                                $status_class = 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
                            } elseif ($v_bugs->bug_status == 'in_progress') {
                                $status_class = 'bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300';
                            } else {
                                $status_class = 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
                            }
                            ?>
                            <tr id="table-bugs-<?= $v_bugs->bug_id ?>">
                                <td class="px-4 py-3">
                                    <a href="<?= base_url() ?>admin/bugs/details/<?= $v_bugs->bug_id ?>"
                                       class="font-medium text-info hover:underline dark:text-sky-300 dark:hover:text-sky-200 <?= ($v_bugs->bug_status == 'resolve') ? 'line-through opacity-70' : '' ?>">
                                        <?= html_escape($v_bugs->bug_title) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_class ?>">
                                        <?= lang($v_bugs->bug_status) ?>
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-slate-700 dark:text-slate-200">
                                    <?= ucfirst($v_bugs->priority) ?>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex rounded-full px-2.5 py-1 text-xs font-semibold <?= $reporter_class ?>">
                                        <?= !empty($reporter->username) ? html_escape($reporter->username) : lang('unknown') ?>
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex flex-wrap items-center gap-0">
                                        <?= btn_edit('admin/bugs/create/' . $v_bugs->bug_id) ?>
                                        <?= ajax_anchor(
                                            base_url("admin/bugs/delete_bug/" . $v_bugs->bug_id),
                                            "",
                                            array(
                                                "class" => "inline-flex items-center justify-center rounded-lg border border-rose-200 bg-rose-50 p-2 text-rose-600 hover:bg-rose-100 dark:border-rose-900/50 dark:bg-rose-900/20 dark:text-rose-300 dark:hover:bg-rose-900/40",
                                                "title" => lang('delete'),
                                                "data-fade-out-on-success" => "#table-bugs-" . $v_bugs->bug_id
                                            )
                                        ); ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr>
                            <td colspan="5" class="px-4 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
                                <?= lang('no_data_found') ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
