<?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>
.appt-time { font-size:12px; font-weight:600; color:#1e3a5f; white-space:nowrap; }
.appt-hoje { background:#e8f5e9 !important; }
.appt-passado { opacity:.7; }
</style>

<section class="content-header">
    <h1><i class="fa fa-calendar"></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('appointments') ?></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-calendar"></i> Agendamentos</h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(26)==1): ?>
            <a href="<?php echo site_url('admin/appointments/add') ?>" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Novo Agendamento
            </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>Título</th>
                    <th>Contato</th>
                    <th>Motivo</th>
                    <th>Data / Hora</th>
                    <th>Observações</th>
                    <th style="width:120px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php if(isset($appointments)): $i=1; foreach($appointments as $a):
                $dt = strtotime($a->date_time);
                $hoje = date('Y-m-d') === date('Y-m-d', $dt);
                $passado = $dt < time() && !$hoje;
            ?>
            <tr class="<?php echo $hoje ? 'appt-hoje' : ($passado ? 'appt-passado' : '') ?>">
                <td><?php echo $i++ ?></td>
                <td>
                    <strong><?php echo htmlspecialchars($a->title) ?></strong>
                    <?php if($hoje): ?>
                    <span class="label label-success" style="margin-left:5px;font-size:10px">Hoje</span>
                    <?php endif; ?>
                </td>
                <td><?php echo htmlspecialchars($a->name ?? '—') ?></td>
                <td><small><?php echo htmlspecialchars($a->motive ?? '—') ?></small></td>
                <td><span class="appt-time"><i class="fa fa-clock-o"></i> <?php echo date_time_convert($a->date_time) ?></span></td>
                <td><small class="text-muted"><?php echo htmlspecialchars(substr($a->notes ?? '', 0, 60)) ?></small></td>
                <td>
                    <?php if(check_user_role(29)==1): ?>
                    <a href="<?php echo site_url('admin/appointments/view_appointment/'.$a->id) ?>" class="btn btn-xs btn-default" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(27)==1): ?>
                    <a href="<?php echo site_url('admin/appointments/edit/'.$a->id) ?>" class="btn btn-xs btn-warning" title="Editar"><i class="fa fa-pencil"></i></a>
                    <?php endif; ?>
                    <?php if(check_user_role(28)==1): ?>
                    <a href="<?php echo site_url('admin/appointments/delete/'.$a->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: [[4,'asc']],
        pageLength: 25,
        language: { url: '//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' }
    });
});
</script>
