<?php
// modules/personel-yonetimi.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
include '../includes/header.php';
include '../includes/menu.php';
// Yetkili Roller
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
// Root rolü hariç tüm personeli çeken tek ve sadeleştirilmiş sorgu.
$sql = "SELECT * FROM kullanicilar WHERE rol != 'root' ORDER BY ad ASC";
$personeller = $pdo->query($sql)->fetchAll();
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3><i class="fas fa-users-cog"></i> Personel Yönetimi</h3>
<div class="d-flex align-items-center gap-2">
<button type="button" onclick="exportList('excel')" class="btn btn-sm btn-success" title="Excel İndir">
<i class="fas fa-file-excel"></i> XLS
</button>
<button type="button" onclick="exportList('yazdir')" class="btn btn-sm btn-danger" title="PDF Önizle">
<i class="fas fa-file-pdf"></i> PDF
</button>
<?php if(in_array($_SESSION['rol'], ['root', 'yonetici', 'insan_kaynaklari'])): ?>
<a href="personel-ekle.php" class="btn btn-primary"><i class="fas fa-user-plus"></i> Yeni Personel Ekle</a>
<?php endif; ?>
</div>
</div>
<div class="card border-0 shadow-sm">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light">
<tr>
<th width="60">Foto</th>
<th>Ad Soyad / Durum</th>
<th>Rol</th>
<th>Telefon</th>
<th class="text-end pe-4">İşlemler</th>
</tr>
</thead>
<tbody>
<?php foreach ($personeller as $p):
$resim = !empty($p['fotograf_yolu']) ? "../".$p['fotograf_yolu'] : "../assets/img/default-user.png";
?>
<tr class="<?php echo ($p['durum'] == 0) ? 'table-secondary text-muted' : ''; ?>">
<td>
<img src="<?php echo $resim; ?>" class="rounded-circle border" width="40" height="40" style="object-fit: cover;">
</td>
<td>
<a href="personel-kart.php?id=<?php echo $p['id']; ?>" target="_blank" class="text-decoration-none fw-bold text-dark">
<?php echo guvenlik($p['ad'] . ' ' . $p['soyad']); ?>
</a>
<?php if($p['durum'] == 1): ?>
<div class="mt-1"><?php echo personelDurumu($pdo, $p['id']); ?></div>
<?php else: ?>
<div class="mt-1"><span class="badge bg-secondary">PASİF</span></div>
<?php endif; ?>
</td>
<td><span class="badge bg-info text-dark"><?php echo strtoupper($p['rol']); ?></span></td>
<td><?php echo $p['telefon']; ?></td>
<td class="text-end pe-3">
<a href="personel-kart.php?id=<?php echo $p['id']; ?>" target="_blank" class="btn btn-sm btn-info text-white" title="Personel Kartı">
<i class="fas fa-id-card"></i>
</a>
<?php if(in_array($_SESSION['rol'], ['root', 'yonetici', 'insan_kaynaklari', 'muhasebe'])): ?>
<a href="personel-duzenle.php?id=<?php echo $p['id']; ?>" class="btn btn-sm btn-primary" title="Düzenle">
<i class="fas fa-edit"></i>
</a>
<?php endif; ?>
<?php if(in_array($_SESSION['rol'], ['root', 'yonetici', 'insan_kaynaklari']) && $p['rol'] != 'root'): ?>
<a href="personel-sil.php?id=<?php echo $p['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Personeli pasife almak istediğinize emin misiniz?');">
<i class="fas fa-trash-alt"></i>
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
function exportList(format) {
// Bu fonksiyon, filtre parametrelerini hazırlayarak rapor-olustur.php'yi çağırır.
// Personel Yönetimi sayfasında filtre olmadığı için tüm aktif personeli isteriz.
// NOT: personel_bilgi raporu için zorunlu parametreler: tur, format, rol_filtre, durum_filtre
let url = '../modules/rapor-olustur.php?';
url += 'tur=personel_bilgi';
url += '&format=' + format;
url += '&rol_filtre=tumu'; // Tüm roller
url += '&durum_filtre=1'; // Sadece aktif personel (Listede görünenler)
window.open(url, '_blank');
}
</script>
<?php include '../includes/footer.php'; ?>