<?php
//koneksi ke data base
$conn = mysqli_connect("localhost", "root", "", "proyekpsw4");

//ambil data
function query($query)
{
	global $conn;
	$result = mysqli_query($conn, $query);
	$rows = [];
	while ($row = mysqli_fetch_assoc($result)) {
		$rows[] = $row;
	}
	return $rows;
}

function hapus($id)
{
	global $conn;
	mysqli_query($conn, "DELETE FROM user WHERE id= $id");

	return mysqli_affected_rows($conn);
}

function hapuskeluhan($id)
{
	global $conn;
	mysqli_query($conn, "DELETE FROM keluhan WHERE id_keluhan= $id");

	return mysqli_affected_rows($conn);
}

function cari($keyword)
{
	$query = "SELECT * FROM user WHERE 
                username LIKE '%$keyword%' OR
				nama LIKE '%$keyword%'
                ";
	return query($query);
}

?>
<?php
function registrasi($data)
{
	global $conn;

	$username = strtolower(stripslashes($data["username"]));
	$password = mysqli_real_escape_string($conn, $data["password"]);
	$password2 = mysqli_real_escape_string($conn, $data["password2"]);
	$nama = stripslashes($data["nama"]);
	$alamat = stripslashes($data["alamat"]);
	$nim = stripslashes($data["nim"]);
	$jenis = stripslashes($data["jenis"]);
	$telepon = stripslashes($data["telepon"]);
	$email = stripslashes($data["email"]);
	$kerja = stripslashes($data["kerja"]);
	$tempat = stripslashes($data["tempat"]);
	$tanggal = stripslashes($data["tanggal"]);
	$bulan = stripslashes($data["bulan"]);
	$tahun = stripslashes($data["tahun"]);
	$yoyo = $tempat . ', ' . $tanggal . ' ' . $bulan . ' ' . $tahun;

	//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('user sudah terdaftar!');
                </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);

	//upload gambar
	$gambar = upload();
	if (!$gambar) {
		return false;
	}

	//tambahkan userbaru ke database
	mysqli_query($conn, "INSERT INTO user VALUES('','$gambar','$nama','$alamat','$nim','$yoyo','$jenis','$telepon','$email','$kerja','$username','$password','')");
	return mysqli_affected_rows($conn);
}

function upload()
{

	$namaFile = $_FILES['gambar']['name'];
	$ukuranFile = $_FILES['gambar']['size'];
	$error = $_FILES['gambar']['error'];
	$tmpName = $_FILES['gambar']['tmp_name'];

	// // cek apakah tidak ada gambar yang di upload
	// if ($error === 4) {
	// 	echo "
	//         <script>
	//         alert('pilih gambar terlebih dahulu!');
	//         </script>";
	// 	return false;
	// }

	// cek apakah yg diupload adalah gambar
	$ekstensiGambarValid = ['jpg', 'jpeg', 'png'];
	$ekstensiGambar = explode('.', $namaFile);
	$ekstensiGambar = strtolower(end($ekstensiGambar));
	if (!in_array($ekstensiGambar, $ekstensiGambarValid)) {
		echo "
            <script>
            alert('yang anda upload bukan gambar!');
            </script>";
		return false;
	}

	//  cek jika ukuran terlalu besar
	if ($ukuranFile > 1000000) {
		echo "
            <script>
            alert('ukuran gambar terlalu besar!');
            </script>";
		return false;
	}

	// lolos pengecekan, gambar siap di upload
	// generate nama gambar tertentu
	$namaFileBaru = uniqid();
	$namaFileBaru .= '.';
	$namaFileBaru .= $ekstensiGambar;


	move_uploaded_file($tmpName, 'img/' . $namaFileBaru);

	return $namaFileBaru;
}


function ubah($data)
{
	global $conn;

	$id = $data["id"];
	$alamat = htmlspecialchars($data["alamat"]);
	$nama = htmlspecialchars($data["nama"]);
	$email = htmlspecialchars($data["email"]);
	$nim = htmlspecialchars($data["nim"]);
	$tempat = htmlspecialchars($data["tempat"]);
	$jenis = htmlspecialchars($data["jenis"]);
	$telepon = htmlspecialchars($data["telepon"]);
	$pekerjaan = htmlspecialchars($data["kerja"]);
	$username = htmlspecialchars($data["username"]);
	$gambarLama = htmlspecialchars($data["gambarLama"]);

	// cek apakah user pilih gambar baru atau tidak
	if ($_FILES['gambar']['error'] === 4) {
		$gambar = $gambarLama;
	} else {
		$gambar = upload();
		$mhs = query("SELECT * FROM user WHERE id= $id")[0];
		$namagambar = $mhs["gambar"];
		$lokasigambar = "img/$namagambar";

		if (file_exists($lokasigambar)) {
			unlink($lokasigambar); // Delete now
		}
	}

	$query = "UPDATE user SET
				gambar = '$gambar',
				nama = '$nama',
                alamat = '$alamat',
                nim = '$nim',
				TTL = '$tempat',
                email = '$email',
				jenis = '$jenis',
                telepon = '$telepon',
                pekerjaan = '$pekerjaan',
				username = '$username'
            WHERE id = $id
                ";
	mysqli_query($conn, $query);
	return mysqli_affected_rows($conn);
}

function ubahAkunUser($data)
{
	global $conn;

	$id = $data["id"];
	$alamat = htmlspecialchars($data["alamat"]);
	$nama = htmlspecialchars($data["nama"]);
	$email = htmlspecialchars($data["email"]);
	$nim = htmlspecialchars($data["nim"]);
	$tempat = htmlspecialchars($data["tempat"]);
	$jenis = htmlspecialchars($data["jenis"]);
	$telepon = htmlspecialchars($data["telepon"]);
	$pekerjaan = htmlspecialchars($data["kerja"]);
	$gambarLama = htmlspecialchars($data["gambarLama"]);

	// cek apakah user pilih gambar baru atau tidak
	if ($_FILES['gambar']['error'] === 4) {
		$gambar = $gambarLama;
	} else {
		$gambar = upload();
		$mhs = query("SELECT * FROM user WHERE id= $id")[0];
		$namagambar = $mhs["gambar"];
		$lokasigambar = "img/$namagambar";

		if (file_exists($lokasigambar)) {
			unlink($lokasigambar); // Delete now
		}
	}

	$query = "UPDATE user SET
				gambar = '$gambar',
				nama = '$nama',
                alamat = '$alamat',
                nim = '$nim',
				TTL = '$tempat',
                email = '$email',
				jenis = '$jenis',
                telepon = '$telepon',
                pekerjaan = '$pekerjaan'
            WHERE id = $id
                ";
	mysqli_query($conn, $query);
	return mysqli_affected_rows($conn);
}


function tambahkeluhan($data)
{
	global $conn;

	$id = $data["id_user"];
	$keluhan = htmlspecialchars($data["keluhan"]);
	$id_jenis = htmlspecialchars($data["id_jenis"]);
	$tanggal = $data["tanggal"];
	$subjek = htmlspecialchars($data["subjek"]);

	//upload gambar
	$gambar = uploadkeluhan();
	if (!$gambar) {
		return false;
	}
	//upload gambar
	$gambar22 = uploadkeluhan22();
	if (!$gambar22) {
		return false;
	}

	//tambahkan data ke database
	mysqli_query($conn, "INSERT INTO keluhan VALUES('','$id','$id_jenis','$subjek','$keluhan','$tanggal','$gambar','$gambar22','')");
	return mysqli_affected_rows($conn);
}

function tambahjawaban($data)
{
	global $conn;

	$id_keluhan = $data["id_keluhan"];
	$tanggal = $data["tanggal"];
	$status = $data["status"];
	$jawaban = htmlspecialchars($data["jawaban"]);

	//tambahkan userbaru ke database
	mysqli_query($conn, "INSERT INTO jawaban VALUES('','$id_keluhan','$tanggal','$jawaban')");

	$query = "UPDATE keluhan SET
				status = '$status'
            WHERE id_keluhan = $id_keluhan
                ";
	mysqli_query($conn, $query);

	return mysqli_affected_rows($conn);
}



function uploadkeluhan()
{

	$namaFile = $_FILES['gambar1']['name'];
	$ukuranFile = $_FILES['gambar1']['size'];
	$error = $_FILES['gambar1']['error'];
	$tmpName = $_FILES['gambar1']['tmp_name'];

	// cek apakah tidak ada gambar yang di upload
	if ($error === 4) {
		return true;
	}

	// cek apakah yg diupload adalah gambar
	$ekstensiGambarValid = ['jpg', 'jpeg', 'png'];
	$ekstensiGambar = explode('.', $namaFile);
	$ekstensiGambar = strtolower(end($ekstensiGambar));
	if (!in_array($ekstensiGambar, $ekstensiGambarValid)) {
		echo "
            <script>
            alert('yang anda upload bukan gambar!');
            </script>";
		return false;
	}

	//  cek jika ukuran terlalu besar
	if ($ukuranFile > 1000000) {
		echo "
            <script>
            alert('ukuran gambar terlalu besar!');
            </script>";
		return false;
	}

	// lolos pengecekan, gambar siap di upload
	// generate nama gambar tertentu
	$namaFileBaru = uniqid();
	$namaFileBaru .= '.';
	$namaFileBaru .= $ekstensiGambar;


	move_uploaded_file($tmpName, 'imgKeluhan/' . $namaFileBaru);

	return $namaFileBaru;
}

function uploadkeluhan22()
{

	$namaFile = $_FILES['gambar2']['name'];
	$ukuranFile = $_FILES['gambar2']['size'];
	$error = $_FILES['gambar2']['error'];
	$tmpName = $_FILES['gambar2']['tmp_name'];

	// cek apakah tidak ada gambar yang di upload
	if ($error === 4) {
		return true;
	}

	// cek apakah yg diupload adalah gambar
	$ekstensiGambarValid = ['jpg', 'jpeg', 'png'];
	$ekstensiGambar = explode('.', $namaFile);
	$ekstensiGambar = strtolower(end($ekstensiGambar));
	if (!in_array($ekstensiGambar, $ekstensiGambarValid)) {
		echo "
            <script>
            alert('yang anda upload bukan gambar!');
            </script>";
		return false;
	}

	//  cek jika ukuran terlalu besar
	if ($ukuranFile > 1000000) {
		echo "
            <script>
            alert('ukuran gambar terlalu besar!');
            </script>";
		return false;
	}

	// lolos pengecekan, gambar siap di upload
	// generate nama gambar tertentu
	$namaFileBaru = uniqid();
	$namaFileBaru .= '.';
	$namaFileBaru .= $ekstensiGambar;


	move_uploaded_file($tmpName, 'imgKeluhan/' . $namaFileBaru);

	return $namaFileBaru;
}


function ubahjawabanAdmin($data)
{
	global $conn;

	$id_keluhan = $data["id_keluhan"];
	$tanggal = $data["tanggal"];
	$jawaban_baru = htmlspecialchars($data["jawaban"]);
	$id_jawaban = $data["id_jawaban"];

	$query = "UPDATE jawaban SET
				tanggal_jawaban = '$tanggal',
                jawaban = '$jawaban_baru'
            WHERE id_jawaban = $id_jawaban
                ";
	mysqli_query($conn, $query);
	return mysqli_affected_rows($conn);
}

function buat_pesan($data)
{
	global $conn;
	$nama = htmlspecialchars($data["nama"]);
	$email = htmlspecialchars($data["email"]);
	$tanggal = $data["tanggal"];
	$subjek = htmlspecialchars($data["subjek"]);
	$pesan = htmlspecialchars($data["pesan"]);

	//tambahkan data ke database
	mysqli_query($conn, "INSERT INTO kontak VALUES('','$nama','$email','$subjek','$pesan','$tanggal')");
	return mysqli_affected_rows($conn);
}

function hapusPesanMasuk($id)
{
	global $conn;
	mysqli_query($conn, "DELETE FROM kontak WHERE id= $id");

	return mysqli_affected_rows($conn);
}

?>