<?php
$answered = 0;
$closed = 0;
$open = 0;
$in_progress = 0;

$progress_tickets_info = $this->tickets_model->get_permission('tbl_tickets');

// 30 days before

for ($iDay = 30; $iDay >= 0; $iDay--) {
    $date = date('Y-m-d', safe_strtotime('today - ' . $iDay . 'days'));
    $where = array('created >=' => $date . " 00:00:00", 'created <=' => $date . " 23:59:59");

    $tickets_result[$date] = count($this->db->where($where)->get('tbl_tickets')->result());
}

if (!empty($progress_tickets_info)):foreach ($progress_tickets_info as $v_tickets):
    if ($v_tickets->status == 'answered') {
        $answered += count(array($v_tickets->status));
    }
    if ($v_tickets->status == 'closed') {
        $closed += count(array($v_tickets->status));
    }
    if ($v_tickets->status == 'open') {
        $open += count(array($v_tickets->status));
    }
    if ($v_tickets->status == 'in_progress') {
        $in_progress += count(array($v_tickets->status));
    }
endforeach;
endif;
if ($this->session->userdata('user_type') == 1) {
    ?>
    <div class="rounded-2xl border border-border bg-card p-4 shadow-sm dark:bg-gray-900 dark:border-gray-700">
    <div class="grid gap-3 md:grid-cols-5">
        
        <button type="button" 
                class="ticket-state-filter rounded-xl border border-border bg-white p-4 text-left shadow-sm transition-colors hover:border-slate-300 dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500" 
                data-filter="answered">
            <p class="text-[11px] uppercase tracking-wide text-muted-foreground dark:text-gray-400">
                <?= lang('answered') . ' ' . lang('tickets') ?>
            </p>
            <p class="mt-2 text-2xl font-semibold text-foreground dark:text-gray-100">
                <?= $answered ?>
            </p>
        </button>

        <button type="button" 
                class="ticket-state-filter rounded-xl border border-border bg-white p-4 text-left shadow-sm transition-colors hover:border-slate-300 dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500" 
                data-filter="in_progress">
            <p class="text-[11px] uppercase tracking-wide text-muted-foreground dark:text-gray-400">
                <?= lang('in_progress') . ' ' . lang('tickets') ?>
            </p>
            <p class="mt-2 text-2xl font-semibold text-foreground dark:text-gray-100">
                <?= $in_progress ?>
            </p>
        </button>

        <button type="button" 
                class="ticket-state-filter rounded-xl border border-border bg-white p-4 text-left shadow-sm transition-colors hover:border-slate-300 dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500" 
                data-filter="open">
            <p class="text-[11px] uppercase tracking-wide text-muted-foreground dark:text-gray-400">
                <?= lang('open') . ' ' . lang('tickets') ?>
            </p>
            <p class="mt-2 text-2xl font-semibold text-foreground dark:text-gray-100">
                <?= $open ?>
            </p>
        </button>

        <button type="button" 
                class="ticket-state-filter rounded-xl border border-border bg-white p-4 text-left shadow-sm transition-colors hover:border-slate-300 dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500" 
                data-filter="closed">
            <p class="text-[11px] uppercase tracking-wide text-muted-foreground dark:text-gray-400">
                <?= lang('closed') . ' ' . lang('tickets') ?>
            </p>
            <p class="mt-2 text-2xl font-semibold text-foreground dark:text-gray-100">
                <?= $closed ?>
            </p>
        </button>

        <div class="rounded-xl border border-border bg-white p-4 text-center shadow-sm dark:bg-gray-800 dark:border-gray-700">
            <div data-sparkline="" 
                 data-bar-color="#23b7e5" 
                 data-height="60" 
                 data-bar-width="7"
                 data-bar-spacing="6" 
                 data-chart-range-min="0"
                 values="<?php
                 if (!empty($tickets_result)) {
                     foreach ($tickets_result as $v_tickets_result) {
                         echo $v_tickets_result . ',';
                     }
                 }
                 ?>"></div>
            <p class="mt-2 text-xs font-semibold text-muted-foreground dark:text-gray-400">
                <?= lang('last_30_days') ?>
            </p>
        </div>
    </div>
</div>
<?php } ?>
<script type="text/javascript">
    $('.ticket-state-filter').on('click', function () {
        var filter_by = $(this).data('filter') || '';
        if (typeof reloadTickets === 'function') {
            reloadTickets(filter_by, '');
            return;
        }
        if (typeof table_url !== 'undefined') {
            table_url(base_url + "admin/tickets/ticketsList/" + filter_by);
        }
    });
</script>
