
<?php
$bug_statuses = isset($bug_statuses) ? $bug_statuses : $this->bugs_model->get_statuses();
$status_filters = array(
    '' => lang('all'),
);
if (!empty($bug_statuses)) {
    foreach ($bug_statuses as $status) {
        $status_filters[$status['value']] = $status['name'];
    }
}
?>

<div class="space-y-4">
    <?= message_box('success'); ?>
    <?= message_box('error'); ?>

    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="flex flex-wrap items-center gap-3 border-b border-border px-4 py-3">
            <div class="flex flex-wrap items-center gap-2">
                <?php foreach ($status_filters as $key => $label) { ?>
                    <button type="button" class="bug-filter rounded-full border border-border px-3 py-1.5 text-xs font-semibold text-foreground hover:border-primary <?= $key === '' ? 'bg-primary text-white border-primary' : ''; ?>"
                        data-filter="<?= html_escape($key); ?>">
                        <?= $label; ?>
                    </button>
                <?php } ?>
            </div>
        </div>

        <div class="table-responsive p-4">
            <table class="table table-striped DataTables" id="DataTables" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th><?= lang('issue_#') ?></th>
                        <th><?= lang('bug_title') ?></th>
                        <th><?= lang('status') ?></th>
                        <th><?= lang('priority') ?></th>
                        <?php $show_custom_fields = custom_form_table(6, null);
                        if (!empty($show_custom_fields)) {
                            foreach ($show_custom_fields as $c_label => $v_fields) {
                                if (!empty($c_label)) { ?>
                                    <th><?= $c_label ?> </th>
                        <?php }
                            }
                        }
                        ?>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>

</div>

<script>
    list = base_url + "client/bugs/bugsList";

    function reloadWith(filter) {
        var url = list;
        if (filter) {
            url += "/" + filter;
        }
        if (typeof table_url !== "undefined") {
            table_url(url);
        } else if (typeof fire_datatable === "function") {
            fire_datatable(url);
        }
    }

    $(function() {
        $(document).on('click', '.bug-filter', function() {
            $('.bug-filter').removeClass('bg-primary text-white border-primary');
            $(this).addClass('bg-primary text-white border-primary');
            var filter = $(this).data('filter') || '';
            reloadWith(filter);
        });

    });
</script>
