<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<link href="<?php echo base_url('assets/css/datatables/dataTables.bootstrap.css') ?>" rel="stylesheet">
<style>
.case-num  { font-family:monospace; font-size:12px; font-weight:600; color:#1e3a5f; }
.star-on   { color:#f39c12; font-size:15px; }
.star-off  { color:#ddd; font-size:15px; }
.star-on:hover, .star-off:hover { color:#f39c12; }
</style>

<section class="content-header">
    <h1><i class="fa fa-folder-open"></i> <?php echo $page_title ?> <small><?php echo lang('list') ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><?php echo lang('cases') ?></li>
    </ol>
</section>

<section class="content">

<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title"><i class="fa fa-folder-open"></i> Processos</h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(5)==1): ?>
            <a href="<?php echo site_url('admin/cases/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Novo Processo
            </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="box-body no-padding">
        <table id="example1" class="table table-hover" style="margin:0">
            <thead>
                <tr>
                    <th style="width:36px">#</th>
                    <th style="width:32px"></th>
                    <th>Título / Fase</th>
                    <th>Número</th>
                    <th>Cliente</th>
                    <th style="width:130px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($cases)): $i=1; foreach($cases as $new):
                // Suporte a case_id ou id
                $cid = isset($new->case_id) ? $new->case_id : (isset($new->id) ? $new->id : 0);
                $starred = isset($new->star) ? $new->star : 0;
            ?>
            <tr>
                <td><?php echo $i++ ?></td>
                <td style="text-align:center;vertical-align:middle">
                    <?php if($starred == 1): ?>
                    <a href="<?php echo site_url('admin/cases/unstar/'.$cid) ?>" title="Remover favorito">
                        <i class="fa fa-star star-on"></i>
                    </a>
                    <?php else: ?>
                    <a href="<?php echo site_url('admin/cases/star/'.$cid) ?>" title="Marcar favorito">
                        <i class="fa fa-star-o star-off"></i>
                    </a>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle">
                    <strong><?php echo htmlspecialchars($new->title ?? '') ?></strong>
                    <?php if(!empty($new->case_stage)): ?>
                    <small class="label label-default" style="margin-left:5px;font-size:10px;vertical-align:middle"><?php echo htmlspecialchars($new->case_stage) ?></small>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle">
                    <span class="case-num"><?php echo htmlspecialchars($new->case_no ?? '') ?></span>
                </td>
                <td style="vertical-align:middle">
                    <?php echo htmlspecialchars($new->client_name ?? $new->name ?? '—') ?>
                </td>
                <td style="vertical-align:middle;white-space:nowrap">
                    <?php if(check_user_role(7)==1): ?>
                    <a href="<?php echo site_url('admin/cases/view_case/'.$cid) ?>" class="btn btn-xs btn-default" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(6)==1): ?>
                    <a href="<?php echo site_url('admin/cases/edit/'.$cid) ?>" class="btn btn-xs btn-warning" title="Editar"><i class="fa fa-pencil"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(8)==1): ?>
                    <a href="<?php echo site_url('admin/cases/delete/'.$cid) ?>" class="btn btn-xs btn-danger" onclick="return confirm('<?php echo lang('are_you_sure') ?>')" title="Excluir"><i class="fa fa-trash"></i></a>
                    <?php endif; ?>
                    <a href="<?php echo site_url('admin/cases/archive/'.$cid) ?>" class="btn btn-xs btn-default" title="Arquivar"><i class="fa fa-archive"></i></a>
                </td>
            </tr>
            <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>

</section>

<script src="<?php echo base_url('assets/js/plugins/datatables/jquery.dataTables.js') ?>"></script>
<script src="<?php echo base_url('assets/js/plugins/datatables/dataTables.bootstrap.js') ?>"></script>
<script>
$(function() {
    $('#example1').dataTable({
        order: [[2,'asc']],
        pageLength: 25,
        language: { url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' }
    });
});
</script>
