<?php
include("../public/impOpreation.php");
include("../library/uploadImages.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("projectstagesview/add.html");
    $smarty->assign("project", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $smarty->assign('date', $date);
    $smarty->display("header.html");
    $smarty->display("projectstagesview/show.html");
    $smarty->assign("project", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $webclientdata = R::load('projectstages', $id);
    $smarty->assign('webclientdata', $webclientdata);
    $smarty->display("header.html");
    $smarty->display("projectstagesview/edit.html");
    $smarty->assign("project", 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'];
    $name = filter_input(INPUT_POST, 'name');
    $is_active = filter_input(INPUT_POST, 'is_active');
    $projectstageidold = filter_input(INPUT_POST, 'projectstageid');
    // print_r($webclientidold);
    // exit();

    if (!$projectstageidold) {;
        $projectclients = R::dispense('projectstages');
        $projectclients->del = 0;
        $projectclients->addtoday = $today;          
        $projectclients->adduserid = $userid;
        $projectclients->deltoday = '';      
        $projectclients->deluserid = '';
    }else{
        $projectclients = R::load('projectstages',$projectstageidold);
        $projectclients->del = 1;
        $projectclients->updatetoday = $today;          
        $projectclients->updateuserid = $userid; 
    }
    $projectclients->name  = $name;
    $projectclients->is_active = $is_active;
    try {
         $webclientid = R::store($projectclients);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


function showajax() {
    $columns = array('projectstages.id','name', '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 = " ";
    $joinQuery = " ";
    if($data1 != ''){
      $searchQuery .=  " and projectstages.id = ".$data1. " ";
    }
    if($del == ''){
       $searchQuery .= " and projectstages.del < 2 "; 
    }

    if($data3 != ''){
       $joinQuery .= " LEFT JOIN webclientprojects ON webclients.id = webclientprojects.webclientid ";
       $searchQuery .= " and webclientprojects.clientid = ".$data3. " ";
    }
    
    if($start_date != '' && $end_date != ''){
       $searchQuery .='and  projectstages.addtoday  >= "' . $start_date . ' 00-00-00" and projectstages.addtoday <= "' . $end_date . ' 23-59-55" ';
    }else{
        $today = date("Y-m-d");
        $searchQuery .='and  projectstages.addtoday  >= "' . $today . ' 00-00-00" and projectstages.addtoday <= "' . $today . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( projectstages.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR projectstages.name LIKE "%'.$_POST["search"]["value"].'%"
                    OR projectstages.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  projectstages.id DESC ";
    }


     $totals = R::count('projectstages',"LEFT JOIN user ON projectstages.adduserid = user.userid $joinQuery  WHERE 1 $searchQuery ");
    
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll("SELECT projectstages.* ,employeename   FROM `projectstages` LEFT JOIN user
         ON projectstages.adduserid = user.userid $joinQuery  WHERE 1 $searchQuery ");   
    $apps = count($rResult);
    
    
     
    $output = array(  
        "draw" => intval($_POST["draw"]),
        "recordsTotal" => $totals,
        "recordsFiltered" =>intval($apps),
        "data" => array()
    );
  
       foreach ($rResult as $row) {
        $date = date('Y-m-d');
         

        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["name"];
        $sub_array[] = $row["addtoday"];
        $sub_array[] = $row['employeename'] ;  
        if($row["del"] < 2){  
        $sub_array[] = '<a href="projectstagesController.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="studentController" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
        }else{
          $sub_array[] = '<a href="projectstagesController.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('projectstages',$id);
    $tables->del = 2;
    $tables->deltoday = $today;
    $tables->deluserid = $userid;     
    try {
        R::store($tables);
        // R::exec("UPDATE `client` SET `conditions`= 1 WHERE studentid = '" . $id . "' ");
        // R::exec("UPDATE `studentaddsubjectdetails` SET `del`= 3, `deltoday`= '$today', `deluserid`= $userid WHERE studentid = '" . $id . "' ");
        // R::exec("UPDATE `studentsexamsval` SET `del`= 3, `deltoday`= '$today', `deluserid`= '$userid' WHERE studentid = '" . $id . "' ");
        // R::exec("UPDATE `studentspays` SET `del`= 3, `deltoday`= '$today', `deluserid`= '$userid' WHERE studentid = '" . $id . "' ");
        echo 1 ;
    } catch (Exception $e) {
        echo 0 ;
    }
}





?>