<?php
require "../config.php";

if(!isset($_GET['id'])){
    die("ID tidak ditemukan");
}

$id = intval($_GET['id']);
$q = $conn->query("SELECT * FROM pengajuan WHERE id='$id'");

if($q->num_rows == 0){
    die("Data tidak ditemukan");
}

$d = $q->fetch_assoc();

/* ==== fallback field ==== */
$nama   = $d['nama'] ?? '';
$hp     = $d['no_hp'] ?? '';
$email  = $d['email'] ?? '';
$inst   = $d['instansi'] ?? '';
$kerja  = $d['pekerjaan'] ?? '';
$alamat = $d['alamat'] ?? '';
$kota   = $d['kota'] ?? '';
$nik    = $d['nik'] ?? '';
$no_surat_permohonan  = $d['no_surat_permohonan'] ?? '';
$tujuan = $d['tujuan'] ?? '';
$tgl    = $d['tanggal_permohonan'] ?? '';

function tampilFile($file){
    if(!$file) return "<i>Tidak ada file</i>";

    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    $path = "../uploads/".$file;

    if(in_array($ext,['jpg','jpeg','png','gif'])){
        return "<img src='$path' class='img-fluid border rounded' style='max-height:400px'>";
    } elseif($ext == 'pdf'){
        return "<iframe src='$path' width='100%' height='500px'></iframe>";
    } else {
        return "<a href='$path' target='_blank' class='btn btn-info btn-sm'>Download File</a>";
    }
}
?>

<?php include 'layout.php'; ?>

<h3 class="mb-3">📄 Detail Permohonan</h3>

<div class="card shadow">
<div class="card-body">

<!-- ===== FILE SURAT ===== -->
<div class="text-center mb-4">
<p><b>Surat Permohonan</b></p>
<?= tampilFile($d['file_surat']) ?>
</div>

<hr>

<!-- ===== BIODATA ===== -->
<h5 class="text-center mb-3">Detail Identitas</h5>

<table class="table table-bordered">
<tr><td>Nama</td><td><?= $nama ?></td></tr>
<tr><td>No HP</td><td><?= $hp ?></td></tr>
<tr><td>Email</td><td><?= $email ?></td></tr>
<tr><td>Instansi</td><td><?= $inst ?></td></tr>
<tr><td>Pekerjaan</td><td><?= $kerja ?></td></tr>
<tr><td>Alamat</td><td><?= $alamat ?></td></tr>
<tr><td>Kota</td><td><?= $kota ?></td></tr>
<tr><td>NIK</td><td><?= $nik ?></td></tr>
<tr><td>No Surat Permohonan</td><td><?= $no_surat_permohonan ?></td></tr>
<tr><td>Tujuan</td><td><?= $tujuan ?></td></tr>
<tr><td>Tanggal</td><td><?= $tgl ?></td></tr>
</table>

<!-- ===== FILE IDENTITAS ===== -->
<div class="text-center mt-4">
<p><b>File Identitas</b></p>
<?= tampilFile($d['file_identitas']) ?>
</div>

<!-- ===== BUTTON ===== -->
<div class="text-center mt-4">



<?php
$from = $_GET['from'] ?? '';

if($from == 'admin2'){

    // cek halaman asal admin2
    if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'surat_selesai') !== false){
        $kembali = '../admin2/surat_selesai';
    } else {
        $kembali = '../admin2/surat_diterima';
    }

} else {

    $kembali = !empty($_SERVER['HTTP_REFERER']) 
        ? $_SERVER['HTTP_REFERER'] 
        : 'surat_masuk';
}
?>

<a href="<?= $kembali ?>" class="btn btn-secondary">
Kembali
</a>

<?php
$tampilAksi = false;

if(isset($_SERVER['HTTP_REFERER'])){
    if(strpos($_SERVER['HTTP_REFERER'], 'surat_masuk') !== false){
        $tampilAksi = true;
    }
}
?>

<?php if($tampilAksi){ ?>

<a href="terima?id=<?= $d['id'] ?>" 
class="btn btn-success"
onclick="return confirm('Terima surat ini?')">
Terima
</a>

<a href="#" 
class="btn btn-danger"
data-bs-toggle="modal"
data-bs-target="#modalTolak<?= $d['id'] ?>">
Tolak
</a>

<?php } ?>
</div>

<!-- MODAL TOLAK -->
<div class="modal fade" id="modalTolak<?= $d['id'] ?>" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">

      <form method="POST" action="tolak">

        <div class="modal-header bg-danger text-white">
          <h5 class="modal-title">Tolak Surat</h5>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
        </div>

        <div class="modal-body">
          <input type="hidden" name="id" value="<?= $d['id'] ?>">

          <textarea name="pesan" class="form-control" rows="4" required
          placeholder="Masukkan alasan penolakan..."></textarea>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-danger">Tolak</button>
        </div>

      </form>

    </div>
  </div>
</div>
</div>
</div>

<?php include 'layout_footer.php'; ?>