<?php
defined('BASEPATH') or exit('No direct script access allowed');

class HomeController extends CI_Controller
{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
		$this->load->model('UserModel', 'usermodel');
	}

	public function index()
	{
		$this->load->view('landing');
	}
	public function student_register()
	{
		$this->load->view('student_register');
	}

	public function student_registration()
	{
		$stdImg = $_FILES['StudentImg']['name'];
		$FirstName = $this->input->post("FirstName");
		$LastName = $this->input->post("LastName");
		$Email = $this->input->post("Email");
		$Password = base64_encode($this->input->post("Password"));
		$PhoneNumber = $this->input->post("PhoneNumber");
		$Affiliate = $this->input->post("Affiliate");
		$Course = $this->input->post("Course");
		$Interest = $this->input->post("Interest");
		$Link = $this->input->post("Link");
		$config = [
			"upload_path" => "uploads/student",
			"allowed_types" => "jpg|jpeg|png",
			"file_name" => $stdImg
		];
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ($this->upload->do_upload('StudentImg')) {
			$uploadData = $this->upload->data();
			$picture = $uploadData['file_name'];
		}
		$data = [
			"photo" => $stdImg,
			"firstname" => $FirstName,
			"lastname" => $LastName,
			"email" => $Email,
			"password" => $Password,
			"phoneno" => $PhoneNumber,
			"affiliate" => $Affiliate,
			"course" => $Course,
			"interest" => $Interest,
			"researchlink" => $Link
		];
		$this->db->select('*')->from('student');
		$this->db->where('email',$Email);
		$query = $this->db->get();
        $row = $query->row();

        if(!empty($row))
       {
		    $this->session->set_flashdata('user', 'Email registered please use another email.');
            redirect("student_register");
       }
       else
       {
            if ($this->db->insert("student", $data)) {
			$this->load->view('login');
		    } else {
			$this->session->set_flashdata('failure', 'Registration failed.');
			redirect("student_register");
		    } 
       }
	}

	public function professor_register()
	{
		$this->load->view('professor_register');
	}

	public function professor_registration()
	{
		$profImg = $_FILES['image']['name'];
		$FirstName = $this->input->post("firstname");
		$LastName = $this->input->post("lastname");
		$Email = $this->input->post("email");
		$Password = base64_encode($this->input->post("password"));
		$PhoneNumber = $this->input->post("phoneno");
		$position = $this->input->post("position");
		$Affiliate = $this->input->post("affiliate");
		$Research = $this->input->post("research");
		$Link = $this->input->post("link");
		$config = [
			"upload_path" => "uploads/professor",
			"allowed_types" => "jpg|jpeg|png",
			"file_name" => $profImg
		];
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ($this->upload->do_upload('image')) {
			$uploadData = $this->upload->data();
			$picture = $uploadData['file_name'];
		}
		$data = [
			"photo" => $profImg,
			"firstname" => $FirstName,
			"lastname" => $LastName,
			"email" => $Email,
			"password" => $Password,
			"phoneno" => $PhoneNumber,
			"position" => $position,
			"affiliate" => $Affiliate,
			"research" => $Research,
			"researchlink" => $Link
		];
		$this->db->select('*')->from('professor');
		$this->db->where('email',$Email);
		$query = $this->db->get();
        $row = $query->row();

        if(!empty($row))
       {
		    $this->session->set_flashdata('user', 'Email registered please use another email.');
            redirect("professor_register");
       }
       else
       {
            if ($this->db->insert("professor", $data)) {
			$this->load->view('login');
		    } else {
			$this->session->set_flashdata('failure', 'Registration failed.');
			 redirect("professor_register");
		    } 
       }
		
	}

	public function login()
	{
		$this->load->view('login');
	}

	public function auth()
	{
		$email = $this->input->post("email");
		$pass = base64_encode($this->input->post("pass"));
		$type = $this->input->post("type");
		if($type == "1") {
			$authChk = $this->db->select("id")->from("student")->where("email", $email)->where("password", $pass)->get()->result_Array();
			if(count($authChk) > 0) {
				echo json_encode(["status" => 200]);
				$_SESSION['id'] = $authChk[0]['id'];
			} else {
				echo json_encode(["status" => 500]);
				exit;
			}
		}
		if($type == "2") {
			$authChk = $this->db->select("id")->from("professor")->where("email", $email)->where("password", $pass)->get()->result_Array();
			if(count($authChk) > 0) {
				echo json_encode(["status" => 200]);
				$_SESSION['id'] = $authChk[0]['id'];
			} else {
				echo json_encode(["status" => 500]);
				exit;
			}
		}
	}
	public function dashboard()
	{
	    $this->load->view('dashboard');
	}
}
