<?php
//include connection file 
include_once("db_cls_connect.php");
include('buiapp/partials/header.php');
$db = new dbObj();
$connString = $db->getConnstring();

$params = $_REQUEST;
$action = $params['action'] != '' ? $params['action'] : '';
$admCls = new Admin($connString);

switch ($action) {
    case 'login':
        $admCls->login();
        break;
    case 'logout':
        $admCls->logout();
        break;
    default:
        return;
}

class Admin {

    //put your code here
    private $username;
    private $password;
    private $names;
    private $email;
    protected $conn;
    protected $data = array();

    function __construct($connString) {
        $this->conn = $connString;
    }

    function getUsername() {
        return $this->username;
    }

    function getPassword() {
        return $this->password;
    }

    function getNames() {
        return $this->names;
    }

    function getEmail() {
        return $this->email;
    }

    function setUsername($username) {
        $this->username = $username;
    }

    function setPassword($password) {
        $this->password = $password;
    }

    function setNames($names) {
        $this->names = $names;
    }

    function setEmail($email) {
        $this->email = $email;
    }

    function loginAdmin() {
        $avail = false;

        return $avail;
    }

    function login() {
        if (isset($_POST['login-submit'])) {
            $user_name = mysqli_real_escape_string($this->conn, trim($_POST['username']));
            $user_password = mysqli_real_escape_string($this->conn, trim($_POST['password']));
            $sql = "SELECT idAdmin, concat(concat(lastName, ', '), concat(firstName, '')) AS Name, password FROM admin WHERE deleted='no' and idAdmin= '$user_name'";
            $resultset = mysqli_query($this->conn, $sql) or die("database error:" . mysqli_error($this->conn));
            $row = mysqli_fetch_assoc($resultset);
            if (($user_password) == $row['password']) {
                $_SESSION['user_session'] = $row['idAdmin'];
                //get the details
                echo "1";
            } else {
                echo "Ohhh ! Wrong Credential."; // wrong details
            }
        }
    }
    
    	function logout() {
		unset($_SESSION['user_session']);
		if(session_destroy()) {
			header("Location: ../index.php");
		}
    
        }

}
