<?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';
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
// --- YILLIK İZİN HESAPLAMA (Burada da gerekli) ---
if (!function_exists('yillikIzinHesapla')) {
function yillikIzinHesapla($pdo, $calisan_id) {
$stmt = $pdo->prepare("SELECT ise_giris_tarihi, devreden_izin, dogum_tarihi FROM kullanicilar WHERE id = ?");
$stmt->execute([$calisan_id]);
$u = $stmt->fetch();
if(!$u) return 0;
$ise_giris = new DateTime($u['ise_giris_tarihi']);
$bugun = new DateTime();
$calisma_yili = $ise_giris->diff($bugun)->y;
// Yaş Hesabı
$yas = 0;
if (!empty($u['dogum_tarihi'])) {
$dt_dogum = new DateTime($u['dogum_tarihi']);
$yas = $bugun->diff($dt_dogum)->y;
}
$toplam_hak = floatval($u['devreden_izin']);
for($i = 1; $i <= $calisma_yili; $i++) {
$hak = 0;
if ($i <= 5) $hak = 14;
elseif ($i < 15) $hak = 20;
else $hak = 26;
// 50 yaş üstü ve 18 yaş altı kontrolü (Min 20 Gün)
if (($yas <= 18 || $yas >= 50) && $hak < 20) {
$hak = 20;
}
$toplam_hak += $hak;
}
$stmt_used = $pdo->prepare("SELECT SUM(toplam_gun) FROM izin_talepleri WHERE calisan_id = ? AND izin_turu = 'yillik' AND durum != 'reddedildi'");
$stmt_used->execute([$calisan_id]);
$kullanilan = floatval($stmt_used->fetchColumn());
return $toplam_hak - $kullanilan;
}
}
$sql = "SELECT * FROM kullanicilar WHERE rol != 'root' ORDER BY durum DESC, ad ASC";
$personeller = $pdo->query($sql)->fetchAll();
?>
<style>
.avatar-cell img { width: 45px; height: 45px; object-fit: cover; border: 2px solid #fff; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
tr.pasif-satir { background-color: #f8f9fa; opacity: 0.75; }
</style>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3><i class="fas fa-users-cog text-primary"></i> Personel Yönetimi</h3>
<div class="d-flex align-items-center gap-2">
<button onclick="exportList('excel')" class="btn btn-sm btn-success"><i class="fas fa-file-excel me-1"></i> Excel</button>
<a href="personel-ekle.php" class="btn btn-primary shadow-sm"><i class="fas fa-user-plus me-1"></i> Yeni Personel</a>
</div>
</div>
<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light text-secondary">
<tr>
<th class="ps-4">Personel</th>
<th>İletişim</th>
<th>Rol</th>
<th>Kalan Yıllık İzin</th> <th>Durum</th>
<th class="text-end pe-4">İşlemler</th>
</tr>
</thead>
<tbody>
<?php foreach($personeller as $p):
$kalan_izin = yillikIzinHesapla($pdo, $p['id']);
$izin_renk = ($kalan_izin > 0) ? 'bg-success' : 'bg-secondary';
// Durum HTML (Mevcut koddan alındı)
$durum_html = ($p['durum'] == 0) ? "<span class='badge bg-secondary'>Pasif</span>" : "<span class='badge bg-success bg-opacity-10 text-success border border-success'>Aktif</span>";
?>
<tr class="<?php echo ($p['durum'] == 0) ? 'pasif-satir' : ''; ?>">
<td class="ps-4">
<div class="d-flex align-items-center">
<div class="avatar-cell me-3"><img src="../<?php echo $p['fotograf_yolu']; ?>" class="rounded-circle"></div>
<div>
<div class="fw-bold"><a href="personel-kart.php?id=<?php echo $p['id']; ?>" class="text-dark text-decoration-none"><?php echo $p['ad'] . ' ' . $p['soyad']; ?></a></div>
<div class="small text-muted"><?php echo substr($p['tc_no'], 0, 3).'***'; ?></div>
</div>
</div>
</td>
<td><?php echo $p['telefon']; ?></td>
<td><span class="badge bg-light text-dark border text-uppercase"><?php echo str_replace('_', ' ', $p['rol']); ?></span></td>
<td>
<span class="badge <?php echo $izin_renk; ?> rounded-pill px-3"><?php echo number_format($kalan_izin, 1); ?> Gün</span>
</td>
<td><?php echo $durum_html; ?></td>
<td class="text-end pe-4">
<a href="personel-kart.php?id=<?php echo $p['id']; ?>" class="btn btn-sm btn-outline-info"><i class="fas fa-id-card"></i></a>
<a href="personel-duzenle.php?id=<?php echo $p['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-edit"></i></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php include '../includes/footer.php'; ?>