<?php

session_start();

if(!isset($_SESSION['role'])){

    header("Location: ../index.php");
    exit;
}

if($_SESSION['role'] != 'admin'){

    header("Location: ../index.php");
    exit;
}

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

$laporan_id = intval($_POST['laporan_id']);
$rumah_ibadah_id = intval($_POST['rumah_ibadah_id']);
$cs_id = intval($_POST['cs_id']);

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

$cek = mysqli_query(
$conn,
"SELECT * FROM bantuan
WHERE laporan_id='$laporan_id'"
);

if(mysqli_num_rows($cek) > 0){

    echo "
    <script>
    alert('Bantuan sudah pernah diassign');
    window.location='validasi.php';
    </script>
    ";

    exit;
}

mysqli_query($conn,

"INSERT INTO bantuan
(laporan_id,rumah_ibadah_id,cs_id,jenis_bantuan,status)

VALUES

(
'$laporan_id',
'$rumah_ibadah_id',
'$cs_id',
'$jenis_bantuan',
'proses'
)"

);

$cek_laporan = mysqli_query(
$conn,
"SELECT * FROM laporan
WHERE id='$laporan_id'"
);

if(mysqli_num_rows($cek_laporan)==0){
    die("Laporan tidak ditemukan");
}

/* UPDATE STATUS */

mysqli_query($conn,

"UPDATE laporan
SET status='diproses'
WHERE id='$laporan_id'"

);

header("Location: validasi.php");

?>