<div class="content-header">
    <h4><i class="fas fa-clock mr-2"></i>Tracking Registrasi Pasien</h4>
    <p class="text-muted">Lama waktu registrasi dari loket hingga selesai</p>
</div>

<!-- Filter Card -->
<div class="card">
    <div class="card-header bg-primary text-white">
        <i class="fas fa-filter mr-1"></i> Filter Data
    </div>
    <div class="card-body">
        <form method="get" action="<?= site_url('tracking') ?>">
            <div class="form-row">
                <div class="col-md-3 mb-2">
                    <label>Tanggal Mulai</label>
                    <input type="date" name="start_date" class="form-control" value="<?= $filters['start_date'] ?>">
                </div>
                <div class="col-md-3 mb-2">
                    <label>Tanggal Selesai</label>
                    <input type="date" name="end_date" class="form-control" value="<?= $filters['end_date'] ?>">
                </div>
                <div class="col-md-3 mb-2">
                    <label>Petugas</label>
                    <select name="id_petugas" class="form-control select2">
                        <option value="">Semua Petugas</option>
                        <?php foreach ($list_petugas as $petugas): ?>
                            <option value="<?= $petugas->petugas_id ?>" 
                                <?= $filters['id_petugas'] == $petugas->petugas_id ? 'selected' : '' ?>>
                                <?= $petugas->nama_lengkap ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3 mb-2">
                    <label>Petugas PJ</label>
                    <select name="id_petugas_pj" class="form-control select2">
                        <option value="">Semua Petugas PJ</option>
                        <?php foreach ($list_petugas as $petugas): ?>
                            <option value="<?= $petugas->petugas_id ?>" 
                                <?= $filters['id_petugas_pj'] == $petugas->petugas_id ? 'selected' : '' ?>>
                                <?= $petugas->nama_lengkap ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-row mt-2">
                <div class="col">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search mr-1"></i> Filter
                    </button>
                    <a href="<?= site_url('tracking/export_excel?' . http_build_query($filters)) ?>" 
                       class="btn btn-success">
                        <i class="fas fa-file-excel mr-1"></i> Excel
                    </a>
                    <a href="<?= site_url('tracking/export_csv?' . http_build_query($filters)) ?>" 
                       class="btn btn-info">
                        <i class="fas fa-file-csv mr-1"></i> CSV
                    </a>
                    <a href="<?= site_url('tracking') ?>" class="btn btn-secondary">
                        <i class="fas fa-redo mr-1"></i> Reset
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Statistics Cards - Simple Version -->
<?php if (!empty($tracking_data)): ?>
<div class="row mb-3">
    <div class="col-md-3">
        <div class="small-box bg-info">
            <div class="inner">
                <h3><?= count($tracking_data) ?></h3>
                <p>Total</p>
            </div>
            <div class="icon">
                <i class="fas fa-users"></i>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="small-box bg-success">
            <div class="inner">
                <h3>
                    <?= count(array_filter($tracking_data, function($item) { 
                        return $item->status_registrasi == 'SELESAI'; 
                    })) ?>
                </h3>
                <p>Selesai</p>
            </div>
            <div class="icon">
                <i class="fas fa-check-circle"></i>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="small-box bg-warning">
            <div class="inner">
                <h3>
                    <?= count(array_filter($tracking_data, function($item) { 
                        return !$item->waktu_selesai_registrasi; 
                    })) ?>
                </h3>
                <p>Proses</p>
            </div>
            <div class="icon">
                <i class="fas fa-clock"></i>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="small-box bg-danger">
            <div class="inner">
                <h3>
                    <?= count(array_filter($tracking_data, function($item) { 
                        return $item->durasi_menit > 60; 
                    })) ?>
                </h3>
                <p>> 1 Jam</p>
            </div>
            <div class="icon">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Data Table -->
<div class="card">
    <div class="card-body">
        <?php if (empty($tracking_data)): ?>
            <div class="alert alert-info text-center">
                <i class="fas fa-info-circle mr-2"></i>
                Tidak ada data registrasi untuk filter yang dipilih.
            </div>
        <?php else: ?>
            <table class="table table-bordered table-striped" id="table-tracking">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Tanggal</th>
                        <th>No RM</th>
                        <th>Nama Pasien</th>
                        <th>Poli</th>
                        <th>Dokter</th>
                        <th>Petugas</th>
                        <th>Mulai</th>
                        <th>Selesai</th>
                        <th>Durasi</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $no = 1; ?>
                    <?php foreach ($tracking_data as $item): ?>
                        <tr>
                            <td><?= $no++ ?></td>
                            <td>
                                <span class="badge badge-secondary">
                                    <?= date('d/m/Y', strtotime($item->tanggal_registrasi)) ?>
                                </span>
                            </td>
                            <td>
                                <span class="font-weight-bold text-primary"><?= $item->rme_no ?></span>
                            </td>
                            <td>
                                <div>
                                    <div class="font-weight-bold"><?= $item->nama_pasien ?></div>
                                    <?php if ($item->nama_petugas_pj): ?>
                                        <small class="text-muted">
                                            <i class="fas fa-user-shield mr-1"></i><?= $item->nama_petugas_pj ?>
                                        </small>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td>
                                <span class="badge badge-info"><?= $item->nama_poli ?></span>
                            </td>
                            <td>
                                <?php if ($item->nama_dokter): ?>
                                    <span class="text-success"><?= $item->nama_dokter ?></span>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($item->nama_petugas): ?>
                                    <?= $item->nama_petugas ?>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <span class="font-weight-bold text-dark">
                                    <?= date('H:i', strtotime($item->waktu_mulai_registrasi)) ?>
                                </span>
                            </td>
                            <td class="text-center">
                                <?php if ($item->waktu_selesai_registrasi): ?>
                                    <span class="font-weight-bold text-success">
                                        <?= date('H:i', strtotime($item->waktu_selesai_registrasi)) ?>
                                    </span>
                                <?php else: ?>
                                    <span class="badge badge-warning">
                                        <i class="fas fa-spinner mr-1"></i>Proses
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <?php if ($item->durasi_menit): ?>
                                    <?php
                                    $badge_class = 'badge-success';
                                    if ($item->durasi_menit > 60) $badge_class = 'badge-danger';
                                    elseif ($item->durasi_menit > 30) $badge_class = 'badge-warning';
                                    ?>
                                    <span class="badge <?= $badge_class ?>">
                                        <?= $item->durasi_menit ?>m
                                    </span>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <?php
                                $status_class = 'badge-primary';
                                $status_icon = 'fas fa-clock';
                                
                                if ($item->status_registrasi == 'SELESAI') {
                                    $status_class = 'badge-success';
                                    $status_icon = 'fas fa-check-circle';
                                } elseif ($item->status_registrasi == 'BATAL') {
                                    $status_class = 'badge-danger';
                                    $status_icon = 'fas fa-times-circle';
                                }
                                ?>
                                <span class="badge <?= $status_class ?>">
                                    <i class="<?= $status_icon ?> mr-1"></i><?= $item->status_registrasi ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <!-- Simple Pagination Info -->
            <div class="mt-3 text-center">
                <small class="text-muted">
                    Menampilkan <?= count($tracking_data) ?> data registrasi | 
                    Terakhir update: <?= date('d/m/Y H:i') ?>
                </small>
            </div>
        <?php endif; ?>
    </div>
</div>

<script src="<?= base_url('assets/adminlte/plugins/jquery/jquery.min.js'); ?>"></script>
<script src="<?= base_url('assets/adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js'); ?>"></script>
<script src="<?= base_url('assets/adminlte/plugins/datatables/jquery.dataTables.min.js'); ?>"></script>
<script src="<?= base_url('assets/adminlte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js'); ?>"></script>
<script src="<?= base_url('assets/adminlte/plugins/select2/js/select2.full.min.js'); ?>"></script>

<script>
$(document).ready(function() {
    // Initialize Select2
    $('.select2').select2({
        theme: 'bootstrap4',
        placeholder: '-- pilih --',
        allowClear: true,
        width: '100%'
    });

    // Initialize DataTable - Simple Configuration
    $('#table-tracking').DataTable({
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": false,
        "responsive": true,
        "pageLength": 25,
        "language": {
            "search": "Cari:",
            "lengthMenu": "Tampil _MENU_ data",
            "zeroRecords": "Tidak ada data",
            "info": "Hal _PAGE_ dari _PAGES_",
            "infoEmpty": "Tidak ada data",
            "infoFiltered": "(filter dari _MAX_ data)",
            "paginate": {
                "first": "Awal",
                "last": "Akhir",
                "next": "Next",
                "previous": "Prev"
            }
        }
    });
});
</script>
