<?php
session_start();
include 'koneksi.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = mysqli_real_escape_string($koneksi, $_POST['nama']);
    $instansi = mysqli_real_escape_string($koneksi, $_POST['instansi']);
    $telepon = mysqli_real_escape_string($koneksi, $_POST['telepon']);
    $email = mysqli_real_escape_string($koneksi, $_POST['email']);
    $jenis_layanan = mysqli_real_escape_string($koneksi, $_POST['jenis_layanan']);
    $keterangan = mysqli_real_escape_string($koneksi, $_POST['keterangan']);
    
    // Buat tabel pengajuan jika belum ada
    $create_table = "CREATE TABLE IF NOT EXISTS pengajuan_arsip (
        id INT AUTO_INCREMENT PRIMARY KEY,
        nama VARCHAR(100),
        instansi VARCHAR(100),
        telepon VARCHAR(20),
        email VARCHAR(100),
        jenis_layanan VARCHAR(50),
        keterangan TEXT,
        status VARCHAR(20) DEFAULT 'pending',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )";
    mysqli_query($koneksi, $create_table);
    
    $query = "INSERT INTO pengajuan_arsip (nama, instansi, telepon, email, jenis_layanan, keterangan) 
              VALUES ('$nama', '$instansi', '$telepon', '$email', '$jenis_layanan', '$keterangan')";
    
    if (mysqli_query($koneksi, $query)) {
        $_SESSION['success'] = "Pengajuan berhasil dikirim! Petugas kami akan segera menghubungi Anda.";
    } else {
        $_SESSION['error'] = "Gagal mengirim pengajuan: " . mysqli_error($koneksi);
    }
    
    header("Location: layanan_arsip_dinamis.php");
    exit();
} else {
    header("Location: layanan_arsip_dinamis.php");
    exit();
}
?>