<?php
include "../config/koneksi.php";
include "../middleware/auth.php";

if($_SESSION['role'] != 'admin'){
    die("Akses ditolak");
}

// ðŸ”¥ FUNGSI FUZZY
function fuzzy_prioritas($kerusakan, $urgensi){
    $nilai = ($kerusakan * 0.6) + ($urgensi * 0.4);

    if($nilai >= 70) return "Tinggi";
    elseif($nilai >= 40) return "Sedang";
    else return "Rendah";
}

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

    $judul = $_POST['judul'];
    $deskripsi = $_POST['deskripsi'];
    $lokasi = $_POST['lokasi'];
    $kerusakan = $_POST['kerusakan'];
    $urgensi = $_POST['urgensi'];
    $tanggal = date('Y-m-d');

    // ðŸ”¥ HITUNG FUZZY
    $prioritas = fuzzy_prioritas($kerusakan, $urgensi);

    // ðŸ”¥ UPLOAD FOTO
    $foto = $_FILES['foto']['name'];
    $tmp  = $_FILES['foto']['tmp_name'];

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

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

        $query = "INSERT INTO komplain 
        (judul, deskripsi, lokasi, kerusakan, urgensi, prioritas, tanggal, foto)
        VALUES 
        ('$judul','$deskripsi','$lokasi','$kerusakan','$urgensi','$prioritas','$tanggal','$foto')";

        if(mysqli_query($conn,$query)){
            echo "<script>alert('Komplain berhasil ditambahkan!');window.location='data_komplain.php';</script>";
        }else{
            echo "Error: " . mysqli_error($conn);
        }

    }else{
        echo "Upload gagal!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Tambah Komplain | PT. ALVINDO</title>

<style>
body{font-family:Arial;background:#f4f6f9}
.container{width:500px;margin:50px auto;background:white;padding:20px;border-radius:10px}
input,textarea,select{width:100%;padding:10px;margin:10px 0}
button{background:#1e3c72;color:white;padding:10px;border:none;width:100%}
</style>
<link rel="stylesheet" href="../assets/sikompro-ux.css">
</head>

<body>

<div class="container">
<h2>Input Komplain</h2>

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

<input type="text" name="judul" placeholder="Judul Komplain" required>

<textarea name="deskripsi" placeholder="Deskripsi"></textarea>

<input type="text" name="lokasi" placeholder="Lokasi" required>

<label>Kerusakan (0-100)</label>
<input type="number" name="kerusakan" min="0" max="100" required>

<label>Urgensi (0-100)</label>
<input type="number" name="urgensi" min="0" max="100" required>

<label>Upload Foto</label>
<input type="file" name="foto" required>

<button name="simpan">Simpan Komplain</button>

</form>

</div>

<?php include '../partials/mobile_nav.php'; ?>
</body>
</html>



