<?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 ($_GET['func'] == 'add') {
    $from_message = $_POST['from_message'];
    $whom_message = $_POST['whom_message'];
    $topic = $_POST['topic'];
    $message = $_POST['message'];

    $query = "INSERT INTO message (`whom_message`, `from_message`, `topic`, `message`, `status`, `time_message`, instance_id) 
    VALUES ('".$whom_message."','".$from_message."','".$topic."','".$message."', 'forSend', '".date('Y-m-d h:i:s')."',  $instance_id)";
    
    // echo $query;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->close();
        $db->close();
    }
}

//-----------------------------------------------------------------show-------------------------------------------------------       
if ($_GET['func'] == 'show') {
    $start = $_GET['start'];
    $limit = $_GET['limit'];
    $query = "SELECT id_message FROM `message` WHERE instance_id=$instance_id";
    $stmt = $db->query($query); 
    $resultRows = $stmt->num_rows;
    $stmt->close();

    $query = "SELECT `id_message`, `whom_message`, `from_message`, `topic`, `message`, `status`, `time_message` FROM `message` WHERE instance_id=$instance_id ORDER BY `time_message` DESC LIMIT ".$start.",".$limit;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($id_message, $whom_message, $from_message, $topic, $message, $status, $time_message);
        while ($stmt->fetch()) 
        {
          $Model [$i]= 
            array('id_message'=>$id_message,
                  'whom_message'=>$whom_message,
                  'from_message'=>$from_message,
                  'topic'=>$topic,
                  'message'=>$message,
                  'status'=>$status,
                  'time_message'=>$time_message,
                  );            
            $i++;
        } 
        $stmt->close();
        $db->close();
    }
    // $total = count($Model);
    echo'{total: '.$resultRows.',rows:'.json_encode($Model).'}';   
    // print_r($Model);
    // exit;
}

//------------------------------------------------------edit-------------------------------------------------------------
if ($_GET['func'] == 'edit') {
    $id_message = $_POST['id_message'];
    // echo $id_message;
    // exit;
    $from_message = $_POST['from_message'];
    $whom_message = $_POST['whom_message'];
    $topic = $_POST['topic'];
    $message = $_POST['message'];

    $query = "UPDATE message SET 
                    `whom_message` = '".$whom_message."',
                    `from_message` = '".$from_message."',
                    `topic` = '".$topic."',
                    `message` = '".$message."', 
                    `status` = 'forSend', 
                    `time_message` = '".date('Y-m-d h:i:s')."'
                WHERE id_message = ".$id_message;
                        // echo $query;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->close();
        $db->close();
    }
}

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


// -------------------------------------------------------------modelEquipmentStore----------------------------------------

//-----------------------------------------------------------------del-------------------------------------------------------       
if ($_GET['func'] == 'del') {
    $id_message = $_GET['id_message'];
    $query = "DELETE FROM message WHERE id_message=".$id_message." LIMIT 1";
    // echo $query;
    // exit;
    if ($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->close();
        $db->close();
    }
}
