<?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>
.pr-badge { display:inline-block; padding:2px 9px; border-radius:10px; font-size:10.5px; font-weight:600; }
.pr-baixa  { background:#e3f2fd; color:#1565c0; }
.pr-media  { background:#e8f5e9; color:#2e7d32; }
.pr-alta   { background:#ffebee; color:#c62828; }
.progress-mini { height:6px; border-radius:3px; background:#e9ecef; overflow:hidden; margin-top:4px; }
.progress-mini-bar { height:100%; border-radius:3px; background:#1e3a5f; }
.task-vencida td { background:#fff8f8 !important; }
</style>

<section class="content-header">
    <h1><i class="fa fa-tasks"></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('tasks') ?></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-tasks"></i> Tarefas</h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(113)==1): ?>
            <a href="<?php echo site_url('admin/tasks/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Nova Tarefa
            </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:40px">#</th>
                    <th>Tarefa</th>
                    <th style="width:80px">Prioridade</th>
                    <th>Prazo</th>
                    <th>Criado por</th>
                    <th>Progresso</th>
                    <th style="width:150px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($tasks)): $i=1; foreach($tasks as $t):
                $vencida = $t->due_date < date('Y-m-d') && $t->progress != 100;
            ?>
            <tr class="<?php echo $vencida ? 'task-vencida' : '' ?>">
                <td><?php echo $i++ ?></td>
                <td>
                    <strong><?php echo htmlspecialchars($t->taskname) ?></strong>
                    <?php if($vencida): ?>
                    <span class="label label-danger" style="margin-left:5px;font-size:10px">Vencida</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if($t->priority==1): ?>
                    <span class="pr-badge pr-baixa">Baixa</span>
                    <?php elseif($t->priority==2): ?>
                    <span class="pr-badge pr-media">Média</span>
                    <?php elseif($t->priority==3): ?>
                    <span class="pr-badge pr-alta">Alta</span>
                    <?php endif; ?>
                </td>
                <td style="white-space:nowrap">
                    <span style="color:<?php echo $vencida?'#c62828':'#333' ?>">
                        <i class="fa fa-calendar-o"></i> <?php echo $t->due_date ?>
                    </span>
                </td>
                <td>
                    <small><?php echo htmlspecialchars($t->name ?? '—') ?></small>
                    <?php if(!empty($t->role)): ?>
                    <small class="label label-default" style="margin-left:3px;font-size:9px"><?php echo htmlspecialchars($t->role) ?></small>
                    <?php endif; ?>
                </td>
                <td style="min-width:80px">
                    <small style="font-size:11px"><?php echo $t->progress ?? 0 ?>%</small>
                    <div class="progress-mini">
                        <div class="progress-mini-bar" style="width:<?php echo $t->progress ?? 0 ?>%;background:<?php echo ($t->progress??0)==100?'#2e7d32':'#1e3a5f' ?>"></div>
                    </div>
                </td>
                <td>
                    <?php if(check_user_role(115)==1): ?>
                    <a href="<?php echo site_url('admin/tasks/view/'.$t->id) ?>" class="btn btn-xs btn-default" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(114)==1): ?>
                    <a href="<?php echo site_url('admin/tasks/edit/'.$t->id) ?>" class="btn btn-xs btn-warning" title="Editar"><i class="fa fa-pencil"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(117)==1): ?>
                    <a href="<?php echo site_url('admin/tasks/comments/'.$t->id) ?>" class="btn btn-xs btn-info" title="Comentários"><i class="fa fa-comments"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(116)==1): ?>
                    <a href="<?php echo site_url('admin/tasks/delete/'.$t->id) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Excluir esta tarefa?')" title="Excluir"><i class="fa fa-trash"></i></a>
                    <?php endif; ?>
                </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: [[3,'asc']],
        pageLength: 25,
        language: { url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' }
    });
});
</script>
