<h2 class="page-title">Mis Trámites</h2>
<p class="text-muted">Aquí puedes ver todos los trámites que has iniciado.</p>

<div class="d-flex justify-content-end mb-3">
    <a href="<?= BASE_HREF ?>dashboard" class="btn btn-secondary">Volver</a>
</div>

<div class="row my-4">
    <div class="col-md-12">
        <div class="card shadow">
            <div class="card-body">
                <table class="table datatables" id="dataTable-1">
                    <thead class="thead-dark">
                        <tr>
                            <th>N°</th>
                            <th>Expediente</th>
                            <th>Procedimiento</th>
                            <th>Asunto</th>
                            <th>Folios</th>
                            <th>Estado</th>
                            <th>Fecha</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($processes as $index => $process): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= htmlspecialchars($process->num_exped) ?></td>
                            <td><?= htmlspecialchars($process->procedure()->description ?? $process->custom_procedure ?? 'Sin procedimiento') ?></td>
                            <td><?= htmlspecialchars($process->subject) ?></td>
                            <td><?= htmlspecialchars($process->folios) ?></td>
                            <td>
                                <?php
                                $badgeClass = 'warning';

                                switch ($process->general_state) {
                                    case 'RESUELTO':
                                        $badgeClass = 'success';
                                        break;
                                    case 'OBSERVADO':
                                        $badgeClass = 'danger'; 
                                        break;
                                }
                                ?>
                                <span class="badge badge-<?= $badgeClass ?>">
                                    <?= htmlspecialchars($process->general_state) ?>
                                </span>
                            </td>

                            <td><?= date('d/m/Y', strtotime($process->entry_date)) ?></td>
                            <td>
                                <div class="dropdown">
                                    <button class="btn btn-sm dropdown-toggle more-horizontal" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <span class="text-muted sr-only">Acciones</span>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-right">
                                        <a class="dropdown-item" href="<?= BASE_HREF ?>processes/<?= $process->id ?>/show-process">Ver Detalle</a>
                                        <?php if ($process->general_state === 'OBSERVADO'): ?>
                                            <a class="dropdown-item" href="<?= BASE_HREF ?>processes/<?= $process->id ?>/amend">Subsanar</a>
                                        <?php endif; ?>
                                    </div>                                    
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
  $(document).ready(function() {    
    $('#dataTable-1').DataTable({
      autoWidth: true,
      "lengthMenu": [
        [10, 15, 20, -1],
        [10, 15, 20, "All"]
      ]
    });
  });
</script>

