<?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");
$smarty->assign('username', $_SESSION['username']);
if (empty($do)) {  
    $smarty->assign('date', $date);
    $smarty->display("header.html");
    $smarty->display("maintenancedeliveryview/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("maintenancedeliveryview/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');
    $maintenancedata = R::load('maintenances',$id);
    $maintenancepolicydata = R::load('maintenancepolicys',$maintenancedata->maintenancepolicyid);
    $smarty->assign('maintenancepolicydata', $maintenancepolicydata);
    $smarty->assign('maintenancedata', $maintenancedata);
    $smarty->assign('del', $del);
    $smarty->display("header.html");
    $smarty->display("maintenancedeliveryview/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'];
    $repayment = filter_input(INPUT_POST, 'repayment');
    $clipnumber = filter_input(INPUT_POST, 'clipnumber');
    $deliverydate = filter_input(INPUT_POST, 'deliverydate');
    $prepaid = filter_input(INPUT_POST, 'prepaid');
    $totalcost = filter_input(INPUT_POST, 'totalcost');
    $requiredcustomer = filter_input(INPUT_POST, 'requiredcustomer');
    $maintenancecost = filter_input(INPUT_POST, 'maintenancecost');
    $paidlater  = filter_input(INPUT_POST, 'paidlater');
    $paidall  = filter_input(INPUT_POST, 'paidall');
    $residual  = filter_input(INPUT_POST, 'residual');
    $bshipcharge = filter_input(INPUT_POST, 'bshipcharge');
    $shipcharge = filter_input(INPUT_POST, 'shipcharge');
    $selectitr  = filter_input(INPUT_POST, 'selectitr');
    $maintenanceid  = filter_input(INPUT_POST, 'maintenances');
    try {
            $maintenances = R::load('maintenances',$maintenanceid);
            $maintenances->repayment = $repayment;
            $maintenances->clipnumber = $clipnumber;
            $maintenances->deliverydate = $deliverydate;          
            $maintenances->prepaid = $prepaid;
            $maintenances->totalcost = $totalcost;          
            $maintenances->requiredcustomer = $requiredcustomer;
            $maintenances->maintenancecost = $maintenancecost;
            $maintenances->paidlater = $paidlater;
            $maintenances->paidall = $paidall;
            $maintenances->residual = $residual;
            $maintenances->bshipcharge = $bshipcharge; 
            $maintenances->shipcharge = $shipcharge;    
            $maintenances->maintenancecaseid = 4;   
            $maintenances->sendorreceipt = 3;      
            $maintenanceid = R::store($maintenances);

        $selectiditr = 'maintenanceid'.$selectitr;
        echo json_encode(array('id' => $maintenanceid,'selectname' => 'maintenanceid','selectid' => $selectiditr,'text' => $text,'selectid' => $selectiditr,'selectitr' => $selectitr));
    } catch (Exception $e) {
        echo 0 ;
    }
}


function showajax() {
    $columns = array('id', 'clientname', 'productName', 'suppliername', 'branchName', 'invoicedate', 'maintenancereceiptdata', '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'];
    $data4      = $_POST['data4'];
    $data5      = $_POST['data5'];

    $searchQuery = " ";
    if($data1 != ''){
      $searchQuery .=  " and maintenances.supplierid = ".$data1. " ";
    }

    if($data2 != ''){
      $searchQuery .=  " and maintenances.clientid = ".$data2. " ";
    }
    if($del == ''){
       $searchQuery .= " and maintenances.conditions = 0 "; 
    }

    if($data3 != ''){
       $searchQuery .= " and maintenances.branchid = ".$data3. " ";
    }

    if($data4 != '' && $data4 > 0){
       $searchQuery .= " and maintenances.maintorguar = ".$data4. " ";
    }

    if($data5 != ''){
       $searchQuery .= " and maintenances.maintenancecaseid = ".$data5. " ";
    }
    
    if($start_date != '' && $end_date != ''){
       $searchQuery .=' and  maintenances.maintenancedate  >= "' . $start_date . ' 00-00-00" and maintenances.maintenancedate <= "' . $end_date . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( maintenances.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR clientname LIKE "%'.$_POST["search"]["value"].'%"
                    OR productName LIKE "%'.$_POST["search"]["value"].'%"
                    OR suppliername LIKE "%'.$_POST["search"]["value"].'%"
                    OR maintenances.supplierdate 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  maintenances.id DESC ";
    }
    
     $totals = R::count('maintenances','LEFT JOIN user ON maintenances.userid = user.userid 
        LEFT JOIN client ON maintenances.clientid = client.clientid
        LEFT JOIN product ON maintenances.productid = product.productId
        LEFT JOIN supplier ON maintenances.supplierid = supplier.supplierid
        LEFT JOIN branch ON maintenances.branchid = branch.branchId  
          WHERE  maintenances.maintenancecaseid = 4  '.$searchQuery.' ');
    
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll('SELECT maintenances.* ,employeename ,clientname ,productName ,suppliername ,branchName, centername, maintenancecenterid FROM `maintenances` 
        LEFT JOIN user ON maintenances.userid = user.userid 
        LEFT JOIN client ON maintenances.clientid = client.clientid 
        LEFT JOIN product ON maintenances.productid = product.productId 
        LEFT JOIN supplier ON maintenances.supplierid = supplier.supplierid 
        LEFT JOIN branch ON maintenances.branchid = branch.branchId  
        LEFT JOIN maintenancecenters ON maintenances.maintenancecenterid = maintenancecenters.id  
        WHERE  maintenances.maintenancecaseid = 4 '.$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[] = '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
                            <input type="checkbox" class="checkboxes" value="' . $row["id"] . '" />
                            <span></span>
                        </label>';
        $sub_array[] = $row["clientname"];
        $sub_array[] = $row["productName"];
        $sub_array[] = $row["suppliername"];
        $sub_array[] = $row["branchName"];
        $sub_array[] = $row["invoicedate"];
        $sub_array[] = $row["maintenancedate"];
        $sub_array[] = $row['employeename']; 
       
        if($row["conditions"] == 0){  
        $sub_array[] = '<a href="maintenancedeliverys.php?do=edit&id='. $row["id"] .'&del=0" type="button" class="btn btn-default btn-lg editicon"></a>';
         $sub_array[] = '<a href="javascript:;" data-id="'. $row["id"] .'" data-tableid="id" data-table="maintenances" type="button" class="btn btn-default btn-lg deleteicon removeteble"  ></a>';
        }else{
          $sub_array[] = '<a href="maintenancedeliverys.php?do=edit&id='. $row["id"] .'&del=1" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
          $sub_array[] = 'محذوف ';
        }
       
        $output['data'][] = $sub_array;
       }
    
       echo json_encode($output, JSON_UNESCAPED_SLASHES);

}



?>