<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"><a href="<?php echo site_url('transaksi/absensi'); ?>">Manajemen Absensi</a></li>
          <li class="breadcrumb-item active"><?php echo $row ? 'Edit' : 'Tambah'; ?></li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="card card-danger card-outline" style="max-width:640px;">
      <div class="card-header"><h3 class="card-title"><i class="fa fa-calendar mr-1"></i> Form Kehadiran</h3></div>
      <div class="card-body">
        <?php
          $form_action = $row ? site_url('transaksi/absensi/simpan') : site_url('transaksi/absensi/simpan');
          echo form_open($form_action, ['id'=>'form-absensi']);
          if ($row): echo form_hidden('id_absensi', $row->id_absensi); endif;
        ?>
        <div class="form-group">
          <label>Pegawai <span class="text-danger">*</span></label>
          <select name="id_pegawai" id="id_pegawai" class="form-control <?php echo form_error('id_pegawai') ? 'is-invalid' : ''; ?>">
            <option value="">-- Pilih Pegawai --</option>
            <?php foreach ($pegawai_list as $p): ?>
              <option value="<?php echo $p->id_pegawai; ?>" <?php echo set_select('id_pegawai', $p->id_pegawai, ($row->id_pegawai ?? '') == $p->id_pegawai); ?>>
                <?php echo html_escape($p->nip . ' - ' . $p->nama_lengkap); ?>
              </option>
            <?php endforeach; ?>
          </select>
          <?php if(form_error('id_pegawai')): ?><div class="invalid-feedback"><?php echo form_error('id_pegawai'); ?></div><?php endif; ?>
        </div>

        <div class="row">
          <div class="col-sm-6">
            <div class="form-group">
              <label>Tanggal <span class="text-danger">*</span></label>
              <input type="date" name="tanggal"
                     class="form-control <?php echo form_error('tanggal') ? 'is-invalid' : ''; ?>"
                     value="<?php echo set_value('tanggal', $row->tanggal ?? date('Y-m-d')); ?>">
              <?php if(form_error('tanggal')): ?><div class="invalid-feedback"><?php echo form_error('tanggal'); ?></div><?php endif; ?>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <label>Status <span class="text-danger">*</span></label>
              <select name="status" class="form-control <?php echo form_error('status') ? 'is-invalid' : ''; ?>">
                <option value="">-- Pilih Status --</option>
                <option value="MASUK" <?php echo set_select('status', 'MASUK', ($row->status ?? '') === 'MASUK'); ?>>Masuk</option>
                <option value="ALPHA" <?php echo set_select('status', 'ALPHA', ($row->status ?? '') === 'ALPHA'); ?>>Alpha</option>
                <option value="SAKIT" <?php echo set_select('status', 'SAKIT', ($row->status ?? '') === 'SAKIT'); ?>>Sakit</option>
                <option value="CUTI" <?php echo set_select('status', 'CUTI', ($row->status ?? '') === 'CUTI'); ?>>Cuti</option>
                <option value="LIBUR" <?php echo set_select('status', 'LIBUR', ($row->status ?? '') === 'LIBUR'); ?>>Libur</option>
                <option value="LEMBUR" <?php echo set_select('status', 'LEMBUR', ($row->status ?? '') === 'LEMBUR'); ?>>Lembur</option>
              </select>
              <?php if(form_error('status')): ?><div class="invalid-feedback"><?php echo form_error('status'); ?></div><?php endif; ?>
            </div>
          </div>
        </div>

        <div class="row" id="row-jam" style="display:none;">
          <div class="col-sm-6">
            <div class="form-group">
              <label>Jam Masuk</label>
              <input type="time" name="jam_masuk"
                     class="form-control"
                     value="<?php echo set_value('jam_masuk', $row->jam_masuk ?? ''); ?>">
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <label>Jam Keluar</label>
              <input type="time" name="jam_keluar"
                     class="form-control"
                     value="<?php echo set_value('jam_keluar', $row->jam_keluar ?? ''); ?>">
            </div>
          </div>
        </div>

        <div class="form-group">
          <label>Keterangan</label>
          <textarea name="keterangan" class="form-control" rows="2" placeholder="Catatan tambahan"><?php echo set_value('keterangan', $row->keterangan ?? ''); ?></textarea>
        </div>

        <?php if ($row): ?>
        <div class="form-group">
          <label>Alasan Koreksi <span class="text-danger">*</span></label>
          <textarea name="alasan_koreksi" class="form-control" rows="2" 
                    placeholder="Wajib diisi - jelaskan alasan perubahan data ini"
                    required><?php echo set_value('alasan_koreksi', ''); ?></textarea>
          <small class="form-text text-muted">Alasan koreksi dicatat dalam audit log untuk keperluan penelusuran.</small>
        </div>
        <?php endif; ?>

        <div class="d-flex justify-content-between">
          <?php if ($row): ?>
          <a href="<?php echo site_url('transaksi/absensi/detail/' . $row->id_pegawai); ?>" class="btn btn-secondary"><i class="fa fa-arrow-left mr-1"></i> Kembali</a>
          <?php else: ?>
          <a href="<?php echo site_url('transaksi/absensi'); ?>" class="btn btn-secondary"><i class="fa fa-arrow-left mr-1"></i> Kembali</a>
          <?php endif; ?>
          <button type="submit" class="btn btn-danger"><i class="fa fa-save mr-1"></i> Simpan</button>
        </div>
        <?php echo form_close(); ?>
      </div>
    </div>
  </div>
</section>

<script>
$(document).ready(function() {
  var status_input = $('select[name="status"]');
  
  function toggleJamFields() {
    if (status_input.val() === 'MASUK' || status_input.val() === 'LEMBUR') {
      $('#row-jam').show();
    } else {
      $('#row-jam').hide();
    }
  }
  
  status_input.change(toggleJamFields);
  toggleJamFields();
});
</script>
