<?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>
.emp-avatar {
    width:34px; height:34px; border-radius:50%;
    background:linear-gradient(135deg,#4a9fd4,#1e3a5f);
    display:inline-flex; align-items:center; justify-content:center;
    font-size:12px; font-weight:700; color:#fff; flex-shrink:0;
}
</style>

<section class="content-header">
    <h1><i class="fa fa-id-badge"></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('employees') ?></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-id-badge"></i> <?php echo lang('employees') ?></h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(38)==1): ?>
            <a href="<?php echo site_url('admin/employees/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> <?php echo lang('add') ?> <?php echo lang('new') ?>
            </a>
            <?php endif; ?>
            <a href="<?php echo site_url('admin/employees/export') ?>" class="btn btn-default btn-sm">
                <i class="fa fa-download"></i> <?php echo lang('export') ?>
            </a>
        </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>Nome</th>
                    <th>Cargo / Função</th>
                    <th style="width:90px">Status</th>
                    <th style="width:180px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($employees)): $i=1; foreach($employees as $new):
                $parts = explode(' ', trim($new->name));
                $ini = strtoupper(substr($parts[0],0,1).(isset($parts[1])?substr($parts[1],0,1):''));
            ?>
            <tr>
                <td><?php echo $i++ ?></td>
                <td style="vertical-align:middle">
                    <div style="display:flex;align-items:center;gap:10px">
                        <?php if(!empty($new->image)): ?>
                        <img src="<?php echo base_url('assets/uploads/images/'.$new->image) ?>" style="width:34px;height:34px;border-radius:50%;object-fit:cover">
                        <?php else: ?>
                        <div class="emp-avatar"><?php echo $ini ?></div>
                        <?php endif; ?>
                        <strong><?php echo ucwords($new->name) ?></strong>
                    </div>
                </td>
                <td style="vertical-align:middle">
                    <span class="label label-default" style="font-size:11px"><?php echo htmlspecialchars($new->role ?? '—') ?></span>
                </td>
                <td style="vertical-align:middle">
                    <?php if($new->status == 1): ?>
                    <span class="label label-success"><?php echo lang('active') ?></span>
                    <?php else: ?>
                    <span class="label label-default"><?php echo lang('inactive') ?></span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle;white-space:nowrap">
                    <?php if(check_user_role(41)==1): ?>
                    <a href="<?php echo site_url('admin/employees/view/'.$new->id) ?>" class="btn btn-xs btn-default" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(39)==1): ?>
                    <a href="<?php echo site_url('admin/employees/edit/'.$new->id) ?>" class="btn btn-xs btn-warning" title="Editar"><i class="fa fa-pencil"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(40)==1): ?>
                    <a href="<?php echo site_url('admin/employees/delete/'.$new->id) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Excluir este colaborador?')" title="Excluir"><i class="fa fa-trash"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(123)==1): ?>
                    <a href="<?php echo site_url('admin/employees/bank_details/'.$new->id) ?>" class="btn btn-xs btn-info" title="Dados bancários"><i class="fa fa-bank"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(126)==1): ?>
                    <a href="<?php echo site_url('admin/employees/documents/'.$new->id) ?>" class="btn btn-xs btn-success" title="Documentos"><i class="fa fa-file"></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: [[1,'asc']],
        pageLength: 25,
        language: { url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' }
    });
});
</script>
