<?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";
  
  
  $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')
  {
    $id_category = $_GET['id_category'];
    if ($_GET['params1']){$id_category = $_GET['params1'];}
    $i=0;
    //$query = "SELECT id, name FROM ms_brand";
    $query = "SELECT ms_brand.id, ms_brand.name FROM ms_brand LEFT JOIN ms_category_brand ON ms_brand.id=ms_category_brand.id_brand WHERE ms_category_brand.id_category=".$id_category;
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id, $name);
      while ($stmt->fetch()) {
        $Model[$i] = 
        array('id'=>$id,
              'name'=>$name
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo'{rows:'.json_encode($Model).'}';   
  }


  //---------------------------- delete ----------------------------------------

  $i=0;
  if ($_POST['func']=='delete')
  {
    $id = $_POST['id'];
    $id_category = $_POST['id_category'];

    $query = "DELETE FROM ms_brand WHERE id= '".$id."'";
    echo $query;
    $stmt = $db->prepare($query);
    $stmt->execute();
    
    $query = "DELETE FROM ms_category_brand WHERE id_brand= '".$id."'"; // AND id_category = '".$id_category."'";
    echo $query;
    $stmt = $db->prepare($query);
    $stmt->execute();

    $stmt->close();
    $db->close();
  }


  //---------------- show to update-------------------------------
  if ($_POST['func']=='edit')
  {
    $idClient = $_POST['id'];

    $query = "SELECT id, name FROM ms_brand WHERE id=".$idClient;
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id,$name);
      while ($stmt->fetch()){
        $data_edit = $id.'/t'.$name.'/t';
      } 
      $stmt->close();
      $db->close();
    }
    echo $data_edit;
  }


  //---------------- update------------------------------------------------------------------

  if ($_POST['func']=='update')
  {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $code = $_POST['code'];
    $id_category = $_POST['id_category'];
    
    if ($id == NaN)                             // add new rows
    {
      /*$query ="SELECT MAX(id) FROM ms_brand";
      $stmt = $db->prepare($query);                   
      $stmt->execute();
      $stmt->bind_result($id);
      while ($stmt->fetch()) 
      {
        $number_id=$id;
      }
      $number_id++;*/
      $query = "INSERT INTO ms_brand (id,name) VALUES ('".$code."','".$name."')";
      $stmt = $db->prepare($query);
      $stmt->execute();
      $id_brand = $stmt->insert_id;
      $query = "INSERT INTO ms_category_brand (id_category, id_brand) VALUES (".$id_category.",'".$code."')";
      $stmt = $db->prepare($query);
      $stmt->execute();
    }
    else                                        // update rows
    {
      $query = "UPDATE ms_brand SET name='".$name."' WHERE id=".$id;
      $stmt = $db->prepare($query);
      $stmt->execute();
    }
    
    $stmt->close();
    $db->close();       
  }  

?>