<?php
include_once("../public/con_reedbean.php");

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
// $userid = $_SESSION['userid'];
if ($do == 'checkcolumn') {
  checkcolumn();
}else if ($do == 'getselectjoin') {
getselectjoin();
}else if ($do == 'removeteble') {
  removeteble();
}else if ($do == 'getselect') {
  getselect();
}else if ($do == 'getselectparent') {
getselectparent();
}else if ($do == 'getselectmultiple') {
  getselectmultiple();
}else if ($do == 'receivedata') {
  receivedata();
}else if ($do == 'receivestudent') {
  receivestudent();
}else if ($do == 'payed') {
   $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];
    $branchiduser = $_SESSION['branchId'];
    $studentid = filter_input(INPUT_GET, 'studentid');
    $deviceid = filter_input(INPUT_GET, 'deviceid');
    $uniqid = time();
    if($studentid && !is_numeric($studentid)){
        $smarty->assign('notstudent', 3);   
    }else if($studentid && is_numeric($studentid)){
      
    $studentabsencescount = R::count('studentabsences', "studentid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$studentid, $deviceid]);
     $students = R::findOne('students','id = ? and del < 2 ',[$studentid]);
     
     $smarty->assign('students', $students); 
     if ($students->branchid || $branchstudent == 0) {
           $branchstudent = R::getRow("SELECT * FROM `branch` WHERE branchId = $students->branchid ");
           if ($students->branchid == $branchiduser || $branchiduser == 0) {
               $smarty->assign('statusbranch', 1);
           }else{
               $branchuser = R::getRow("SELECT * FROM `branch` WHERE branchId = $branchiduser ");
               $smarty->assign('statusbranch', 2);
               $smarty->assign('branchuser', $branchuser);
           }
    }
    $smarty->assign('branchstudent', $branchstudent);
    $studentsubjects = R::findOne('studentsubjects','deviceid = ? ',[$deviceid]);
    $studentspays = R::findOne('studentspays',"studentid = ? and studentsubjectid = ? and del < 2 and payenddate >= '$date' order by id desc",[$studentid, $studentsubjects->id]);
    if ($studentspays->id < 1) {
            $smarty->assign('status', 2);
    }
    if($studentabsencescount == 0 && $studentspays->id > 0 ){ 
           $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and studentsubjectid = ? and del < 2  order by id desc',[$studentid, $studentsubjects->id]);
            $studentabsences = R::dispense('studentabsences');
            $studentabsences->onlys = 0;
            $studentabsences->del = 0;
            $studentabsences->arrivedtime = 0;
            $studentabsences->addtoday = $today;          
            $studentabsences->adduserid = $_SESSION['userid'];
            $studentabsences->uniqid = time();
            $studentabsences->studentid = $studentid;
            $studentabsences->todaysend = $date;
            $studentabsences->timesend = $times;
            $studentabsences->type = 1;
            $studentabsences->deviceid = $deviceid;
            $studentabsences->status = 1;
            $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
            $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
            $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
            $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
            R::store($studentabsences); 
            $smarty->assign('status', 1);
    }
     $smarty->assign('notstudent', 1);   
    }else{
      $smarty->assign('notstudent', 2);   
    }

 
    $smarty->display("studentview/notpayed.html"); 
}else if ($do == 'getsetting') {
  getsetting();
}else if ($do == 'changeenddate') {
  changeenddate();
}
function checkcolumn() {
    $table = filter_input(INPUT_POST, 'table');
    $tablecol = filter_input(INPUT_POST, 'tablecol');
    $tablecolval = filter_input(INPUT_POST, 'tablecolval');
    $tableid = filter_input(INPUT_POST, 'tableid');
    $parentid = filter_input(INPUT_POST, 'parentid');
    $parentcol = filter_input(INPUT_POST, 'parentcol');

    if (!$tableid && !$parentid) {
    $checkinput = R::count($table, " $tablecol = ? and del < 2 ", [$tablecolval]);
    }else if ($tableid && !$parentid) {
    $checkinput = R::count($table, " $tablecol = ? and id != ? and del < 2 ", [$tablecolval, $tableid]); 
    }else if (!$tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and del < 2 ", [$tablecolval, $parentid]);
    }else if ($tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and id != ? and del < 2", [$tablecolval, $parentid, $tableid]);
    }  
    if ($checkinput > 0) {
         echo 1;
    } else {
         echo 0;
    }
       
}





function getselectjoin() {
    $searchterm = filter_input(INPUT_POST, 'searchTerms');
    $tablesearch = filter_input(INPUT_POST, 'tablesearch');
    $table = filter_input(INPUT_POST, 'table');
    $parentcol = filter_input(INPUT_POST, 'parentcol');
    $parentid = filter_input(INPUT_POST, 'parentid');
    $join = filter_input(INPUT_POST, 'join');
    $filter = filter_input(INPUT_POST, 'filter');
    $search = '';

    if ($parentid) {
       $search .= " and $table.$parentcol =  $parentid "; 
    }

    if ($filter) {
       $search .= $filter;
    }

 
    $return_arr = array();
    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name FROM $table 
        $join WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
    foreach ($allData as $data) {
           $row_array = array();
        $row_array['id'] = $data['id'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);

}



function removeteble() {
    $id = filter_input(INPUT_POST, 'id');
    $table = filter_input(INPUT_POST, 'table');
    $tables = R::load($table,$id);
    $tables->del = 2;
    $tables->deltoday = date("Y-m-d H:i:s");     
    $tables->deluserid = $_SESSION['userid']; 
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}












function getselect() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $filter = $_POST['filter'];
    $parentid = $_POST['parentid'];
    $parentcol = $_POST['parentcol'];

    $search = '';
    if ($parentid) {
    $search .= " and $table.del < 2"; 
    }
    if ($parentid) {
       $search .= " and $parentcol = $parentid "; 
    }

    if ($filter) {
       $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

     $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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);
}


function getselectmultiple() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $studentsubjectid = $_POST['studentsubjectid'];
    
    $row_array = array();
    $return_arr = array();
    $search = '';
    if ($del) {
       $search .= " and $table.del < 2"; 
    }
    $studentid = '0';
    $studentstosubjects = R::findAll('studentaddsubjectdetails', 'studentsubjectid = ? and del < 2',[$studentsubjectid]);
    foreach($studentstosubjects as $data){
     $studentid .= ','.$data->studentid;
    }
    $search .= " and $table.id not in ($studentid) ";
    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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);
}






function getselectparent() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $filter = $_POST['filter'];
    $search = '';
    
    $search .= " and $table.del < 2"; 
    if ($parentid) {
       $search .= " and $parentcol =  $parentid "; 
    }

    if ($filter) {
       $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

     $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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);
}







function receivedata() {
    $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];    
    $lists = explode("\n", $_POST['textfile']);
    foreach ($lists as $list) {
        $data = explode('?', $list);
        $studentabsencescount = R::count('studentabsences', "studentid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$data[1], $data[5]]);
        if($studentabsencescount == 0 && $list){ 
            $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and deviceid = ? and del < 2 order by id desc',[$data[1], $data[5]]); 
               $studentabsences = R::dispense('studentabsences');
                $studentabsences->onlys = 0;
                $studentabsences->del = 0;
                $studentabsences->arrivedtime = $arrivedtime;
                $studentabsences->addtoday = $today;          
                $studentabsences->adduserid = $_SESSION['userid'];
                $studentabsences->uniqid = $data[0];
                $studentabsences->studentid = $data[1];
                $studentabsences->todaysend = $data[2];
                $studentabsences->timesend = $data[3];
                $studentabsences->type = $data[4];
                $studentabsences->deviceid = $data[5];
                $studentabsences->status = $data[6];
                $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
                $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
                $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
                $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
                R::store($studentabsences);
        }
    }
    
}






function receivestudent() {
    $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];
   $brancheid = filter_input(INPUT_GET, 'brancheid');
   $studentid = filter_input(INPUT_GET, 'studentid');
  $studentabsencescount = R::count('studentabsences', "uniqid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$data[0], $data[5]]);
    if($studentabsencescount == 0 && $studentid && $brancheid){ 
        $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and deviceid = ? and del < 2 order by id desc',[$data[1], $data[5]]); 
     
   
   $studentabsences = R::dispense('studentabsences');
    $studentabsences->onlys = 0;
    $studentabsences->del = 0;
    $studentabsences->arrivedtime = $arrivedtime;
    $studentabsences->addtoday = $today;          
    $studentabsences->adduserid = $_SESSION['userid'];
    $studentabsences->uniqid = $data[0];
    $studentabsences->studentid = $data[1];
    $studentabsences->todaysend = $data[2];
    $studentabsences->timesend = $data[3];
    $studentabsences->type = $data[4];
    $studentabsences->deviceid = $data[5];
    $studentabsences->status = $data[6];
    $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
    $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
    $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
    $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
    R::store($studentabsences);
    }
}

function getsetting() {
    $getdata = R::getRow("SELECT * FROM programsettings");
    echo json_encode($getdata);
}




function changeenddate() {
    $id = filter_input(INPUT_POST, 'payid');
    $dateend = filter_input(INPUT_POST, 'dateend');
    $datestart = filter_input(INPUT_POST, 'datestart');
    $tables = R::load(studentspays,$id);
    $tables->paystartdate = $datestart;
    $tables->payenddate = $dateend;
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


?>