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

<div class="rounded-2xl border border-border bg-card shadow-sm">
    <div class="flex items-center justify-between border-b border-border px-5 py-4">
        <h4 class="text-sm font-semibold text-foreground"><?= lang('complaints') ?></h4>
    </div>

    <div class="p-5">
        <div class="table-responsive">
            <table class="table table-striped DataTables w-full" id="complaints_table">
                <thead class="text-xs uppercase text-muted-foreground">
                    <tr>
                        <th class="px-3 py-2"><?= lang('complaint') . ' ' . lang('code') ?></th>
                        <th class="px-3 py-2"><?= lang('description') ?></th>
                        <th class="px-3 py-2"><?= lang('type') ?></th>
                        <th class="px-3 py-2"><?= lang('complaint') . ' ' . lang('date') ?></th>
                        <th class="px-3 py-2"><?= lang('due_date') ?></th>
                        <th class="px-3 py-2"><?= lang('status') ?></th>
                        <th class="px-3 py-2"><?= lang('action') ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $complaints = join_data('tbl_complaints', 'tbl_complaints.*,tbl_complaints_types.name as complaints_type_name',
                        array('tbl_complaints.client' =>  $profile_info->user_id),
                        array('tbl_complaints_types' => 'tbl_complaints_types.id=tbl_complaints.ticket_sub_type'),
                        'object');

                    if (!empty($complaints)) {
                        foreach ($complaints as $complaint) {
                            $status_class = 'bg-emerald-100 text-emerald-700';
                            if ($complaint->status == 'open') {
                                $status_class = 'bg-sky-100 text-sky-700';
                            } elseif ($complaint->status == 'closed') {
                                $status_class = 'bg-rose-100 text-rose-700';
                            }
                    ?>
                            <tr class="border-b border-border">
                                <td class="px-3 py-2"><?= $complaint->ticket_code ?></td>
                                <td class="px-3 py-2">
                                    <a class="text-primary hover:underline" href="<?= base_url('admin/complaints/details/' . $complaint->tickets_id) ?>">
                                        <?= $complaint->subject ?>
                                    </a>
                                </td>
                                <td class="px-3 py-2">
                                    <span class="inline-flex items-center rounded-full bg-muted px-2 py-0.5 text-xs font-semibold text-muted-foreground"><?= $complaint->complaints_type_name ?></span>
                                </td>
                                <td class="px-3 py-2"><?= display_date($complaint->lodged_date) ?></td>
                                <td class="px-3 py-2"><?= display_date($complaint->due_date) ?></td>
                                <td class="px-3 py-2">
                                    <span class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-semibold <?= $status_class; ?>"><?= lang($complaint->status) ?></span>
                                </td>
                                <td class="px-3 py-2">
                                    <a href="<?= base_url('admin/complaints/details/' . $complaint->tickets_id) ?>"
                                       class="inline-flex items-center rounded-md border border-border px-2.5 py-1 text-xs font-semibold text-foreground hover:bg-muted" data-toggle="tooltip" data-placement="top" title="<?= lang('view') ?>">
                                        <i data-lucide="eye"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php }
                    } else { ?>
                        <tr>
                            <td colspan="7" class="px-3 py-6 text-center text-sm text-muted-foreground"><?= lang('no_data_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

