<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('master/pegawai'); ?>">Data Pegawai</a></li>
          <li class="breadcrumb-item active">Detail</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="row">
      <!-- Sidebar info -->
      <div class="col-md-3">
        <div class="card card-danger card-outline text-center">
          <div class="card-body pt-4 pb-3">
            <div class="mb-3">
              <i class="fa fa-user-circle" style="font-size:72px; color:#c77;"></i>
            </div>
            <h5 class="mb-1 font-weight-bold"><?php echo html_escape($row->nama_lengkap); ?></h5>
            <p class="text-muted mb-2" style="font-size:13px;"><?php echo html_escape($row->nama_jabatan ?? '-'); ?></p>
            <span class="badge <?php echo $row->status_pegawai === 'AKTIF' ? 'badge-aktif' : 'badge-nonaktif'; ?> mb-3">
              <?php echo $row->status_pegawai; ?>
            </span>
            <div class="border-top pt-3">
              <a href="<?php echo site_url('master/pegawai/edit/' . $row->id_pegawai); ?>" class="btn btn-primary btn-sm btn-block mb-2">
                <i class="fa fa-pencil mr-1"></i> Edit Data
              </a>
              <a href="<?php echo site_url('master/mapping_komponen/pegawai/' . $row->id_pegawai); ?>" class="btn btn-info btn-sm btn-block mb-2">
                <i class="fa fa-sliders mr-1"></i> Mapping Komponen
              </a>
              <a href="<?php echo site_url('master/pegawai/toggle_status/' . $row->id_pegawai); ?>"
                 class="btn <?php echo $row->status_pegawai === 'AKTIF' ? 'btn-warning' : 'btn-success'; ?> btn-sm btn-block btn-confirm"
                 data-msg="<?php echo $row->status_pegawai === 'AKTIF' ? 'Nonaktifkan' : 'Aktifkan'; ?> pegawai ini?">
                <i class="fa fa-power-off mr-1"></i>
                <?php echo $row->status_pegawai === 'AKTIF' ? 'Nonaktifkan' : 'Aktifkan'; ?>
              </a>
            </div>
          </div>
        </div>
      </div>

      <!-- Detail tabs -->
      <div class="col-md-9">
        <div class="card card-danger card-outline">
          <div class="card-header p-0">
            <ul class="nav nav-tabs" id="tabs-detail" role="tablist">
              <li class="nav-item">
                <a class="nav-link active" data-toggle="tab" href="#tab-identitas">Identitas</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#tab-penggajian">Penggajian</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#tab-penunjang">BPJS & Rekening</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#tab-riwayat">Riwayat Perubahan</a>
              </li>
            </ul>
          </div>
          <div class="card-body">
            <div class="tab-content">
              <!-- Tab Identitas -->
              <div class="tab-pane active" id="tab-identitas">
                <table class="table table-sm table-borderless">
                  <?php
                  $fields = [
                    'NIP'           => $row->nip,
                    'NIK'           => $row->nik ?? '-',
                    'Jenis Kelamin' => $row->jenis_kelamin === 'L' ? 'Laki-laki' : 'Perempuan',
                    'Tempat Lahir'  => $row->tempat_lahir ?? '-',
                    'Tanggal Lahir' => $row->tanggal_lahir ? tgl_indonesia($row->tanggal_lahir) : '-',
                    'Alamat'        => $row->alamat ?? '-',
                    'No. Telepon'   => $row->no_telepon ?? '-',
                    'Email'         => $row->email ?? '-',
                    'Tgl. Masuk'    => tgl_indonesia($row->tanggal_masuk),
                  ];
                  foreach ($fields as $label => $val): ?>
                  <tr>
                    <th style="width:160px; color:#5d0000;"><?php echo $label; ?></th>
                    <td>: <?php echo html_escape($val); ?></td>
                  </tr>
                  <?php endforeach; ?>
                </table>
              </div>

              <!-- Tab Penggajian -->
              <div class="tab-pane" id="tab-penggajian">
                <table class="table table-sm table-borderless">
                  <?php
                  $pg = [
                    'Divisi'       => $row->nama_divisi ?? '-',
                    'Jabatan'      => $row->nama_jabatan ?? '-',
                    'Status PTKP'  => $row->ptkp_status,
                    'Gaji Pokok'   => (float)$row->gaji_pokok > 0 ? 'Rp ' . rupiah($row->gaji_pokok) : '— (diatur via Mapping Komponen)',
                  ];
                  foreach ($pg as $label => $val): ?>
                  <tr>
                    <th style="width:160px; color:#5d0000;"><?php echo $label; ?></th>
                    <td>: <?php echo html_escape($val); ?></td>
                  </tr>
                  <?php endforeach; ?>
                </table>
                <a href="<?php echo site_url('master/mapping_komponen/pegawai/' . $row->id_pegawai); ?>" class="btn btn-danger btn-sm">
                  <i class="fa fa-sliders mr-1"></i> Atur Komponen Gaji
                </a>
              </div>

              <!-- Tab Penunjang -->
              <div class="tab-pane" id="tab-penunjang">
                <table class="table table-sm table-borderless">
                  <?php
                  $pj = [
                    'NPWP'                    => $row->npwp ?? '-',
                    'BPJS Kesehatan'          => $row->no_bpjs_kesehatan ?? '-',
                    'BPJS Ketenagakerjaan'    => $row->no_bpjs_ketenagakerjaan ?? '-',
                    'Bank'                    => $row->nama_bank ?? '-',
                    'No. Rekening'            => $row->no_rekening ?? '-',
                  ];
                  foreach ($pj as $label => $val): ?>
                  <tr>
                    <th style="width:200px; color:#5d0000;"><?php echo $label; ?></th>
                    <td>: <?php echo html_escape($val); ?></td>
                  </tr>
                  <?php endforeach; ?>
                </table>
              </div>

              <!-- Tab Riwayat Perubahan -->
              <div class="tab-pane" id="tab-riwayat">
                <?php if (!empty($riwayat_list)): ?>
                <table class="table table-sm table-bordered table-hover table-striped">
                  <thead>
                    <tr>
                      <th style="width:40px;">#</th>
                      <th>Waktu</th>
                      <th>Jenis</th>
                      <th>Nilai Lama</th>
                      <th>Nilai Baru</th>
                      <th>Keterangan</th>
                      <th>Dibuat Oleh</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php foreach ($riwayat_list as $i => $rw): ?>
                    <tr>
                      <td><?php echo $i + 1; ?></td>
                      <td><?php echo format_tanggal_waktu($rw->created_at); ?></td>
                      <td><span class="badge badge-info"><?php echo html_escape($rw->jenis_perubahan); ?></span></td>
                      <td><?php echo html_escape((string) $rw->nilai_lama); ?></td>
                      <td><?php echo html_escape((string) $rw->nilai_baru); ?></td>
                      <td><?php echo html_escape((string) $rw->keterangan); ?></td>
                      <td><?php echo html_escape($rw->dibuat_oleh ?: '-'); ?></td>
                    </tr>
                    <?php endforeach; ?>
                  </tbody>
                </table>
                <?php else: ?>
                <div class="alert alert-info mb-0"><i class="fa fa-info-circle mr-1"></i> Belum ada riwayat perubahan untuk pegawai ini.</div>
                <?php endif; ?>
              </div>
            </div>
          </div>
          <div class="card-footer">
            <a href="<?php echo site_url('master/pegawai'); ?>" class="btn btn-secondary btn-sm">
              <i class="fa fa-arrow-left mr-1"></i> Kembali ke Daftar
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<script>
$(function () {
  $(document).on('click', '.btn-confirm', function (e) {
    e.preventDefault();
    var href = $(this).attr('href');
    var msg  = $(this).data('msg') || 'Lanjutkan aksi ini?';
    Swal.fire({
      title: 'Konfirmasi', text: msg, icon: 'question',
      showCancelButton: true, confirmButtonColor: '#800000',
      cancelButtonText: 'Batal', confirmButtonText: 'Ya, lanjutkan'
    }).then(function (r) { if (r.isConfirmed) window.location.href = href; });
  });
});
</script>
