<?php
$all_tickets_info = $this->db->where(array('project_id' => $project_details->project_id))->get('tbl_tickets')->result();
?>

<div class="space-y-4">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <div>
            <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('tickets') ?></h3>
            <p class="text-sm text-slate-500 dark:text-slate-400"><?= lang('tickets') ?> overview</p>
        </div>
        <a href="<?= base_url() ?>client/tickets/index/project_tickets/0/<?= $project_details->project_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">
            <i data-lucide="plus" class="h-4 w-4"></i>
            <?= lang('new_ticket') ?>
        </a>
    </div>

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
        <div class="overflow-x-auto overflow-y-visible">
            <table id="table-tickets" class="min-w-full text-sm">
                <thead class="bg-slate-50 text-slate-500 dark:bg-slate-800 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('ticket_code') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('subject') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('date') ?></th>
                        <?php if ($this->session->userdata('user_type') == '1') { ?>
                            <th class="px-4 py-3 text-left"><?= lang('reporter') ?></th>
                        <?php } ?>
                        <th class="px-4 py-3 text-left"><?= lang('department') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php
                    if (!empty($all_tickets_info)) {
                        foreach ($all_tickets_info as $v_tickets_info) {
                            if ($v_tickets_info->status == 'open') {
                                $s_label = 'bg-rose-50 text-rose-700';
                            } elseif ($v_tickets_info->status == 'closed') {
                                $s_label = 'bg-emerald-50 text-emerald-700';
                            } else {
                                $s_label = 'bg-slate-100 text-slate-700';
                            }
                            $profile_info = $this->db->where(array('user_id' => $v_tickets_info->reporter))->get('tbl_account_details')->row();
                            $dept_info = $this->db->where(array('departments_id' => $v_tickets_info->departments_id))->get('tbl_departments')->row();
                            $dept_name = !empty($dept_info) ? $dept_info->deptname : '-';
                            $status = $v_tickets_info->status == 'in_progress' ? 'In Progress' : $v_tickets_info->status;
                    ?>
                            <tr class="text-slate-700 transition hover:bg-slate-50/70 dark:text-slate-200 dark:hover:bg-slate-800/50">
                                <td class="px-4 py-3"><span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-200"><?= $v_tickets_info->ticket_code ?></span></td>
                                <td class="px-4 py-3"><a class="font-medium text-primary hover:underline dark:text-sky-300" href="<?= base_url() ?>client/tickets/index/tickets_details/<?= $v_tickets_info->tickets_id ?>"><?= $v_tickets_info->subject ?></a></td>
                                <td class="px-4 py-3"><?= display_date($v_tickets_info->created) ?></td>
                                <?php if ($this->session->userdata('user_type') == '1') { ?>
                                    <td class="px-4 py-3">
                                        <?php if (!empty($profile_info)) { ?>
                                            <div class="flex items-center gap-2">
                                                <img class="h-8 w-8 rounded-full" src="<?= base_url() . $profile_info->avatar ?>" alt="">
                                                <span><?= $profile_info->fullname ?></span>
                                            </div>
                                        <?php } else { echo '-'; } ?>
                                    </td>
                                <?php } ?>
                                <td class="px-4 py-3"><?= $dept_name ?></td>
                                <td class="px-4 py-3"><span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $s_label ?> dark:bg-slate-800/70 dark:text-slate-200"><?= ucfirst($status) ?></span></td>
                            </tr>
                    <?php
                        }
                    }
                    ?>
                    <?php if (empty($all_tickets_info)) { ?>
                        <tr>
                            <td colspan="6" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

