<?php

/*
 * 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.
 */

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

class Department {

    public $departmentCode;
    public $departmentName;
    public $facultyCode;

    public function __construct() {
        $db = new dbObj();
        $connString = $db->getConnstring();
        $this->conn = $connString;
    }

    public function setDepartmentCode($departmentCode) {
        $this->departmentCode = $departmentCode;
    }

    public function setDepartmentName($departmentName) {
        $this->departmentName = $departmentName;
    }

    public function setFacultyCode($facultyCode) {
        $this->facultyCode = $facultyCode;
    }

    public function checkDpCode($depCode) {
        $depCode = trim($depCode);
        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM departments WHERE DepartmentCode='$depCode'";


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

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        if (mysqli_num_rows($query) > 0) {
            return 'taken';
        } else {
            return 'not_taken';
        }
        return "not_taken";
    }

    public function insertDepartment($dep_code, $fal_code, $description) {

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

        $sql = "INSERT INTO departments(DepartmentCode, DepartmentName, Faculty_FacultyCode1) VALUES (trim('$dep_code'),trim('$description'), trim('$fal_code'))";

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

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

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

        $sql = 'SELECT * FROM departments';

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

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

}
