<section class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1><?php echo $page_title; ?></h1>
        <p class="text-muted mb-0"><?php echo $page_subtitle; ?></p>
      </div>
      <div class="col-sm-6 text-right">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?php echo site_url('dashboard'); ?>">Dashboard</a></li>
          <li class="breadcrumb-item active">Manajemen Shift</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="row">
      <!-- Daftar Shift -->
      <div class="col-md-8">
        <div class="card card-danger card-outline">
          <div class="card-header d-flex align-items-center">
            <h3 class="card-title mr-auto"><i class="fa fa-clock mr-1"></i> Daftar Shift</h3>
            <a href="<?php echo site_url('master/shift/assignment'); ?>" class="btn btn-outline-secondary btn-sm mr-2">
              <i class="fa fa-calendar mr-1"></i> Jadwal Kerja
            </a>
            <a href="<?php echo site_url('master/shift/tambah'); ?>" class="btn btn-danger btn-sm">
              <i class="fa fa-plus mr-1"></i> Tambah Shift
            </a>
          </div>
          <div class="card-body">
            <?php if (empty($list)): ?>
              <div class="text-center text-muted py-4">
                <i class="fa fa-clock fa-3x mb-2"></i>
                <p>Belum ada data shift. <a href="<?php echo site_url('master/shift/tambah'); ?>">Tambah sekarang</a>.</p>
              </div>
            <?php else: ?>
            <div class="table-responsive">
              <table class="table table-bordered table-hover">
                <thead>
                  <tr>
                    <th style="width:50px;">#</th>
                    <th>Nama Shift</th>
                    <th>Jam Masuk</th>
                    <th>Jam Keluar</th>
                    <th style="width:80px;">Toleransi</th>
                    <th style="width:80px;">Tipe</th>
                    <th style="width:80px;">Status</th>
                    <th style="width:130px;">Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  <?php $no = 1; foreach ($list as $s): ?>
                  <tr class="<?php echo $s->is_default ? 'table-warning' : ''; ?>">
                    <td><?php echo $no++; ?></td>
                    <td>
                      <?php echo html_escape($s->nama_shift); ?>
                      <?php if ($s->is_default): ?>
                        <span class="badge badge-warning ml-1">Default</span>
                      <?php endif; ?>
                    </td>
                    <td><strong><?php echo substr($s->jam_masuk, 0, 5); ?></strong></td>
                    <td>
                      <?php echo substr($s->jam_keluar, 0, 5); ?>
                      <?php if ($s->lintas_tengah_malam): ?>
                        <span class="badge badge-info ml-1" title="Jam keluar di hari berikutnya">+1</span>
                      <?php endif; ?>
                    </td>
                    <td class="text-center"><?php echo $s->toleransi_menit; ?> mnt</td>
                    <td class="text-center">
                      <?php if ($s->lintas_tengah_malam): ?>
                        <span class="badge badge-dark">Malam</span>
                      <?php else: ?>
                        <span class="badge badge-secondary">Normal</span>
                      <?php endif; ?>
                    </td>
                    <td class="text-center">
                      <span class="badge <?php echo $s->is_active ? 'badge-success' : 'badge-secondary'; ?>">
                        <?php echo $s->is_active ? 'Aktif' : 'Nonaktif'; ?>
                      </span>
                    </td>
                    <td>
                      <a href="<?php echo site_url('master/shift/edit/' . $s->id_shift); ?>"
                         class="btn btn-primary btn-xs mr-1" title="Edit"><i class="fa fa-edit"></i></a>
                      <?php if (!$s->is_default): ?>
                      <a href="<?php echo site_url('master/shift/set_default/' . $s->id_shift); ?>"
                         class="btn btn-warning btn-xs" title="Jadikan Default"
                         onclick="return confirm('Jadikan shift ini sebagai default?')">
                         <i class="fa fa-star"></i>
                      </a>
                      <?php endif; ?>
                    </td>
                  </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
            </div>
            <?php endif; ?>
          </div>
        </div>
      </div>

      <!-- Info Panel -->
      <div class="col-md-4">
        <div class="card card-info card-outline">
          <div class="card-header">
            <h3 class="card-title"><i class="fa fa-info-circle mr-1"></i> Keterangan</h3>
          </div>
          <div class="card-body">
            <p class="text-sm"><strong>Shift Default</strong> digunakan sebagai fallback jadwal kerja hari Senin-Jumat jika pegawai atau divisi belum memiliki jadwal mingguan.</p>
            <p class="text-sm"><strong>Toleransi</strong> adalah jumlah menit keterlambatan yang masih dimaafkan sebelum pegawai dinyatakan terlambat.</p>
            <p class="text-sm"><strong>Badge +1</strong> menandakan shift yang melewati tengah malam (jam keluar di hari berikutnya).</p>
            <hr>
            <a href="<?php echo site_url('master/shift/assignment'); ?>" class="btn btn-block btn-outline-danger">
              <i class="fa fa-calendar mr-1"></i> Kelola Jadwal Kerja
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
