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

$id = $_GET['id'];

$data = $conn->query("SELECT * FROM pengajuan WHERE id='$id'");
$d = $data->fetch_assoc();

if(isset($_POST['kirim'])){

    $pesan = $conn->real_escape_string($_POST['pesan']);

  // ================= FILE WAJIB =================
$file_balasan = "";

if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

    $nama_asli = $_FILES['file']['name'];
    $tmp       = $_FILES['file']['tmp_name'];

    $ext = strtolower(pathinfo($nama_asli, PATHINFO_EXTENSION));

    $allowed = ['pdf','doc','docx','xls','xlsx'];

    if(in_array($ext, $allowed)){

        $nama_baru = time() . "_" . preg_replace("/[^a-zA-Z0-9._-]/", "_", $nama_asli);

        $path = "../uploads/" . $nama_baru;

        if(move_uploaded_file($tmp, $path)){

            if(filesize($path) > 0){
                $file_balasan = $nama_baru;
            } else {
                unlink($path);
                die("File wajib gagal upload");
            }

        } else {
            die("Gagal upload file wajib");
        }

    } else {
        die("Format file wajib tidak diizinkan!");
    }

} else {
    die("Surat balasan wajib diupload!");
}


// ================= FILE OPSIONAL =================
$file_lampiran = "";

if(isset($_FILES['file2']) && $_FILES['file2']['error'] == 0){

    $nama_asli2 = $_FILES['file2']['name'];
    $tmp2       = $_FILES['file2']['tmp_name'];

    $ext2 = strtolower(pathinfo($nama_asli2, PATHINFO_EXTENSION));

    $allowed = ['pdf','doc','docx','xls','xlsx'];

    if(in_array($ext2, $allowed)){

        $nama_baru2 = time() . "_lampiran_" . preg_replace("/[^a-zA-Z0-9._-]/", "_", $nama_asli2);

        $path2 = "../uploads/" . $nama_baru2;

        if(move_uploaded_file($tmp2, $path2)){

            if(filesize($path2) > 0){
                $file_lampiran = $nama_baru2;
            }

        }
    }
}

    // update database
   $conn->query("
    UPDATE pengajuan SET
    pesan_balasan='$pesan',
    file_balasan='$file_balasan',
    file_lampiran='$file_lampiran',
    status_admin2='Sudah Dikirim',
    status='Selesai'
    WHERE id='$id'
");

 
    // **Simpan riwayat status ke log_status**
    $conn->query("
    INSERT INTO log_status (kode_tiket, status, tanggal)
    VALUES ('".$d['kode_tiket']."','Selesai',NOW())
    ");
    
    echo "<script>
        alert('Balasan berhasil dikirim');
        location='surat_diterima';
    </script>";
}
?>

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body class="bg-light">

<div class="container mt-5">

<div class="card shadow p-4">

<h4>Balas Tiket</h4>
<hr>

<p><b>No Tiket:</b> <?= $d['kode_tiket'] ?></p>
<p><b>Nomor Surat:</b> <?= $d['no_surat'] ?></p>
<p><b>Disposisi:</b> <?= $d['disposisi'] ?></p>
<p><b>Nama:</b> <?= $d['nama'] ?? $d['nama_lengkap'] ?></p>
<p><b>Email:</b> <?= $d['email'] ?></p>

<form method="POST" enctype="multipart/form-data">

<div class="mb-3">
<label>Upload Surat Balasan (Word / Excel / PDF)</label>
<input type="file" name="file" class="form-control"
accept=".doc,.docx,.xls,.xlsx,.pdf">
</div>

<div class="mb-3">
<label>Upload File Balasan (Opsional)</label>
<input type="file" name="file2" class="form-control"
accept=".doc,.docx,.xls,.xlsx,.pdf">
</div>

<div class="mb-3">
<label>Pesan Balasan</label>
<textarea name="pesan" class="form-control" rows="6"></textarea>
</div>

<button type="submit" name="kirim"
class="btn btn-success w-100">
Kirim Balasan
</button>

</form>

</div>
</div>

</body>
</html>