<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

    class Table extends CI_Controller {

        function __construct() {

            parent::__construct();
            
        }

        
        public function list() {
            
             $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);
    
    if($jsonObj->floorid!="")
    {
        $sql = "SELECT PLACES.*, SHAREDTICKETS.NAME AS ORDERID FROM PLACES "
                . "LEFT JOIN SHAREDTICKETS ON PLACES.ID=SHAREDTICKETS.ID AND SHAREDTICKETS.BMODE=2"
                . " WHERE PLACES.FLOOR LIKE '".$jsonObj->floorid."' "
                . " ORDER BY PLACES.FLOOR, PLACES.NAME ";
    }
    else
    {
        $sql = "SELECT * FROM PLACES ORDER BY FLOOR, NAME";
    }
            
            
    $arr = $this->db->query($sql)->result();
    
    $data=array();
    $i=0;
    if ($arr != '') {
                foreach ($arr as $key => $aRow) {
                    $i++;
                    array_push($data,
                    array(
                        //'id' => $i, 
                        'id' => $aRow->ID, 
                     'name' => $aRow->NAME, 
                        'floor' => $aRow->FLOOR, 
                     'capacity' => "10", 
                     'status' => 0,
                        'orderid' => $aRow->ORDERID,
                    'restaurant_id' => 1
                    ));
                }
    }
    
            

   //add the header here
   header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
            
            
        }
        
        public function floor() {
            
            
            $sql = "SELECT * FROM FLOORS ORDER BY NAME";
    $arr = $this->db->query($sql)->result();
    
    $data=array();
    $i=0;
    if ($arr != '') {
                foreach ($arr as $key => $aRow) {
                    $i++;
                    array_push($data,
                    array(
                        'id' => $aRow->ID, 
                     'name' => $aRow->NAME
                    ));
                }
    }
    
            

   //add the header here
   header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
            
            
        }
        
        public function locktable() {
            
             $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);
    
    if($jsonObj->tableid!="" && $jsonObj->waitername!="")
    {
        $sql = "SELECT * FROM SHAREDTICKETS WHERE ID LIKE '".$jsonObj->tableid."' AND BMODE=2 ";
        
             
    $arr = $this->db->query($sql)->result();

    if (sizeof($arr)!=0) {
        
        if($arr[0]->LOCKED!=$jsonObj->waitername)
        {
            if($arr[0]->CONTENT!=NULL)
            {
                   $data= array(
        'status' => "0",
        'message' => "Order not taken from app."
                );
            }
 else {
        $data= array(
        'status' => "0",
        'message' => "Table already opened by ".$arr[0]->LOCKED
                );
 }
     
        
    }
        else
        {
            $data= array(
        'status' => "1",
        'message' => "Table already locked."
                );
        }
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
        
    }
 else {
     
     $nowtime = time();
     
     $data = array(
        
        'ID'=> $jsonObj->tableid,
        'NAME'=>'('.date('H:i ', time()).' '. $nowtime%1000 .')',
        'BMODE'=>2,
        'LOCKED'=>$jsonObj->waitername,
        
    );

    $this->db->insert('SHAREDTICKETS',$data);
        
     $data= array(
        'status' => "1",
        'message' => "Table locked.",
                    );
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
    }
    
            

  
            
    }
    else
    {
         $data= array(
        'status' => "0",
        'message' => "Parameters missing.",
                    );
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
                
    }
            
       
            
        }
        
        
        
        public function cleartable() {
            
             $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);
    
    if($jsonObj->tableid!="" && $jsonObj->waitername!="")
    {
        $sql = "DELETE FROM SHAREDTICKETS WHERE ID LIKE '".$jsonObj->tableid."' AND CONTENT IS NULL AND BMODE=2 AND LOCKED LIKE '".$jsonObj->waitername."'";
        
             
    $arr = $this->db->query($sql);

    if($arr!=null)
    {
      $data= array(
        'status' => "1",
        'message' => $sql,
                    );
    
            
    }
    else
    {
        $data= array(
        'status' => "0",
        'message' => "Items clearing failed.",
                    );
    }
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
    }
    else
    {
         $data= array(
        'status' => "0",
        'message' => "Parameters missing.",
                    );
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
                
    }
            
       
            
        }

    }
