<?php 
include '../koneksi.php';
session_start();
date_default_timezone_set('Asia/Jakarta');


$petugas = $_SESSION['id'];
//$waktu = date('Y-m-d H:i:s'); 
$id  = $_POST['kode_surat'];
$nomor_surat  = $_POST['nomor_surat'];
$tgl_surat  = $_POST['tgl_surat'];
$jml_monev  = $_POST['jml_monev'];

$rand = rand();

$filename = $_FILES['file']['name'];

if($filename == ""){

	mysqli_query($koneksi, "update undangan set no_surat='$nomor_surat', tgl_surat='$tgl_surat', jml_monev='$jml_monev' where kode_surat='$id'")or die(mysqli_error($koneksi));
	header("location:undangan.php");

}else{

	$jenis = pathinfo($filename, PATHINFO_EXTENSION);

	if($jenis == "php") {
		header("location:undangan.php?alert=gagal");
	}else{

		// hapus file lama
		$lama = mysqli_query($koneksi,"select * from undangan where kode_surat='$id'");
		$l = mysqli_fetch_assoc($lama);
		$nama_file_lama = $l['arsip_file'];
		unlink("../arsipmonev/".$nama_file_lama);

		// upload file baru
		move_uploaded_file($_FILES['file']['tmp_name'], '../arsipmonev/'.$rand.'_'.$filename);
		$nama_file = $rand.'_'.$filename;
		mysqli_query($koneksi, "update undangan set no_surat='$nomor_surat',tgl_surat='$tgl_surat', jml_monev='$jml_monev' file_surat ='$nama_file' where kode_surat='$id'")or die(mysqli_error($koneksi));
		header("location:undangan.php?alert=sukses");
	}
}

