<?php

session_start();

include '../config/koneksi.php';

$pelapor_id = $_SESSION['id'];

$nama_warga = mysqli_real_escape_string(
$conn,
$_POST['nama_warga']
);

$alamat = mysqli_real_escape_string(
$conn,
$_POST['alamat']
);

$kondisi = mysqli_real_escape_string(
$conn,
$_POST['kondisi']
);

$latitude = floatval($_POST['latitude']);
$longitude = floatval($_POST['longitude']);

/* FOTO */
$foto =
time().'_'.
basename($_FILES['foto']['name']);

$tmp = $_FILES['foto']['tmp_name'];

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

$allowed = [
'jpg',
'jpeg',
'png',
'webp'
];

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

    echo "
    <script>
    alert('Format foto harus JPG, PNG, atau WEBP');
    history.back();
    </script>
    ";

    exit;
}

if(!move_uploaded_file(
    $tmp,
    "../assets/uploads/".$foto
)){
    die("Upload foto gagal");
}
/* INSERT */

mysqli_query($conn,

"INSERT INTO laporan

(
pelapor_id,
nama_warga,
alamat,
latitude,
longitude,
kondisi,
foto,
status
)

VALUES

(
'$pelapor_id',
'$nama_warga',
'$alamat',
'$latitude',
'$longitude',
'$kondisi',
'$foto',
'pending'
)"

);

echo "
<script>
alert('Laporan berhasil dikirim');
window.location='dashboard.php';
</script>
";

?>