<?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("contactusview/show.html");
    $smarty->display("footer.html");
}else if ($do == 'details') {
    $id = filter_input(INPUT_GET, 'id');
    $showData = R::load('contactus', $id);
    $showData->isRead = 1;
    $showData->userreadid = $_SESSION["userid"];
    $showData->dataread = date("Y-m-d");
    R::store($showData);
    $smarty->assign('showData', $showData);
    $user = R::getRow('SELECT user.*  FROM `user` WHERE userid = ?',[$showData->userreadid]); 
    $smarty->assign('user', $user);
    $smarty->display("header.html");
    $smarty->display("contactusview/details.html");
    $smarty->display("footer.html");
}else if ($do == 'getselectjoin') {
  getselectjoin();
}else if ($do == 'showajax') {
  showajax(); 
}else if ($do == 'removecontroller') {
  removecontroller();
}





function getselectjoin() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $tableid = $_POST['tableid'];
    $tablewhere = $_POST['tablewhere'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $join = $_POST['join'];
    $filter = $_POST['filter'];
    $search = '';
    if ($parentid) {
       $search .= " and $table.$parentcol = $parentid "; 
    }
    if ($filter) {
       $search .= $filter;
    }
    $row_array = array();
    $return_arr = array();
    $allData = R::getAll("SELECT $tableid as tabid ,CONCAT($tablesearch) as name FROM $table 
        $join WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search $tablewhere limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['tabid'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}



function showajax() {
    $columns = array('id', 'id', 'clientid', 'name','mobile', 'email', 'subject', 'isRead', 'sysDate', 'userreadid', 'id', 'isdel');

    $start_date = $_POST['start_date'];
    $end_date   = $_POST['end_date'];
    $isdel        = $_POST['isdel'];
    $isRead        = $_POST['isRead'];
    $id  = $_POST['id'];
    $clientid  = $_POST['clientid'];
    $userreadid  = $_POST['userreadid'];
  
    $searchQuery = " ";
    if($id != ''){
       $searchQuery .= " and contactus.id = ".$id. " ";
    }

    if($clientid != ''){
      $searchQuery .=  " and contactus.clientid = ".$clientid. " ";
    }

    if($userreadid != ''){
      $searchQuery .=  " and contactus.userreadid = ".$userreadid. " ";
    }


    if($isdel == 1){
       $searchQuery .= " and contactus.isdel > 0 "; 
    }
    
    if($isRead == 1){
       $searchQuery .= " and contactus.isRead > 0 "; 
    }else{
       $searchQuery .= " and contactus.isRead = 0 "; 
    }

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


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( contactus.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR contactus.clientid LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.name LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.mobile LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.email LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.subject LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.isRead LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.sysDate LIKE "%'.$_POST["search"]["value"].'%"
                    OR contactus.userreadid LIKE "%'.$_POST["search"]["value"].'%"
        )
     ";
    }


    if(isset($_POST["order"]))
    {
    $searchQuery .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  contactus.id DESC ";
    }
    
     $totals = R::count('contactus','WHERE 1 '.$searchQuery.' ');
    
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll('SELECT contactus.* ,user.employeename  FROM `contactus` 
        LEFT JOIN user ON contactus.userreadid = 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) {
        if($row["clientid"] < 1){
           $clientid = 'زائر';
        }else{
           $clientid = 'عميل';
        }

        if($row["isRead"] < 1){
           $isRead = 'غير مقروءة';
        }else{
           $isRead = 'مقروءة';
        }


        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $clientid;
        $sub_array[] = $row["name"];
        $sub_array[] = $row["mobile"];
        $sub_array[] = $row["email"];
        $sub_array[] = $row["subject"];
        $sub_array[] = $isRead;
        $sub_array[] = $row["sysDate"];
        $sub_array[] = $row["employeename"];
        if($row["isdel"] < 1){  
            $sub_array[] = '<a href="contactusController.php?do=details&id='. $row["id"] .'" type="button" class="btn btn-default btn-lm">تفاصيل</a>';
            $sub_array[] = '<a href="javascript:;" data-id="'. $row["id"] .'" data-controll="contactusController" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
        }else{
          $sub_array[] = '';
          $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('contactus',$id);
    $tables->isdel = 1;
    $tables->deltoday = $today;
    $tables->deluserid = $userid;     
    try {
        R::store($tables);
        echo 1 ;
    } catch (Exception $e) {
        echo 0 ;
    }
}




?>