<?php  
  session_start();
    header('Content-Type: text/html; charset=utf-8');
    $instance_id = $_SESSION['client_id'];
        //require "log.php";
        //include "../settings.php";
		
		$dbset = json_decode($_SESSION['settingsApp'],true);
		
		$db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);
        //$db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
        $db->query("SET CHARSET SET utf8");
        $db->query("SET NAMES 'utf8'");
        if (mysqli_connect_errno()) {
            echo "Error connecting to DB";
            exit;
        }
//-----------------------------------------------------------------show-------------------------------------------------------------    
    
    if ($_GET['func']=='show')
    {
        $i=0;
        $query = "SELECT id, name, unit FROM toir_ingredients WHERE instance_id = $instance_id  LIMIT 100";
	    if ($stmt = $db->prepare($query))
	    {
            $stmt->execute();
            $stmt->bind_result($id, $name, $unit);
            while ($stmt->fetch()) 
            {
              $Model [$i]= 
                array('id'=>$id,
                      'name'=>$name,
                      'unit'=>$unit
                      );            
                $i++;
            } 
            $stmt->close();
            $db->close();
	    }
   		echo'{rows:'.json_encode($Model).'}';   
    }

        
//-----------------------------------------------------------------add-------------------------------------------------------        

if ($_GET['func']=='add')
{  
  foreach ($_POST as $key => $value)
  {
    $data[$key] = $value;
  }
   $query = "INSERT INTO toir_ingredients (name, unit, instance_id) VALUES ('".$data[name]."', '".$data[unit]."', $instance_id)";
   $sql = $db->real_query($query);
   
    $time = date("j-M-Y G:i : s");
    $ip = getenv('REMOTE_ADDR');
    $referrer = getenv('HTTP_REFERER');
    $quer = $query;  
    $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Добавление нового ингредиента   || SEARCHSTRING  ".$quer;
    // writeToLogFile($msg);
   
   $db->close();
   echo json_encode(array('success'=>true,'content'=>$_POST));      
}
        
        
//-----------------------------------------------------------------delete-------------------------------------------------------------    
                
    if ($_GET['func']=='del')
        { 
           $id = $_GET['id'];
           $query = "DELETE FROM toir_ingredients WHERE id=".$id." LIMIT 1";
           $stmt = $db->prepare($query);
           $stmt->execute();
           
            $time = date("j-M-Y G:i : s");
            $ip = getenv('REMOTE_ADDR');
            $referrer = getenv('HTTP_REFERER');
            $quer = $query;  
            $mes = $id_type_job;
            $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Удаление ингредиента : ".$mes." || SEARCHSTRING  ".$quer;
            // writeToLogFile($msg);
           
           $db->close(); 
        }
        

//-----------------------------------------------------------------update-------------------------------------------------------------    
                    
    if ($_GET['func']=='update')
        {  
          foreach ($_POST as $key => $value)
          {
            $data[$key] = $value;
          }
          $data['id'] += 0;
           $query = "UPDATE toir_ingredients SET name='".$data[name]."', unit='".$data[unit]."'  WHERE id=".$data[id]."";
           $stmt = $db->prepare($query);
           $stmt->execute();
           
            $time = date("j-M-Y G:i : s");
            $ip = getenv('REMOTE_ADDR');
            $referrer = getenv('HTTP_REFERER');
            $quer = $query;  
            $mes = $data['id_type_job'];
            $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Изменение ингредиента : ".$mes." || SEARCHSTRING  ".$quer;
            // writeToLogFile($msg);
           
           $stmt->close();
           echo json_encode(array('success'=>true,'content'=>$_POST));
        }
        
        

?>