<?php
include("../public/impOpreation.php");

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');


$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$do = $_GET['do'];

$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) { 
    $smarty->display("header.html"); 
    $smarty->display("studentsexamsview/add.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $studentsetting = R::load('studentsetting',1);
    $smarty->assign("studentsetting",$studentsetting);
    $smarty->assign('date', $date);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("header.html"); 
    $smarty->display("studentsexamsview/show.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    $id = filter_input(INPUT_GET, 'id');
    $addstudent = filter_input(INPUT_GET, 'addstudent');
    $studentsexams = R::load('studentsexams', $id);
    $studentaddsubjects = R::findOne('studentaddsubjects','id = ? and del < 2',[$studentsexams->studentaddsubjectid]);
    $studentsubjects = R::findOne('studentsubjects','id = ? and del < 2',[$studentaddsubjects->studentsubjectid]);
    
    if ($studentsexams->examsstudent != 1 ) {
        $studentaddsubjectdetails = R::findAll('studentaddsubjectdetails', " studentaddsubjectid = ? and  studentaddsubjectdetails.del < 2",[$studentsexams->studentaddsubjectid]);
        foreach($studentaddsubjectdetails as $studentaddsubjectdetail){
            $studentsexamsval = R::dispense('studentsexamsval');
            $studentsexamsval->studentid  = $studentaddsubjectdetail->studentid;
            $studentsexamsval->examspresence = 1;
            $studentsexamsval->studentaddsubjectid  = $studentaddsubjectdetail->studentaddsubjectid;
            $studentsexamsval->studentsubjectgroupid  = $studentaddsubjectdetail->studentsubjectgroupid;
            $studentsexamsval->studentaddsubjectid  = $studentaddsubjectdetail->studentaddsubjectid;
            $studentsexamsval->studentsubjectid  = $studentaddsubjectdetail->studentsubjectid;
            $studentsexamsval->del = 0;
            $studentsexamsval->addtoday = $today;
            $studentsexamsval->adduserid = $_SESSION['userid']; 
            $studentsexamsval->deltoday = '';
            $studentsexamsval->deluserid = ''; 
            $studentsexamsval->studentsexamid  = $id;
            R::store($studentsexamsval);
        }
    }
    $studentsexamsval = R::findAll('studentsexamsval', "studentsexamid = ? and del < 2",[$id]); 
    foreach($studentsexamsval as $studentsexamsva){
          $students = R::findOne('students','id = ? and del < 2',[$studentsexamsva->studentid]);
          $studentsexamsva->student = $students->studentname.' / '. $students->studentphone;
          
        
    }
    $smarty->assign('studentsexamsval', $studentsexamsval);    
    $studentsexams->examsstudent  = 1;
    R::store($studentsexams); 
    $studentsexams->studentsubjects = $studentsubjects->subjectname;    
    $smarty->assign('studentsexams', $studentsexams);
    $smarty->assign('addstudent', $addstudent);
    $smarty->display("header.html"); 
    $smarty->display("studentsexamsview/edit.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
}else if ($do == 'getallstudenttime') {
$studentid = filter_input(INPUT_POST, 'studentid');  
$studentsexamid = filter_input(INPUT_POST, 'studentsexamid'); 
$studentsexams = R::load('studentsexams', $studentsexamid);
$studentsexams->studentid  = $studentsexams->studentid.','.$studentid;
R::store($studentsexams);
$student = R::load('students', $studentid);
$studentsexamsval = R::dispense('studentsexamsval');
$studentsexamsval->studentid  = $student->id;
$studentsexamsval->examsvalue  = "";
$studentsexamsval->examspresence = 1;
$studentsexamsval->studentaddsubjectid  = $studentsexams->studentaddsubjectid;
$studentsexamsval->studentsubjectgroupid  = $studentsexams->studentsubjectgroupid;
$studentsexamsval->studentaddsubjectid  = $studentsexams->studentaddsubjectid;
$studentsexamsval->studentsubjectid  = $studentsexams->studentsubjectid;
$studentsexamsval->del = 0;
$studentsexamsval->addtoday = $today;
$studentsexamsval->adduserid = $_SESSION['userid']; 
$studentsexamsval->deltoday = '';
$studentsexamsval->deluserid = ''; 
$studentsexamsval->studentsexamid  = $studentsexamid;
$studentsexamsvalid =R::store($studentsexamsval);

$smarty->assign('student', $student);
$smarty->assign('studentsexamsvalid', $studentsexamsvalid);
$smarty->display("studentsexamsview/getallstudenttime.html");
} else if ($do == "savedata") {
    savedata();
} else if ($do == "showajax") {
    showajax();
} else if ($do == "examsstudent") {
    examsstudent();
} else if ($do == "getstudentnotexam") {
    getstudentnotexam();
}


function savedata() {
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $studentaddsubjectid = filter_input(INPUT_POST, 'studentaddsubjectid');
    $examname = filter_input(INPUT_POST, 'examname');
    $greatdegree = filter_input(INPUT_POST, 'greatdegree');
    $studentsexamid = filter_input(INPUT_POST, 'studentsexamid');
    $studentaddsubjectsdata = R::load('studentaddsubjects',$studentaddsubjectid); 
    $studentaddsubjects = R::findAll('studentaddsubjects', "studentsubjectid = ? and del < 2 ",[$studentaddsubjectsdata->studentsubjectid]);  
    if (!$studentsexamid) {
        foreach($studentaddsubjects as $studentaddsubject) {
            $studentsexams = R::dispense('studentsexams');
            $studentsexams->deltoday = '';
            $studentsexams->deluserid = ''; 
            $studentsexams->del = 0;
            $studentsexams->addtoday = $today;          
            $studentsexams->adduserid = $userid;
            $studentsexams->studentaddsubjectid  = $studentaddsubject->id;
            $studentsexams->studentsubjectid   = $studentaddsubject->studentsubjectid;
            $studentsexams->studentsubjectgroupid  = $studentaddsubject->studentsubjectgroupid;
            $studentsexams->studentid  = $studentaddsubject->studentid;
            $studentsexams->examname  = $examname;
            $studentsexams->greatdegree  = $greatdegree;
            R::store($studentsexams);  
        }
    }else{
       $studentsexams = R::load('studentsexams',$studentsexamid); 
       $studentsexams->del = 1;
       $studentsexams->updatetoday = $today;          
       $studentsexams->updateuserid = $userid; 
       $studentsexams->examname  = $examname;
       $studentsexams->greatdegree  = empty($greatdegree) ? $studentsexams->greatdegree : $greatdegree;
       R::store($studentsexams);
    }
    
    try {
        
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


function showajax() {
    $columns = array('id', 'examname', 'greatdegree','subjectname','subjectgroupname','subjectgroupprice', 'addtoday', 'employeename', 'id', 'id', 'id');
    $start_date = $_POST['start_date'];
    $end_date   = $_POST['end_date'];
    $del        = $_POST['del'];
    $data1      = $_POST['data1'];
    $data2      = $_POST['data2'];
    $data3      = $_POST['data3'];

    $searchQuery = " ";
    if($data1 != ''){
        $searchQuery .= " and studentsexams.studentaddsubjectid = ".$data1. " ";
    }

    if($data2 != ''){
       $searchQuery .= " and studentsexams.id = ".$data2. " "; 
    }
    if($del == ''){
       $searchQuery .= " and studentsexams.del < 2 "; 
    }

    // if($data3 != ''){
    //    $searchQuery .= " and addstudentstosubjects.branchid = ".$data3. " ";
    // }
    
    if($start_date != '' && $end_date != ''){
       $searchQuery .='and  studentsexams.addtoday  >= "' . $start_date . ' 00-00-00" and studentsexams.addtoday <= "' . $end_date . ' 23-59-55" ';
    }else{
        $today = date("Y-m-d");
        $searchQuery .='and  studentsexams.addtoday  >= "' . $today . ' 00-00-00" and studentsexams.addtoday <= "' . $today . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( studentsexams.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR studentsexams.examname LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentsexams.greatdegree LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentsexams.subjectname LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentsexams.subjectgroupname LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentsexams.subjectgroupprice LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentsexams.addtoday LIKE "%'.$_POST["search"]["value"].'%"
                    OR employeename LIKE "%'.$_POST["search"]["value"].'%"
        )
     ";
    }


    if(isset($_POST["order"]))
    {
    $searchQuery .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  studentsexams.id DESC ";
    }
    
      $totals = R::count('studentsexams','LEFT JOIN user ON studentsexams.adduserid = user.userid WHERE 1 '.$searchQuery.' ');
         
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }
 $rResult = R::getAll('SELECT examname, greatdegree,studentsexams.id as id, studentsexams.addtoday as addtoday, studentsexams.del as del, employeename ,studentsubjects.del as studentsubjectsdel ,studentsubjectgroups.del as studentsubjectgroupsdel,  
 subjectname, subjectgroupname, subjectgroupprice  FROM `studentsexams` LEFT JOIN user ON studentsexams.adduserid = user.userid 
  LEFT JOIN studentsubjects ON studentsexams.studentsubjectid = studentsubjects.id 
 LEFT JOIN studentsubjectgroups ON studentsexams.studentsubjectgroupid = studentsubjectgroups.id WHERE studentsubjects.del < 2 and studentsubjectgroups.del < 2 '.$searchQuery.' ');  
    $apps = count($rResult);
     
    $output = array(  
         "draw" => intval($_POST["draw"]),    
        "recordsTotal" => intval($apps),
        "recordsFiltered" => $totals,
        "data" => array()
    );
  
       foreach ($rResult as $row) {
        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["examname"];
        $sub_array[] = $row["greatdegree"];
        $sub_array[] = $row["subjectname"];
        $sub_array[] = $row["subjectgroupname"];
        $sub_array[] = $row["subjectgroupprice"];
        $sub_array[] = $row["addtoday"];
        $sub_array[] = $row["employeename"];
        if($row["del"] < 2){    
        $sub_array[] = '<a href="studentsExamsController.php?do=edit&id='. $row["id"] .'" type="button" class="btn btn-default btn-lg editicon"></a>';
         $sub_array[] = '<a href="javascript:;" data-id="'. $row["id"] .'" data-table="studentsexams"  type="button" class="btn btn-default btn-lg deleteicon removerow"  ></a>';
        }else{
          $sub_array[] = '<a href="studentsExamsController.php?do=edit&id='. $row["id"] .'" type="button" class="btn btn-default btn-lm">تفاصيل</a>';
          $sub_array[] = 'محذوف ';
        }
       
        $output['data'][] = $sub_array;
       }
    
       echo json_encode($output, JSON_UNESCAPED_SLASHES);

}

function examsstudent() {
    $examsvalue = filter_input(INPUT_POST, 'examsvalue');
    $examspresence = filter_input(INPUT_POST, 'examspresence');
    $id = filter_input(INPUT_POST, 'id');
    $tables = R::load('studentsexamsval',$id);
    $tables->del = 1;
    $tables->updatetoday = date("Y-m-d H:i:s");
    $tables->updateuserid = $_SESSION['userid']; 
    $tables->examsvalue = $examsvalue;  
    $tables->examspresence = $examspresence;     
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


function getstudentnotexam() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $studentaddsubjectid = $_POST['studentaddsubjectid'];
    $studentsexamid = $_POST['studentsexamid'];

    $row_array = array();
    $return_arr = array();
    $search = '';
    $studentid = '0';
    $studentaddsubjectdetails = R::findAll('studentaddsubjectdetails', "studentaddsubjectid = ? and del < 2",[$studentaddsubjectid]);
    foreach($studentaddsubjectdetails as $studentaddsubjectdetail){
        $countstudents = R::count('studentsexamsval', "studentid = ? and studentsexamid = ? and del < 2",[$studentaddsubjectdetail->studentid, $studentsexamid]);
        if ($countstudents == 0) {
             $studentid .= ','.$studentaddsubjectdetail->studentid;
         } 

     
    }
    $search .= " and del < 2 and students.id in ($studentid) ";
    $allData = R::getAll("SELECT id,CONCAT($tablesearch) as name
        FROM students 
        WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['id'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}






?>