<?php
include '../../classes/students_class.php';
$student=new Student("../../manage/connection.php");
$address="";
$image="default.jpg";
session_start();
$admin=$_SESSION['admin']['id'];
if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['phone']) && isset($_POST['gender']) && !empty($_POST['gender']) && isset($_POST['dob']) && isset($_POST['state']) && isset($_POST['address'])
&& isset($_POST['class_room']) && !empty($_POST['class_room']) && isset($_POST['parent_name']))
{
    if(isset($_POST['address']))
    {
        $address=htmlentities($_POST['address']);
    }
    $first_name=htmlentities($_POST['first_name']);
    $last_name=htmlentities($_POST['last_name']);
    $phone=htmlentities($_POST['phone']);
    $dob=$_POST['dob'];
    $gender=htmlentities($_POST['gender']);
    $state=htmlentities($_POST['state']);
    $address=htmlentities($_POST['address']);
    $class_room=htmlentities($_POST['class_room']);
    $parent_name=htmlentities($_POST['parent_name']);
    if(isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name']))
    {
        $temp_name=$_FILES['image']['tmp_name'];
        $image=$phone.'_'.$_FILES['image']['name'];
        move_uploaded_file($temp_name,'../images/'.uniqid('student_').$image);
    }
    $added_on=time();

    if($student->addStudent($first_name, $last_name, $dob, $gender, $state, $address, $class_room, $parent_name, $phone, $admin, $image, $added_on))
    {
        echo 'Student Added';
    }
    else
    {
        echo 'Could Not Add';
    }
    // if(!$student->findStudent($phone))
    // {
    //     if($student->addStudent($first_name, $last_name, $dob, $gender, $state, $address, $class_room, $parent_name, $phone, $admin, $image, $added_on))
    //     {
    //         echo 'Student Added';
    //     }
    //     else
    //     {
    //         echo 'Could Not Add';
    //     }
    // }
    // else
    // {
    //     echo "Student With parent's phone Already Exist";
    // }
}
else
{
    echo '<div>Fields Not Set</div>';
    if(!isset($_POST['first_name']))
    {
        echo '<div>Name Not Set</div>';
    }
    if(!isset($_POST['phone']))
    {
        echo '<div>Phone Not Set</div>';
    }
    if(!isset($_POST['gender']))
    {
        echo '<div>Gender Not Set</div>';
    }
    if(empty($_POST['gender']))
    {
        echo '<div>Gender Not Set</div>';
    }
    if(!isset($_POST['dob']))
    {
        echo '<div>Passwor Not Set</div>';
    }
}
?>