<?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;
        }

   
$i = 0;   
//-----------------------------------------------------------------add-------------------------------------------------------       


if ($_REQUEST['func'] == 'add') {
   
   //print_r($_REQUEST);
	$flag =  $_REQUEST['flag'];
	foreach ($_POST as $key => $value)
    {
      $data[$key] = $value;
    }
    if ($flag == 'add') {
    $query = "INSERT INTO `articles` (`type_work`, `model`, `article`, instance_id) 
    		VALUES ('".$data[type_work]."', '".$data[model]."', '".$data[article]."', $instance_id) ";
       //echo $query;
      // exit;
  } else {
    $query = "UPDATE `articles` SET `type_work` = '".$data[type_work]."', `model` = '".$data[model]."', `article`= '".$data[article]."' WHERE `id_article` = '".$data[id_article]."' LIMIT 1";
  }
    $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));      

}

//-----------------------------------------------------------------show-------------------------------------------------------       
if ($_GET['func'] == 'show') {
	$startLimit = $_REQUEST['start'];
	$limitLimit = $_REQUEST['limit'];
  $i = 0;
    $query = "SELECT `id_article`,`type_work`,`model`,`article` FROM `articles` WHERE instance_id = $instance_id LIMIT $startLimit, $limitLimit";
    // echo $query;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($id, $type_work, $model, $article);
        while ($stmt->fetch()) 
        {
          $Model [$i]= 
            array('id'=>$id,
                  'type_work'=>$type_work,
                  'model'=>$model,
                  'article'=>$article,
                  );            
            $i++;
        } 
        $stmt->close();
    
    }
    $query = "SELECT COUNT(*) FROM `articles` WHERE instance_id = $instance_id";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $stmt->bind_result($total);
    $stmt->fetch();
    $stmt->close();    
    
    $db->close();
     echo "{total: $total,rows:".json_encode($Model)."}";
     
    // print_r($Model);
    // exit;
}



// --------------------------------------------------------------typeWorkStore----------------------------------------------

if ($_GET['func'] == 'typeWorkStore') {
    $query = "SELECT `id_type_job`, `name_job` FROM `type_job` WHERE instance_id = $instance_id ";
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($id, $name_job);
        while ($stmt->fetch()) 
        {
          $Model [$i]= 
            array('id'=>$id,
                  'name_job'=>$name_job,
                  'name_job2'=>$name_job
                  );            
            $i++;
        } 
        $stmt->close();
        $db->close();
    }
     echo'{"rows":'.json_encode($Model).'}';  
  }
// -------------------------------------------------------------modelEquipmentStore----------------------------------------
if ($_GET['func'] == 'modelEquipmentStore') {
    $id_client = $_SESSION['client_id'];
    // echo $id_client;
    $query = "SELECT `equipment`.`id_equipment`,`equipment`.`model` FROM `toir_client_equipment` 
              LEFT JOIN `equipment` ON `equipment`.`id_equipment`=`toir_client_equipment`.`id_equipment`
              WHERE `instanse_id` = ".$id_client;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($id, $model);
        while ($stmt->fetch()) 
        {
          $Model [$i]= 
            array('id'=>$id,
                  'model'=>$model,
                  'model2'=>$model
                  );            
            $i++;
        } 
        $stmt->close();
        $db->close();
    }
     echo'{"rows":'.json_encode($Model).'}';  
  }

//-----------------------------------------------------------------del-------------------------------------------------------       
  if ($_GET['func'] == 'del') {
    $id_article =  $_GET['id_article'];
    $query = "DELETE FROM `articles` WHERE `id_article` = ".$id_article;
    // echo $query; 
    // exit;
    $stmt = $db->prepare($query);
           
           $time = date("j-M-Y G:i : s");
            $ip = getenv('REMOTE_ADDR');
            $referrer = getenv('HTTP_REFERER');
            $quer = $query;  
            $mes = $id_equipment;
            $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Удалено оборудование : ".$mes." || SEARCHSTRING  ".$quer;
            // writeToLogFile($msg);
           
           $stmt->execute();
           $db->close(); 
  }

