<?php    
  session_start();
  header('Content-Type: text/html; charset=utf-8');
  //echo chr(239).chr(187).chr(191);;
  $role = $_SESSION['role'];
  $id_user = $_SESSION['id'];
  include "../settings.php";
  
  $instance_id = $_SESSION['client_id'];
  
  $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 ms_question.id, ms_question.name, ms_client.name, date_start, date_end, ms_type_question.name FROM ms_question LEFT JOIN ms_client ON ms_question.id_client=ms_client.id LEFT JOIN ms_type_question ON ms_question.id_type_question=ms_type_question.id WHERE instance_id = ".$instance_id."";
    $query = "SELECT q.id, q.name, tp.name, tp.address, q.date_start, q.date_end, tq.name
FROM ms_question q
LEFT JOIN ms_trade_point tp ON q.id_client=tp.`id`
LEFT JOIN ms_type_question tq ON q.id_type_question=tq.id
WHERE q.instance_id = ".$instance_id.";";
	if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id, $name, $name_client, $address_client, $date_start, $date_end, $name_type_question);
      while ($stmt->fetch()) {
        $Model[$i] = 
        array('id'=>$id,
              'name'=>$name,
              'name_client'=>$name_client.' '.$address_client,
              'date_start'=>$date_start,
              'date_end'=>$date_end,
              'name_type_question'=>$name_type_question
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo'{rows:'.json_encode($Model).'}';   
  }
  
  
  //-------------------------------------------------add--------------------------------------------
  if ($_POST['func']=='add')
  {
    $name = $_POST['name'];
    $id_type_question = $_POST['id_type_question'];
    $id_client = $_POST['id_client'];
    $date_start = $_POST['date_start'];
    $date_end = $_POST['date_end'];
    
    $body = '<div><h4>'.$name.'</h4></div>';
    
    $query = "INSERT INTO ms_question (name, `id_client`, id_trade_point, date_start, date_end, body, id_type_question,instance_id) VALUES ('".$name."', ".$id_client.", ".$id_client.", '".$date_start."', '".$date_end."', '".$body."', ".$id_type_question.",".$instance_id.")";
    if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->close();
		 echo json_encode(array('success'=>true, 'message'=>''));
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
    $db->close();
	exit;
  }  
  
  //-----------------------------------------delete----------------------------------
  if ($_POST['func']=='delete')
  {
    $string_id=$_POST['id'];

    $query = "DELETE FROM ms_question WHERE id=".$string_id;
    $stmt = $db->prepare($query);
    $stmt->execute();

    $stmt->close();
    $db->close();
  }
  
  
  
  
  if ($_GET['func']=='showTypeQuestion')
  {
    $i=0;
    $query = "SELECT id,name FROM ms_type_question";
    if ($stmt = $db->prepare($query))
    {
      $stmt->execute();
      $stmt->bind_result($id,$name);
      while ($stmt->fetch())
      {
        $store[$i]= 
        array('id'=>$id,
              'name'=>$name,
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo'{rows:'.json_encode($store).'}';
  }
  
  if ($_GET['func']=='showClient')
  {
    $i=0;
	$query = "SELECT tp.id,tp.name,r.name,n.name,c.name,tp.address FROM ms_trade_point tp LEFT JOIN ms_region r ON tp.id_region = r.id LEFT JOIN ms_network n ON tp.id_network = n.id LEFT JOIN ms_city c ON tp.id_city = c.id WHERE tp.instance_id = ".$instance_id.";"; //WHERE tp.id > 0 AND tp.id <501 "; // WHERE tp.id >= ".$_GET['start']." LIMIT ". $_GET['limit'];
    
	//echo $query;
   $stmt = $db->prepare($query);
      $stmt->execute();
      $stmt->bind_result($id, $name, $region, $network, $city, $address);
      while ($stmt->fetch()) {
        $Model[$i] = 
        array(
               'id'=>$id,
               'name'=>$name,
               'region'=>$region,
               'network'=>$network,
               'city'=>$city,
               'address'=>$address
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    
    echo'{rows:'.json_encode($Model).'}'; 
  }
  
?>