<?php
date_default_timezone_set('Asia/Jakarta');

// Koneksi ke Database
error_reporting(E_ALL);
$conn = mysqli_connect("localhost", "jbkzbmma", 'o7B-a54CP7fp#G', "jbkzbmma_student");


error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
	
function query($sql) {
	global $conn;
	$result = mysqli_query($conn, $sql);
	$rows = [];
	while( $row = mysqli_fetch_assoc($result) ) {
		$rows[] = $row;
	}
	return $rows;
}




function hapus($id) {
    session_start();
    //$logkanwil=$_SESSION["logkanwil"]; 

	global $conn;
	mysqli_query($conn, "delete from data_student where id = $id");
	         return mysqli_affected_rows($conn);
            
    }



function tambah($data) {
    
    session_start();
    $logkanwil=$_SESSION["logkanwil"]; 
 	global $conn;
    
    $nama = htmlspecialchars($data["nama"]);
    $sekolah = htmlspecialchars($data["sekolah"]);

	$kelas = htmlspecialchars($data["kelas"]);
    $jk = htmlspecialchars($data["jk"]);
    $ramalan_ai = htmlspecialchars($data["ramalan_ai"]);
     $tgl_lahir = htmlspecialchars($data["tgl_lahir"]);
     $alamat = htmlspecialchars($data["alamat"]);
     $wawa = htmlspecialchars($data["wawa"]);

$sql = "INSERT INTO data_student (nama,sekolah,kelas,jk,ramalan_ai,alamat,wawa,tgl_lahir) 
             VALUES ('$nama','$sekolah','$kelas','$jk','$ramalan_ai','$alamat','$wawa','$tgl_lahir')";


    mysqli_query($conn, $sql);
	return mysqli_affected_rows($conn);

}





function ubah($data) {
    global $conn;

    $id = $data["id"];
    $nama = htmlspecialchars($data["nama"]);
    $sekolah = htmlspecialchars($data["sekolah"]);
    $kelas = htmlspecialchars($data["kelas"]);
    $jk = $data["jk"];
    $tgl_lahir = $data["tgl_lahir"];
    $wawa = htmlspecialchars($data["wawa"]);
    $alamat = htmlspecialchars($data["alamat"]);
    $ramalan_ai = htmlspecialchars($data["ramalan_ai"]);
    $terima_uang = htmlspecialchars($data["terima_uang"]);
    $testimoni = htmlspecialchars($data["testimoni"]);

    // Ambil data lama
    $old = query("SELECT * FROM data_student WHERE id = $id")[0];
    $foto = isset($data["foto"]) ? $data["foto"] : $old["foto"];

    $query = "UPDATE data_student SET
                nama = '$nama',
                sekolah = '$sekolah',
                kelas = '$kelas',
                jk = '$jk',
                tgl_lahir = '$tgl_lahir',
                wawa = '$wawa',
                alamat = '$alamat',
                ramalan_ai = '$ramalan_ai',
                terima_uang = '$terima_uang',
                testimoni = '$testimoni',
                foto = '$foto'
              WHERE id = $id";

    mysqli_query($conn, $query);
    return mysqli_affected_rows($conn);
}





function registrasi($data) {
	global $conn;

	$username = stripslashes($data["username"]);
	$password = mysqli_real_escape_string($conn, $data["password"]);
	$password2 = mysqli_real_escape_string($conn, $data["password2"]);
        $gol = htmlspecialchars($data["gol"]);
        $kanwil = htmlspecialchars($data["kanwil"]);
        

	// cek username sudah ada atau belum
	$result = mysqli_query($conn, "SELECT username FROM user WHERE username = '$username'");

	if( mysqli_fetch_assoc($result) ) {
		echo "<script>
				alert('Username sudah terdaftar!')
		      </script>";
		return false;
	}
	
 if (empty($username) or empty($password) or empty($gol) or empty($kanwil))
  {
   echo "<script>
			alert('Field ada yang masih kosong!!');
			document.location.href = 'registrasi.php';
		 </script>";
		 return false; 
    }
    
    
	// cek konfirmasi password
	if( $password !== $password2 ) {
		echo "<script>
				alert('Konfirmasi password tidak sesuai!');
		      </script>";
		return false;
	}

	// enkripsi password
	$password = password_hash($password, PASSWORD_DEFAULT);

	// tambahkan userbaru ke database
	mysqli_query($conn, "INSERT INTO user VALUES('', '$username', '$password', '$gol','$kanwil')");

	return mysqli_affected_rows($conn);

}

function gantipass($data) {
	global $conn;

	$username = stripslashes($data["username"]);
	$passwordold = mysqli_real_escape_string($conn, $data["passwordold"]);
	
	$password = mysqli_real_escape_string($conn, $data["password"]);
	$password2 = mysqli_real_escape_string($conn, $data["password2"]);
        $gol = htmlspecialchars($data["gol"]);
        $kanwil = htmlspecialchars($data["kanwil"]);



     //cek username 
     $result = mysqli_query($conn, "SELECT username FROM user WHERE username = '$username' ");
	if  ( !mysqli_fetch_assoc($result) ) {
		echo "<script>
				alert('Username tidak valid')
		      </script>";
		return false;
	}
	


		// cek password lama
	$result = mysqli_query($conn, "SELECT * FROM user WHERE username = '$username' ");
		$row = mysqli_fetch_assoc($result);
		if( !password_verify($passwordold, $row["password"]) ) {
			// set session
			echo "<script>
				alert('Password lama tidak valid')
		      </script>";
		return false;	
		}

 if (empty($password))

   {
   echo "<script>
			alert('Password baru masih kosong!!');
		 </script>";
		 return false; 
    }


	// cek konfirmasi password
	if( $password !== $password2 ) {
		echo "<script>
				alert('Konfirmasi password tidak sesuai!');
		      </script>";
		return false;
	}

	// enkripsi password
	$password = password_hash($password, PASSWORD_DEFAULT);



	$sql = "UPDATE user SET
	            password = '$password'
			    
			WHERE
				username = '$username'";
			
	mysqli_query($conn, $sql);
	return mysqli_affected_rows($conn);

}



// Ramalan AI

function tambah_ramalan_ai($data) {
    
    session_start();
    $logkanwil=$_SESSION["logkanwil"]; 
    
	global $conn;
    
    $nilai = htmlspecialchars($data["nilai"]);

$sql = "INSERT INTO penilaian_ai (nilai) 
             VALUES ('$nilai')";


    mysqli_query($conn, $sql);
	return mysqli_affected_rows($conn);

}


function hapus_ramalan_ai($id) {
    session_start();
    //$logkanwil=$_SESSION["logkanwil"]; 

	global $conn;
	mysqli_query($conn, "delete from penilaian_ai where id = $id");
	         return mysqli_affected_rows($conn);
            
    }

function ubah_ramalan_ai($data) {
    global $conn;

    $id = $data["id"];
    $nilai = htmlspecialchars($data["nilai"]);

    $query = "UPDATE penilaian_ai SET
                nilai = '$nilai'
              WHERE id = $id";

    mysqli_query($conn, $query);
    return mysqli_affected_rows($conn);
}



function getRamalanAI() {
    global $conn;

    $query = "SELECT nilai FROM penilaian_ai ORDER BY RAND() LIMIT 1";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        return $row['nilai'];
    } else {
        return "Tidak ada ramalan tersedia.";
    }
}

?>