<?php
// modules/tum-izinler.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
// Yetki
yetkiKontrol(['root', 'yonetici', 'mudur', 'vardiya_amiri', 'muhasebe', 'insan_kaynaklari']);
include '../includes/header.php';
include '../includes/menu.php';
$mesaj = '';
$kullanici_id = $_SESSION['kullanici_id'];
$rol = $_SESSION['rol'];
// --- İŞLEM (ONAY / RED / GERİ ALMA) ---
if (isset($_GET['islem']) && isset($_GET['id'])) {
$id = (int)$_GET['id'];
$aksiyon = $_GET['islem'];
// Yetki Kontrolü için sorgu hazırlığı
$sql_yetki = "SELECT * FROM izin_talepleri WHERE id = ?";
$yetki_params = [$id];
// Yönetici/Müdür sadece kendine geleni, Diğerleri (İK/Muhasebe/Root) her şeyi
if(!in_array($rol, ['root', 'muhasebe', 'insan_kaynaklari'])) {
// GÜVENLİK GÜNCELLEMESİ: $kullanici_id parametre olarak eklendi
$sql_yetki .= " AND hedef_yonetici_id = ?";
$yetki_params[] = $kullanici_id;
}
$stmt = $pdo->prepare($sql_yetki);
$stmt->execute($yetki_params);
$izin = $stmt->fetch();
if ($izin) {
$yeni_durum = '';
if($aksiyon == 'onayla') $yeni_durum = 'onaylandi';
elseif($aksiyon == 'reddet') $yeni_durum = 'reddedildi';
elseif($aksiyon == 'beklemede') $yeni_durum = 'beklemede'; // Geri alma
// Sadece İK ve Muhasebe onaylı/reddedilmiş bir şeyi değiştirebilir
if ($izin['durum'] != 'beklemede' && !in_array($rol, ['root', 'muhasebe', 'insan_kaynaklari'])) {
$mesaj = "<div class='alert alert-warning'>Onaylanmış işlemi değiştirme yetkiniz yok. İK ile görüşün.</div>";
} else {
$pdo->prepare("UPDATE izin_talepleri SET durum = ?, onaylayan_id = ?, onaylanma_tarihi = NOW() WHERE id = ?")->execute([$yeni_durum, $kullanici_id, $id]);
// LOG KAYDI
$log_detay = "İzin durumu değişti: " . strtoupper($aksiyon) . " (Önceki: {$izin['durum']})";
logKaydet($pdo, $kullanici_id, 'guncelleme', $log_detay, 'izin_talepleri', $id);
// Bildirim
bildirimGonder($pdo, $izin['calisan_id'], "İzin Durumu Güncellendi", "İzin talebiniz $yeni_durum olarak güncellendi.", "../modules/izin-talep.php");
$mesaj = "<div class='alert alert-success'>İşlem başarıyla uygulandı: <strong>".strtoupper($yeni_durum)."</strong></div>";
}
}
}
// --- LİSTELEME ---
$sql_list = "SELECT i.*, k.ad, k.soyad, k.rol as k_rol, o.ad as o_ad, o.soyad as o_soyad
FROM izin_talepleri i
JOIN kullanicilar k ON i.calisan_id = k.id
LEFT JOIN kullanicilar o ON i.onaylayan_id = o.id ";
$list_params = [];
if (!in_array($rol, ['root', 'muhasebe', 'insan_kaynaklari'])) {
// GÜVENLİK GÜNCELLEMESİ: $kullanici_id parametre olarak eklendi
$sql_list .= " WHERE i.hedef_yonetici_id = ? ";
$list_params[] = $kullanici_id;
}
$sql_list .= " ORDER BY i.olusturulma_tarihi DESC";
$stmt_list = $pdo->prepare($sql_list);
$stmt_list->execute($list_params);
$izinler = $stmt_list->fetchAll();
?>
<div class="container-fluid">
<h3 class="mb-4"><i class="fas fa-calendar-check text-primary"></i> İzin Yönetim Paneli</h3>
<?php echo $mesaj; ?>
<div class="card border-0 shadow-sm">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-dark">
<tr>
<th>Personel</th>
<th>Tür</th>
<th>Tarih / Süre</th>
<th>Açıklama / Not</th> <th>Durum / İşlem Yapan</th>
<th class="text-end">Yönetim</th>
</tr>
</thead>
<tbody>
<?php foreach ($izinler as $iz): ?>
<tr>
<td>
<strong><?php echo $iz['ad'].' '.$iz['soyad']; ?></strong><br>
<span class="badge bg-light text-dark border"><?php echo strtoupper($iz['k_rol']); ?></span>
</td>
<td>
<?php if($iz['izin_turu'] == 'saatlik'): ?>
<span class="badge bg-warning text-dark">SAATLİK</span>
<?php else: ?>
<span class="badge bg-secondary"><?php echo strtoupper($iz['izin_turu']); ?></span>
<?php endif; ?>
</td>
<td>
<?php echo tarihTurkce(date('Y-m-d', strtotime($iz['baslangic_tarihi']))); ?>
<br>
<small class="text-muted">
<?php echo ($iz['izin_turu']=='saatlik') ? $iz['saatlik_sure'].' Saat' : floatval($iz['toplam_gun']).' Gün'; ?>
</small>
</td>
<td>
<small class="text-wrap" style="max-width: 250px;">
<?php
// Açıklamayı göster, çok uzunsa kısalt
echo mb_substr($iz['aciklama'], 0, 80, 'UTF-8');
if (mb_strlen($iz['aciklama'], 'UTF-8') > 80) {
echo '...';
}
?>
</small>
</td>
<td>
<?php if($iz['durum']=='onaylandi'): ?>
<div class="text-success fw-bold"><i class="fas fa-check-circle"></i> Onaylandı</div>
<?php if($iz['o_ad']): ?>
<small class="text-muted text-nowrap">Yapan: <?php echo $iz['o_ad'].' '.$iz['o_soyad']; ?></small>
<?php endif; ?>
<?php elseif($iz['durum']=='reddedildi'): ?>
<span class="badge bg-danger">Reddedildi</span>
<?php else: ?>
<span class="badge bg-warning text-dark">Beklemede</span>
<?php endif; ?>
</td>
<td class="text-end pe-3">
<div class="btn-group">
<?php if($iz['durum'] == 'beklemede'): ?>
<a href="?islem=onayla&id=<?php echo $iz['id']; ?>" class="btn btn-sm btn-success" title="Onayla"><i class="fas fa-check"></i></a>
<a href="?islem=reddet&id=<?php echo $iz['id']; ?>" class="btn btn-sm btn-danger" title="Reddet"><i class="fas fa-times"></i></a>
<?php elseif(in_array($rol, ['root', 'muhasebe', 'insan_kaynaklari'])): ?>
<button type="button" class="btn btn-sm btn-secondary dropdown-toggle" data-bs-toggle="dropdown">
<i class="fas fa-cog"></i>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="?islem=beklemede&id=<?php echo $iz['id']; ?>">Beklemeye Al</a></li>
<li><a class="dropdown-item text-danger" href="?islem=reddet&id=<?php echo $iz['id']; ?>">İptal Et / Reddet</a></li>
</ul>
<?php endif; ?>
<?php if($iz['durum'] == 'onaylandi'): ?>
<a href="izin-yazdir.php?id=<?php echo $iz['id']; ?>" target="_blank" class="btn btn-sm btn-dark ms-1" title="Yazdır"><i class="fas fa-print"></i></a>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php include '../includes/footer.php'; ?>