<?php
include("../public/impOpreation.php");
$do = $_GET['do'];

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

if (empty($do)) { 
     $trainerssetting = R::load('trainerssetting',1);
    $smarty->assign("trainerssetting",$trainerssetting);
    $smarty->display("header.html"); 
    $smarty->display("trainerspaymentsystemsview/add.html");
    $smarty->assign("trainers", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $trainerssetting = R::load('trainerssetting',1);
    $smarty->assign("trainerssetting",$trainerssetting);
    $smarty->assign('date', $date);
    $smarty->display("header.html"); 
    $smarty->display("trainerspaymentsystemsview/show.html");
    $smarty->assign("trainers", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $trainerspaymentsystem = R::load('trainerspaymentsystems', $id);
    // $studentsubjects = R::findOne('studentsubjects','id = ? and del < 2',[$trainerspaymentsystem->studentsubjectid]);
    //  $trainerspaymentsystem->studentsubjects = $studentsubjects->subjectname;
    $smarty->assign('trainerspaymentsystem', $trainerspaymentsystem);
    
     $trainerssetting = R::load('trainerssetting',1);
    $smarty->assign("trainerssetting",$trainerssetting);
    
    $smarty->display("header.html"); 
    $smarty->display("trainerspaymentsystemsview/edit.html");
    $smarty->assign("trainers", 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');
    $numbercatarrh = filter_input(INPUT_POST, 'numbercatarrh');
    // $studentaddsubjectid = filter_input(INPUT_POST, 'studentaddsubjectid');
    $trainerspaymentsystemid = filter_input(INPUT_POST, 'trainerspaymentsystemid');

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


function showajax() {
    $columns = array('id', 'paymentname', 'paymentprice', 'numbercatarrh','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 trainerspaymentsystems.studentaddsubjectid = ".$data1. " ";
    // }

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

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

    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( trainerspaymentsystems.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR trainerspaymentsystems.paymentname LIKE "%'.$_POST["search"]["value"].'%"
                    OR trainerspaymentsystems.paymentprice LIKE "%'.$_POST["search"]["value"].'%"
                    OR trainerspaymentsystems.numbercatarrh LIKE "%'.$_POST["search"]["value"].'%"
                    OR trainerspaymentsystems.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  trainerspaymentsystems.id DESC ";
    }
          $totals = R::count('trainerspaymentsystems','LEFT JOIN user ON trainerspaymentsystems.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, numbercatarrh, trainerspaymentsystems.id as id, trainerspaymentsystems.addtoday as addtoday,
     trainerspaymentsystems.del as del, employeename  FROM `trainerspaymentsystems` 
  LEFT JOIN user ON trainerspaymentsystems.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["id"];
        $sub_array[] = $row["paymentname"];
        $sub_array[] = $row["paymentprice"];
        $sub_array[] = $row["numbercatarrh"];
        // $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="trainersPaymentSystems.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="trainersPaymentSystems" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
        }else{
          $sub_array[] = '<a href="trainersPaymentSystems.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('trainerspaymentsystems',$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 trainerspaymentsystemid = '" . $id . "' ");
    } catch (Exception $e) {
        echo 0 ;
    }
}


?>