<?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'];}
    $id_brand = $_GET['id_brand'];
    if ($_GET['params2']){$id_brand = $_GET['params2'];}
    $i=0;
    $query = "SELECT id, article, name, id_category_brand, barcode, info, price, unit, begin_date, end_date, delivery_period, balance_stock, photo FROM ms_product WHERE id_category_brand IN (SELECT id FROM ms_category_brand WHERE id_category=".$id_category." AND id_brand=".$id_brand.")";
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id, $article, $name, $id_category_brand, $barcode, $info, $price, $unit, $begin_date, $end_date, $delivery_period, $balance_stock, $photo);
      while ($stmt->fetch()) {
        $Model[$i] = 
        array('id'=>$id,
              'article'=>$article,
              'name'=>$name,
              'id_category_brand'=>$id_category,
              'barcode'=>$barcode,
              'info'=>$info,
              'price'=>$price,
              'unit'=>$unit,
              'begin_date'=>$begin_date,
              'end_date'=>$end_date,
              'delivery_period'=>$delivery_period,
              'balance_stock'=>$balance_stock,
              'photo'=>$photo
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo'{rows:'.json_encode($Model).'}';   
  }


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

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

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

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


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

    $query = "SELECT id, article, name, id_category_brand, barcode, info, price, unit, begin_date, end_date, delivery_period, balance_stock FROM ms_product WHERE id=".$idClient;
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id, $article, $name, $id_category_brand, $barcode, $info, $price, $unit, $begin_date, $end_date, $delivery_period, $balance_stock);
      while ($stmt->fetch()){
        $data_edit = $id.'/t'.$article.'/t'.$name.'/t'.$id_category_brand.'/t'.$barcode.'/t'.$info.'/t'.$price.'/t'.$unit.'/t'.$begin_date.'/t'.$end_date.'/t'.$delivery_period.'/t'.$balance_stock.'/t';
      } 
      $stmt->close();
      $db->close();
    }
    echo $data_edit;
  }


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

  if ($_POST['func']=='update')
  {
    $id = $_POST['id'];
    $article = $_POST['article'];
    $name = $_POST['name'];
    
    $id_category_brand = $_POST['id_category_brand'];
    $barcode = $_POST['barcode'];
    $info = $_POST['info'];
    $price = $_POST['price'];
    $unit = $_POST['unit'];
    $begin_date = $_POST['begin_date'];
    $end_date = $_POST['end_date'];
    $delivery_period = $_POST['delivery_period'];
    $balance_stock = $_POST['balance_stock'];
    $id_category = $_POST['id_category'];
    $id_brand = $_POST['id_brand'];
    
    //$photo = $_POST['photo'];
    
    if ($id == NaN)                             // add new rows
    {
      /*$query ="SELECT MAX(id) FROM ms_product";
      $stmt = $db->prepare($query);                   
      $stmt->execute();
      $stmt->bind_result($id);
      while ($stmt->fetch()) 
      {
        $number_id=$id;
      }
      $number_id++;*/
      $query = "INSERT INTO ms_product (article, name, id_category_brand, barcode, info, price, unit, begin_date, end_date, delivery_period, balance_stock) VALUES ('".$article."', '".$name."', (SELECT id FROM ms_category_brand WHERE id_category=".$id_category." AND id_brand=".$id_brand."), '".$barcode."', '".$info."', '".$price."', '".$unit."', '".$begin_date."', '".$end_date."', ".$delivery_period.", ".$balance_stock.")";
      $stmt = $db->prepare($query);
      $stmt->execute();    
    }
    else                                        // update rows
    {
      $query = "UPDATE ms_product SET article='".$article."', name='".$name."', id_category_brand=".$id_category_brand.", barcode='".$barcode."', info='".$info."', price='".$price."', unit='".$unit."', begin_date='".$begin_date."', end_date='".$end_date."', delivery_period='".$delivery_period."', balance_stock='".$balance_stock."' WHERE id=".$id;
      $stmt = $db->prepare($query);
      $stmt->execute();
    }
    
    $stmt->close();
    $db->close();       
  }  

?>