<?php
  require_once("models/config.php");
  if(isUserLoggedIn()) { 
    //print_r($_SESSION["userPieUser"]->user_id);
  }else{
    header("Location: index.php"); die(); 
  }
  
if (isset($_POST["action"])) {
	switch ($_POST["action"]) {
		case 'insert_manufacturer': insert_manufacturer($_POST["id"]); break;
		case 'remove_manufacturer': remove_manufacturer($_POST["id"]); break;
		case 'set_unselected_category': set_unselected_category($_POST["id"]); break;
		case 'set_selected_category': set_selected_category($_POST["id"]); break;
		case 'delete': remove_product($_POST["id"]); break;
		case 'insert': insert_product($_POST["id"]); break;		
		case 'insert_bulk': insert_product_bulk($_POST["id_subcategoria"],$_POST["user_id"]); break;
		case 'delete_bulk': delete_product_bulk($_POST["id_subcategoria"],$_POST["user_id"]); break;
	}
}
if (isset($_GET["man"])) {
	insert_manufacturer2($_GET["man"]);
}
function insert_manufacturer2($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$resultado = $mysqli->query("SELECT distinct ci.id, ci.nombre FROM pricefile_mx p INNER JOIN categorias_mx ci ON ci.id = p.id_subcategoria WHERE p.isactive= 1 AND p.id_marca = ".$id." AND p.id_subcategoria NOT IN  (SELECT category_id FROM category_xref_user_mx WHERE user_id = ".$_SESSION["userPieUser"]->user_id.")");
	$subcategorias = array();
	while ($row = $resultado->fetch_assoc()) {
		if (!in_array($row["nombre"], $subcategorias)) {
			$subcategorias[] = $row["nombre"];
		}
	}


}
function insert_manufacturer($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("INSERT INTO manufacturer_xref_user_mx (manufacturer_id_xref,user_id) VALUES (".$id.",".$_SESSION["userPieUser"]->user_id.")");

	$resultado = $mysqli->query("SELECT distinct ci.id, ci.nombre FROM pricefile_mx p INNER JOIN categorias_mx ci ON ci.id = p.id_subcategoria WHERE p.isactive= 1 AND p.id_marca = ".$id." AND p.id_subcategoria NOT IN  (SELECT category_id FROM category_xref_user_mx WHERE user_id = ".$_SESSION["userPieUser"]->user_id.")");
	$subcategorias = array();
	while ($row = $resultado->fetch_assoc()) {
		if (!in_array($row["nombre"], $subcategorias)) {
			$subcategorias[] = $row["nombre"];
		}
	}
	if (!empty($subcategorias)) {
		echo "Te sugerimos agregar estas subcategorias: ".implode(",", $subcategorias);
	}else{
		echo "no";
	}
	
}
function remove_manufacturer($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("DELETE FROM manufacturer_xref_user_mx WHERE user_id = ".$_SESSION["userPieUser"]->user_id." AND manufacturer_id_xref = ".$id);
	
}
function set_selected_category($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("DELETE FROM category_xref_user_mx WHERE user_id = ".$_SESSION["userPieUser"]->user_id." AND category_id = ".$id);
}
function set_unselected_category($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("INSERT INTO category_xref_user_mx (category_id,user_id) VALUES (".$id.",".$_SESSION["userPieUser"]->user_id.")");
}
function remove_product($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("DELETE FROM product_xref_user WHERE product_id = ".$id." AND user_id = ".$_SESSION["userPieUser"]->user_id);
}
function insert_product($id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	echo "INSERT INTO product_xref_user (user_id, product_id) VALUES (".$_SESSION["userPieUser"]->user_id.",".$id.")";
	$mysqli->query("INSERT INTO product_xref_user (user_id, product_id) VALUES (".$_SESSION["userPieUser"]->user_id.",".$id.")");
}
function insert_product_bulk($id_subcategoria = NULL, $user_id = NULL){
	$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
	$mysqli->query("INSERT INTO product_xref_user (user_id, product_id) SELECT ".$user_id.",p.id FROM pricefile p WHERE p.id_subcategoria = ".$id_subcategoria." AND id_fabricante IN (SELECT manufacturer_id_xref FROM manufacturer_xref_user WHERE user_id = ".$user_id.") AND p.isactive = 1 AND NOT EXISTS (SELECT id FROM product_xref_user WHERE product_id = p.id AND user_id = ".$user_id.") LIMIT 1");
}

?>   