<?php
include("../public/impOpreation.php");
include("../library/uploadImages.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');

$youtubeLink = new YoutubeLink();
$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("maintenanceclientview/add.html");
    $smarty->assign("maintenance", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $smarty->display("header.html");
    $smarty->display("maintenanceclientview/show.html");
    $smarty->assign("maintenance", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $del = filter_input(INPUT_GET, 'del');
    $clientdata = R::getRow('SELECT client.* FROM `client` WHERE clientid = ?',[$id]);
    $smarty->assign('clientdata', $clientdata);
    $smarty->assign('del', $del);
    $smarty->display("header.html");
    $smarty->display("maintenanceclientview/edit.html");
    $smarty->assign("maintenance", 1);
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
  savedata();
}else if ($do == 'showajax') {
  showajax();
}


function savedata() {
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $branchid = $_SESSION['branchId'];
    $clientname = filter_input(INPUT_POST, 'clientname');
    $clientphone = filter_input(INPUT_POST, 'clientphone');
    $clientmobile = filter_input(INPUT_POST, 'clientmobile');
    $clientaddress = filter_input(INPUT_POST, 'clientaddress');
    $clientid = filter_input(INPUT_POST, 'clientid');
    $selectitr  = filter_input(INPUT_POST, 'selectitr');
    try {
        if (!$clientid) {
         R::exec("INSERT INTO `client`(`clientname`, `clientaddress`, `clientphone`, `clientmobile`, `clientdate`, `userid`, `branchId`) VALUES 
         ('$clientname', '$clientaddress', '$clientphone', '$clientmobile','$date', $userid, $branchid)");
        $last =R::getROW("SELECT clientid, clientname FROM `client` order by clientid desc");
        $clientid = $last['clientid'];
        $text = $last['clientname'];
        }else{
           R::exec("UPDATE `client` SET `clientname`='$clientname',`clientaddress`='$clientaddress',`clientphone`='$clientphone',`clientmobile`='$clientmobile',`branchId`= $branchid WHERE clientid = $clientid");
           $text = $clientname;
        }
        $selectiditr = 'clientid'.$selectitr;
        echo json_encode(array('id' => $clientid,'selectname' => 'clientid','selectid' => $selectiditr,'text' => $text
            ,'selectid' => $selectiditr,'selectitr' => $selectitr));
    } catch (Exception $e) {
        echo 0 ;
    }
}


function showajax() {
    $columns = array('clientid', 'clientname', 'clientphone', 'clientmobile', 'clientdate', 'branchName', 'employeename', 'clientid', 'clientid');
    $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 client.clientid = ".$data1. " ";
    }
    if($del == ''){
       $searchQuery .= " and client.conditions = 0 "; 
    }

    if($data3 != ''){
       $searchQuery .= " and client.branchId = ".$data3. " ";
    }
    
    if($start_date != '' && $end_date != ''){
       $searchQuery .=' and  client.clientdate  >= "' . $start_date . ' 00-00-00" and client.clientdate <= "' . $end_date . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( client.clientid LIKE "%'.$_POST["search"]["value"].'%" 
                    OR client.clientname LIKE "%'.$_POST["search"]["value"].'%"
                    OR client.clientmobile LIKE "%'.$_POST["search"]["value"].'%"
                    OR client.clientphone LIKE "%'.$_POST["search"]["value"].'%"
                    OR client.clientdate LIKE "%'.$_POST["search"]["value"].'%"
                    OR branchName 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  client.clientid DESC ";
    }
    
     $totals = R::count('client','LEFT JOIN user ON client.userid = user.userid 
        LEFT JOIN branch ON client.branchId = branch.branchId  WHERE 1 '.$searchQuery.' ');
    
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll('SELECT client.* ,employeename ,branchName  FROM `client` 
        LEFT JOIN user ON client.userid = user.userid 
        LEFT JOIN branch ON client.branchId = branch.branchId  
        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["clientid"];
        $sub_array[] = $row["clientname"];
        $sub_array[] = $row["clientphone"];
        $sub_array[] = $row["clientmobile"];
        $sub_array[] = $row["clientdate"];
        $sub_array[] = $row["branchName"];
        $sub_array[] = $row['employeename'] ;  
        if($row["conditions"] == 0){  
        $sub_array[] = '<a href="maintenanceclients.php?do=edit&id='. $row["clientid"] .'&del=0" type="button" class="btn btn-default btn-lg editicon"></a>';
         $sub_array[] = '<a href="javascript:;" data-id="'. $row["clientid"] .'" data-table="client" data-tableid="clientid" type="button" class="btn btn-default btn-lg deleteicon removeteble"  ></a>';
        }else{
          $sub_array[] = '<a href="maintenanceclients.php?do=edit&id='. $row["clientid"] .'&del=1" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
          $sub_array[] = 'محذوف ';
        }
       
        $output['data'][] = $sub_array;
       }
    
       echo json_encode($output, JSON_UNESCAPED_SLASHES);

}







?>