<?php

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



// ==========================
// AMBIL DATA
// ==========================

$id = $_POST['id'];

$file = time().'_'.$_FILES['file']['name'];

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

$folder = "../uploads/".$file;



// ==========================
// VALIDASI FILE
// ==========================

$ekstensi =
pathinfo($file, PATHINFO_EXTENSION);

$allowed = [
'pdf',
'doc',
'docx',
'png',
'jpg',
'jpeg'
];

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

    echo "

    <script>

    alert('Format file tidak didukung');

    window.location='dashboard.php';

    </script>

    ";

    exit;
}



// ==========================
// UPLOAD FILE
// ==========================

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



    // ==========================
    // UPDATE DATABASE
    // ==========================

    mysqli_query(

        $conn,

        "UPDATE berkas
         SET
         file='$file',
         status='pending',
         keterangan=''
         WHERE id='$id'"
    );



    echo "

    <script>

    alert('Berkas revisi berhasil diupload');

    window.location='dashboard.php';

    </script>

    ";

}else{

    echo "

    <script>

    alert('Upload gagal');

    window.location='dashboard.php';

    </script>

    ";
}

?>