<?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;
        }
// ======================================================
if ($_GET['func']=='show') {
$i=0;
$query = "SELECT id_evaluation_criteria,name_evaluation_criteria FROM evaluation_criteria WHERE instance_id =$instance_id  LIMIT 1000";
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($id_evaluation_criteria, $name_evaluation_criteria);
        while ($stmt->fetch()) 
        {
          $Model [$i]= 
            array('id_evaluation_criteria'=>$id_evaluation_criteria,
                  'name_evaluation_criteria'=>$name_evaluation_criteria
                  );            
            $i++;
        } 
        $stmt->close();
        $db->close();
    }
    echo'{rows:'.json_encode($Model).'}';   
}
// ---------------------------------------------------------
if ($_GET['func']=='add') { 
  foreach ($_POST as $key => $value)
  {
    $data[$key] = $value;
  }
   $query = "INSERT INTO evaluation_criteria (name_evaluation_criteria, instance_id) VALUES ('".$data[name_evaluation_criteria]."', $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));      
}
// ---------------------------------------------------------
if ($_GET['func']=='del') { 
   $id_evaluation_criteria = $_GET['id_evaluation_criteria'];
   $query = "DELETE FROM evaluation_criteria WHERE id_evaluation_criteria=".$id_evaluation_criteria." 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_evaluation_criteria;
    $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Удаление оценочного критерия : ".$mes." || SEARCHSTRING  ".$quer;
    // writeToLogFile($msg);
   
   $db->close(); 
}
// ------------------------------------------------------
if ($_GET['func']=='update') {  
  foreach ($_POST as $key => $value)
  {
    $data[$key] = $value;
  }
  $data['id_evaluation_criteria'] += 0;
   $query = "UPDATE evaluation_criteria SET name_evaluation_criteria='".$data[name_evaluation_criteria]."' WHERE id_evaluation_criteria=".$data[id_evaluation_criteria]."";
   $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_evaluation_criteria'];
    $msg = "IP: ".$ip." || TIME: ".$time." || REFERRER: ".$referrer." || DATA: Изменение оценочного критерия : ".$mes." || SEARCHSTRING  ".$quer;
    // writeToLogFile($msg);
   
   $stmt->close();
   echo json_encode(array('success'=>true,'content'=>$_POST));
} 
        