<?php
session_start();
if (isset($_SESSION['logged']) && $_SESSION['logged'] == "1" && $_SESSION['role'] == "admin") {

require_once('../../assets/constants/config.php');
require_once('../../assets/constants/uniques.php');

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        
    $stmt = $conn->prepare("SELECT * FROM tbl_school_info");
    $stmt->execute();
    $result = $stmt->fetchAll();
    
    foreach($result as $row) {

        $currency = $row[6];
        date_default_timezone_set($row[5]);

    }
                          
    }catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }


$name = ucwords($_POST['name']);
$reg_date = date('Y-m-d G:i:s');

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	
$stmt = $conn->prepare("SELECT * FROM tbl_departments WHERE name = :name");
$stmt->bindParam(':name', $name);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec > 0) {

$_SESSION['reply'] = "002";
header("location:../departments");

}else{

$stmt = $conn->prepare("INSERT INTO tbl_departments (name, reg_date) VALUES (:name, :reg_date)");

$stmt->bindParam(':name', $name);
$stmt->bindParam(':reg_date', $reg_date);
$stmt->execute();

$_SESSION['reply'] = "003";
header("location:../departments");

}

					  
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

}else{

header("location:../");

}

?>
