<?php
 header("Access-Control-Allow-Origin: *");
date_default_timezone_set("Africa/Lagos");
//include connection file 
include_once("db_cls_connect.php");
include('buiapp/partials/header.php');

$params = $_REQUEST;
//$action = $params['action'] != '' ? $params['action'] : '';
$staff = new Staff();
/*
switch ($action) {
    case 'login':
        $staff->login();
        break;
    case 'logout':
        $staff->logout();
        break;
    default:
        return;
}
*/
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Staff
 *
 * @author Username
 */

class Staff {

    //put your code here
    public function __construct() {
        $db = new dbObj();
        $connString = $db->getConnstring();
        $this->conn = $connString;
    }

     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 * FROM staffs WHERE password='$user_password' and staffsId= '$user_name'";
            $resultset = mysqli_query($this->conn, $sql) or die("database error:" . mysqli_error($this->conn));
            if (mysqli_num_rows($resultset)>0) {
                $row = mysqli_fetch_assoc($resultset);
                $_SESSION['staff_session'] = $user_name;
                //get the details
				calogin($_SESSION['staff_session']);
                echo "1";
            }else{
				 $sql = "SELECT * FROM staffs WHERE phonenumber='$user_password' and staffsId= '$user_name'";
            $resultset = mysqli_query($this->conn, $sql) or die("database error:" . mysqli_error($this->conn));
			 if (mysqli_num_rows($resultset)>0) {
				 $_SESSION['staff_reg'] = $user_name;
				echo "Reg";
			 }else {
                echo "Ohhh ! Wrong Credential."; // wrong details
            }
			
			
			
			}



			
        }
    }
    
    	function logout() {
			calogout($_SESSION['staff_session']);
		unset($_SESSION['staff_session']);
		if(session_destroy()) {
			header("Location: ../../index.php");
		}
    
        }

        public function insertStaff($depcode, $coscode, $description, $cosunit) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "INSERT INTO course(CourseID, Description, UNIT, Departments_DepartmentCode) VALUES  (trim('$coscode'),trim('$description'), trim('$cosunit'), trim('$depcode'))";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

    public function getAllStaffs() {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = 'SELECT * FROM staffs';

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    
    public function getAllStaffsByCourseCode($course_code) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM `staff_course_mapping` JOIN staffs ON (staff_course_mapping.staff_id=staffs.staffsId)
WHERE staff_course_mapping.course_id='$course_code'";
        
        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    
    public function getCoursesAssignedToStaff($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM `staff_course_mapping` JOIN course ON (staff_course_mapping.course_id=course.CourseID)
WHERE staff_course_mapping.staff_id='$id'";
        
        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

    public function assignStaff($id, $course_id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
        $today=date("Y-m-d");
        
        $sql="SELECT id from staff_course_mapping WHERE staff_id ='$id' and course_id='$course_id' ";
        $query = mysqli_query($this->conn, $sql);
        
        if(mysqli_num_rows($query)>0){
            return 0;
        }else{
        $sql = "INSERT INTO staff_course_mapping(staff_id, course_id,date_) VALUES  ('$id','$course_id','$today')";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        
        }
        return 1;
    }
    
    public function unassignStaff($id, $course_id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
       
        $sql = "DELETE FROM staff_course_mapping WHERE staff_id='$id' AND  course_id='$course_id' ";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        
        
        return 1;
    }
public function getStaffByID($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM staffs WHERE staffsId='$id'";
        
        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
       
        if(mysqli_num_rows($query)>0){
            //valid
            $rs= mysqli_fetch_assoc($query);
            return $rs;
        }else{
            //wrong
            return 0;
        }
        
        
        
    }
}



function calogout($username){
	$dbc = connect();
	$timestamp = strtotime("now");
	$deviceid = getdeviceip();	
	mysqli_query($dbc, "INSERT into logs(action,actor,message,lastseen, deviceid) VALUES('Logout','$username', 'User logged out', '$timestamp', '$deviceid' )");
	
}
function calogin($username){
	$dbc = connect();
	$timestamp = strtotime("now");
	$deviceid = getdeviceip();
	mysqli_query($dbc, "INSERT into logs(action,actor,message,lastseen, deviceid) VALUES('Login','$username', 'User logged in', '$timestamp', '$deviceid' )");
	
}

