<?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');

$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("studentspaymentsystemsview/add.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $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("studentspaymentsystemsview/show.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $studentspaymentsystem = R::load('studentspaymentsystems', $id);
    $studentsubjects = R::findOne('studentsubjects','id = ? and del < 2',[$studentspaymentsystem->studentsubjectid]);
     $studentspaymentsystem->studentsubjects = $studentsubjects->subjectname;
    $smarty->assign('studentspaymentsystem', $studentspaymentsystem);
    $smarty->display("header.html"); 
    $smarty->display("studentspaymentsystemsview/edit.html");
    $smarty->assign("student", 1);
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
  savedata();
}else if ($do == 'showajax') {
  showajax();
}else if ($do == 'removecontroller') {
  removecontroller();
}


function savedata() {
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $paymentname = filter_input(INPUT_POST, 'paymentname');
    $paymentprice = filter_input(INPUT_POST, 'paymentprice');
    $paymentmonth = filter_input(INPUT_POST, 'paymentmonth');
    $studentaddsubjectid = filter_input(INPUT_POST, 'studentaddsubjectid');
    $studentspaymentsystemid = filter_input(INPUT_POST, 'studentspaymentsystemid');

    if (!$studentspaymentsystemid) {;
        $studentspaymentsystems = R::dispense('studentspaymentsystems');
        $studentspaymentsystems->del = 0;
        $studentspaymentsystems->addtoday = $today;          
        $studentspaymentsystems->adduserid = $userid;
        $studentspaymentsystems->deltoday = '';  
        $studentspaymentsystems->deluserid = '';
    }else{
        $studentspaymentsystems = R::load('studentspaymentsystems',$studentspaymentsystemid);
        $studentspaymentsystems->del = 1;
        $studentspaymentsystems->updatetoday = $today;          
        $studentspaymentsystems->updateuserid = $userid; 
    }
    $subjects = R::load('studentaddsubjects',$studentaddsubjectid);
    $studentspaymentsystems->paymentname  = $paymentname;
    $studentspaymentsystems->paymentprice = $paymentprice;
    $studentspaymentsystems->paymentmonth = $paymentmonth;
    $studentspaymentsystems->studentaddsubjectid = $studentaddsubjectid;
    $studentspaymentsystems->studentsubjectid = $subjects->studentsubjectid;
    try {
        R::store($studentspaymentsystems);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


function showajax() {
    $columns = array('id', 'paymentname', 'paymentprice', 'paymentmonth','subjectname','subjectprice', 'addtoday', 'employeename', '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 studentspaymentsystems.studentaddsubjectid = ".$data1. " ";
    }

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

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


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( studentspaymentsystems.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR studentspaymentsystems.paymentname LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentspaymentsystems.paymentprice LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentspaymentsystems.paymentmonth LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentspaymentsystems.subjectname LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentspaymentsystems.subjectprice LIKE "%'.$_POST["search"]["value"].'%"
                    OR studentspaymentsystems.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  studentspaymentsystems.id DESC ";
    }
          $totals = R::count('studentspaymentsystems','LEFT JOIN user ON studentspaymentsystems.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 paymentname, paymentprice, paymentmonth, studentspaymentsystems.id as id, studentspaymentsystems.addtoday as addtoday, studentspaymentsystems.del as del, employeename 
     ,studentsubjects.del as studentsubjectsdel,
 subjectname, subjectprice  FROM `studentspaymentsystems` 
  LEFT JOIN user ON studentspaymentsystems.adduserid = user.userid 
  LEFT JOIN studentsubjects ON studentspaymentsystems.studentsubjectid = studentsubjects.id  WHERE studentsubjects.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["paymentname"];
        $sub_array[] = $row["paymentprice"];
        $sub_array[] = $row["paymentmonth"];
        $sub_array[] = $row["subjectname"];
        // $sub_array[] = $row["subjectprice"];
        $sub_array[] = $row["addtoday"];
        $sub_array[] = $row["employeename"];   
        // $sub_array[] = '<a href="studentsExamsController.php?do=edit&id='. $row["id"] .'&addstudent=1" type="button" class="btn btn-default btn-lm ">+</a>'; 
         if($row["del"] < 2){ 
        $sub_array[] = '<a href="studentsPaymentSystems.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-controll="studentsPaymentSystems" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
        }else{
          $sub_array[] = '<a href="studentsPaymentSystems.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 removecontroller() {
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $id = filter_input(INPUT_POST, 'id');
    $tables = R::load('studentspaymentsystems',$id);
    $tables->del = 2;
    $tables->deltoday = $today;
    $tables->deluserid = $userid;          
    try {
        R::store($tables);
        R::exec("UPDATE `studentspays` SET `del`= 3, `deltoday`= '$today', `deluserid`= $userid  WHERE studentspaymentsystemid = '" . $id . "' ");
    } catch (Exception $e) {
        echo 0 ;
    }
}


?>