<?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; }
.task-vencida td { background:#fff8f8 !important; }
.assigned-pill {
    display:inline-flex; align-items:center; gap:5px;
    background:#f0f4ff; color:#1e3a5f;
    padding:2px 8px; border-radius:10px; font-size:11px; font-weight:600;
}
.assigned-avatar {
    width:18px; height:18px; border-radius:50%;
    background:linear-gradient(135deg,#4a9fd4,#1e3a5f);
    display:inline-flex; align-items:center; justify-content:center;
    font-size:8px; font-weight:700; color:#fff; flex-shrink:0;
}
</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:36px">#</th>
                    <th>Tarefa</th>
                    <th style="width:80px">Prioridade</th>
                    <th>Prazo</th>
                    <th>Criado por</th>
                    <th>Atribuído a</th>
                    <th>Progresso</th>
                    <th style="width:140px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($tasks)): $i=1; foreach($tasks as $t):
                $vencida = isset($t->due_date) && $t->due_date < date('Y-m-d') && ($t->progress ?? 0) != 100;

                // Atribuído a — tenta campo assigned_name ou assigned_to_name
                $assigned = isset($t->assigned_name) ? $t->assigned_name
                          : (isset($t->assigned_to_name) ? $t->assigned_to_name
                          : (isset($t->assigned_to) ? $t->assigned_to : ''));
                $a_parts = explode(' ', trim($assigned));
                $a_ini = strtoupper(substr($a_parts[0],0,1).(isset($a_parts[1])?substr($a_parts[1],0,1):''));

                // Criado por
                $creator = isset($t->name) ? $t->name : '';
            ?>
            <tr class="<?php echo $vencida ? 'task-vencida' : '' ?>">
                <td><?php echo $i++ ?></td>
                <td style="vertical-align:middle">
                    <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; ?>
                    <?php if(($t->progress ?? 0) == 100): ?>
                    <span class="label label-success" style="margin-left:5px;font-size:10px">Concluída</span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle">
                    <?php if(($t->priority ?? 0) == 1): ?>
                    <span class="pr-badge pr-baixa">Baixa</span>
                    <?php elseif(($t->priority ?? 0) == 2): ?>
                    <span class="pr-badge pr-media">Média</span>
                    <?php elseif(($t->priority ?? 0) == 3): ?>
                    <span class="pr-badge pr-alta">Alta</span>
                    <?php else: ?>
                    <span class="pr-badge" style="background:#f5f5f5;color:#888">—</span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle;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 style="vertical-align:middle">
                    <small><?php echo htmlspecialchars($creator) ?></small>
                    <?php if(!empty($t->role)): ?>
                    <br><span class="label label-default" style="font-size:9px"><?php echo htmlspecialchars($t->role) ?></span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle">
                    <?php if(!empty($assigned)): ?>
                    <span class="assigned-pill">
                        <span class="assigned-avatar"><?php echo $a_ini ?></span>
                        <?php echo htmlspecialchars($assigned) ?>
                    </span>
                    <?php else: ?>
                    <span class="text-muted" style="font-size:12px">—</span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle;min-width:80px">
                    <small style="font-size:11px;color:#888"><?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':(($t->progress??0)>=50?'#1e3a5f':'#e67e22') ?>"></div>
                    </div>
                </td>
                <td style="vertical-align:middle;white-space:nowrap">
                    <?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>
