<?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>
.contact-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>

<!-- Modal Import -->
<div class="modal fade" id="myModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content" style="border-radius:10px;overflow:hidden">
            <div class="modal-header" style="background:linear-gradient(135deg,#1e3a5f,#2e75b6)">
                <button type="button" class="close" data-dismiss="modal" style="color:#fff">&times;</button>
                <h4 class="modal-title" style="color:#fff"><i class="fa fa-upload"></i> <?php echo lang('import') ?></h4>
            </div>
            <form method="post" enctype="multipart/form-data" action="<?php echo site_url('admin/contacts/import') ?>">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Arquivo CSV</label>
                        <input type="file" name="file" class="form-control" accept=".csv">
                    </div>
                    <small class="text-muted"><i class="fa fa-info-circle"></i> O arquivo deve estar no formato CSV com colunas: nome, telefone, email, endereço.</small>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-primary"><i class="fa fa-upload"></i> Importar</button>
                </div>
            </form>
        </div>
    </div>
</div>

<section class="content-header">
    <h1><i class="fa fa-address-book-o"></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('contacts') ?></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-address-book-o"></i> <?php echo lang('contacts') ?></h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(22)==1): ?>
            <a href="<?php echo site_url('admin/contacts/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> <?php echo lang('add_new') ?>
            </a>
            <a href="#myModal" data-toggle="modal" class="btn btn-default btn-sm">
                <i class="fa fa-upload"></i> <?php echo lang('import') ?>
            </a>
            <?php endif; ?>
            <a href="<?php echo site_url('admin/contacts/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>Telefone</th>
                    <th>E-mail</th>
                    <th style="width:120px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($contacts)): $i=1; foreach($contacts 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">
                        <div class="contact-avatar"><?php echo $ini ?></div>
                        <strong><?php echo htmlspecialchars($new->name) ?></strong>
                    </div>
                </td>
                <td style="vertical-align:middle">
                    <?php if(!empty($new->contact)): ?>
                    <a href="tel:<?php echo $new->contact ?>"><i class="fa fa-phone text-muted" style="margin-right:5px"></i><?php echo htmlspecialchars($new->contact) ?></a>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td style="vertical-align:middle">
                    <?php if(!empty($new->email)): ?>
                    <a href="mailto:<?php echo $new->email ?>"><i class="fa fa-envelope-o text-muted" style="margin-right:5px"></i><?php echo htmlspecialchars($new->email) ?></a>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td style="vertical-align:middle;white-space:nowrap">
                    <?php if(check_user_role(155)==1): ?>
                    <a href="<?php echo site_url('admin/contacts/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(23)==1): ?>
                    <a href="<?php echo site_url('admin/contacts/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(24)==1): ?>
                    <a href="<?php echo site_url('admin/contacts/delete/'.$new->id) ?>" 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; ?>
                </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>
