<?php 


class log_app
{	

	public $koneksi;

	public function login()
	{
		$this->koneksi;
		$user = $_POST['user'];
		$pass = $_POST['pass'];
		$login = mysqli_query($this->koneksi, "SELECT * FROM tb_user WHERE username = '$user'");
		$cek = mysqli_num_rows($login);

		if ($cek > 0) {
			$key = mysqli_fetch_array($login);
			if (!password_verify($pass, $key['password'])) {
				return false;
			}
			if ($key['level']=="admin" || $key['level']=="logistik" || $key['level']=="gudang") {
				session_start();
				$_SESSION['idkaskit'] = $key['id'];
				$_SESSION['userkaskit'] = $key['username'];
				$_SESSION['passkaskit'] = $key['password'];
				$_SESSION['namakaskit'] = $key['nama'];
				$_SESSION['levelkaskit'] = $key['level'];
				$_SESSION['fotokaskit'] = $key['foto'];
				// Log login activity
				$log_tgl = date("Y-m-d");
				$log_jam = date("H:i:s");
				$log_detail = "Login: " . $key['username'] . " (" . $key['level'] . ")";
				mysqli_query($this->koneksi, "INSERT INTO activity_log SET user_id='" . $key['id'] . "', action='login', detail='$log_detail', tanggal='$log_tgl', jam='$log_jam'");
				header("location: admin/index.php?m=awal&msg=login_success");
				exit;
			}else if ($key['level']=="kasir") {
				session_start();
				$_SESSION['idkaskit'] = $key['id'];
				$_SESSION['userkaskit'] = $key['username'];
				$_SESSION['passkaskit'] = $key['password'];
				$_SESSION['namakaskit'] = $key['nama'];
				$_SESSION['levelkaskit'] = $key['level'];
				$_SESSION['fotokaskit'] = $key['foto'];
				// Log login activity
				$log_tgl = date("Y-m-d");
				$log_jam = date("H:i:s");
				$log_detail = "Login: " . $key['username'] . " (" . $key['level'] . ")";
				mysqli_query($this->koneksi, "INSERT INTO activity_log SET user_id='" . $key['id'] . "', action='login', detail='$log_detail', tanggal='$log_tgl', jam='$log_jam'");
				header("location: kasir/index.php?m=awal&msg=login_success");
				exit;
			}
		}else{
			return false;
		}


	}

	public function sesi()
	{

	}
}

$proses = new log_app();
require_once __DIR__ . '/../../koneksi.php';
$proses->koneksi = $koneksi;
 ?>

