<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php $CI = &get_instance(); ?>
<link href="<?php echo base_url('assets/css/datatables/dataTables.bootstrap.css') ?>" rel="stylesheet">
<style>
.client-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-users"></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('clients') ?></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-users"></i> <?php echo lang('clients') ?></h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(33)==1): ?>
            <a href="<?php echo site_url('admin/clients/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Novo Cliente
            </a>
            <?php endif; ?>
            <a href="<?php echo site_url('admin/clients/export') ?>" class="btn btn-default btn-sm">
                <i class="fa fa-download"></i> Exportar
            </a>
        </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>Nome</th>
                    <th>Telefone</th>
                    <th>Email</th>
                    <th style="width:120px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($clients)): $i=1; foreach($clients as $c): ?>
            <?php
            $parts = explode(' ', trim($c->name));
            $ini = strtoupper(substr($parts[0],0,1).(isset($parts[1])?substr($parts[1],0,1):''));
            ?>
            <tr>
                <td><?php echo $i++ ?></td>
                <td>
                    <div style="display:flex;align-items:center;gap:10px">
                        <?php if(!empty($c->image)): ?>
                        <img src="<?php echo base_url('assets/uploads/images/'.$c->image) ?>" style="width:34px;height:34px;border-radius:50%;object-fit:cover">
                        <?php else: ?>
                        <div class="client-avatar"><?php echo $ini ?></div>
                        <?php endif; ?>
                        <div>
                            <strong><?php echo htmlspecialchars($c->name) ?></strong>
                            <?php if(!empty($c->gender)): ?>
                            <small class="text-muted" style="display:block;font-size:11px"><?php echo $c->gender ?></small>
                            <?php endif; ?>
                        </div>
                    </div>
                </td>
                <td><?php echo htmlspecialchars($c->contact ?? '—') ?></td>
                <td><small><?php echo htmlspecialchars($c->email ?? '—') ?></small></td>
                <td>
                    <?php if(check_user_role(36)==1): ?>
                    <a href="<?php echo site_url('admin/clients/view_client/'.$c->id) ?>" class="btn btn-xs btn-default" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(34)==1): ?>
                    <a href="<?php echo site_url('admin/clients/edit/'.$c->id) ?>" class="btn btn-xs btn-warning" title="Editar"><i class="fa fa-pencil"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(35)==1): ?>
                    <a href="<?php echo site_url('admin/clients/delete/'.$c->id) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Excluir este cliente?')" 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: [[1,'asc']],
        pageLength: 25,
        language: { url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' }
    });
});
</script>
