<?php
include("../public/impOpreation.php");
$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("trainersreportsview/show.html");
    $smarty->assign("trainers", 1);
    $smarty->display("footer.html");
} else if ($do == "showajax") {
    showajax();
}



function showajax() {
    $columns = array( 'id', 'id');
    $start_date = $_POST['start_date'];
    $data1      = $_POST['data1'];

    $searchQuery = " ";
    if($data1 != ''){
        $searchQuery .= " and trainersabsences.trainersid = ".$data1. " ";
    }

    

    // if($data3 != ''){
    //    $searchQuery .= " and addstudentstosubjects.branchid = ".$data3. " ";
    // }
    
    if($start_date != ''){
       $searchQuery .='and  trainersabsences.addtoday  >= "' . $start_date . ' 00-00-00" and trainersabsences.addtoday <= "' . $start_date . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( trainersabsences.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR employeename LIKE "%'.$_POST["search"]["value"].'%"
        )
     ";
    }

 $searchQuery .= "GROUP BY hour( trainersabsences.addtoday )";
    if(isset($_POST["order"]))
    {
    $searchQuery .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  trainersabsences.id DESC ";
    }
    
   
    
      $totals = R::count('trainersabsences','LEFT JOIN user ON trainersabsences.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  HOUR(addtoday) as houers, count(*) as counts  FROM `trainersabsences` LEFT JOIN user ON trainersabsences.adduserid = user.userid 
 WHERE 1 '.$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["houers"];
        $sub_array[] = $row["counts"];
        $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);
}






?>