<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style type="text/css">
    .subsel{background: blue; color: #f7f7f7;}
  </style>  
</head>
<body>
<?php
  require_once("models/config.php");
  if(isUserLoggedIn()) { 
    
  }else{
    header("Location: index.php"); die(); 
  }
  
$mysqli = new mysqli("localhost", "bdicentralserver_sc", "Myl-PGfSbzcf", "bdicentralserver_sc");
              $result_pro = $mysqli->query("SELECT * FROM manufacturer_xref_user_mx WHERE user_id = ".$loggedInUser->user_id."");
              if ($result_pro->num_rows>0) {
                
              
?> 
        
            <?php 
            $i = 1;
            $categorias = array();
            echo "<h3>Categorías</h3><hr><select style=\"width: 100%;\" id=\"categoria_seleccionada\" multiple size=\"20\">";
            while ($row = $result_pro->fetch_assoc()) {

              $result_pro1 = $mysqli->query("SELECT DISTINCT id_subcategoria FROM pricefile_mx WHERE id_marca =  ".intval($row["manufacturer_id_xref"]));
              while ($row1 = $result_pro1->fetch_assoc()) {
                
                $result_pro2 = $mysqli->query("SELECT if(parent_id=0,id,parent_id) as parent_id FROM categorias_mx WHERE id = ".intval($row1["id_subcategoria"])." ORDER BY nombre ASC");
                $row3 = $result_pro2->fetch_row();
                
                if (!in_array( $row3, $categorias)) {
                  $categorias[] = $row3;
                }
              }
            }
            foreach ($categorias as $categoria) {
              $result_ca = $mysqli->query("SELECT ci.id, ci.nombre FROM categorias_mx ci WHERE ci.id =  ".intval($categoria[0])." ORDER BY ci.nombre ASC");
              while ($row_ca = $result_ca->fetch_assoc()) {
                
                $rs0 = $mysqli->query("SELECT * FROM category_xref_user_mx WHERE user_id = ".$loggedInUser->user_id." AND category_id = ".$row_ca["id"]);

                     if ($rs0->num_rows==0) {
                        echo "<option value=\"".$row_ca["id"]."\">".utf8_encode($row_ca["nombre"])."</option>";
                     }else{
                        echo "<option class=\"subsel\" value=\"".$row_ca["id"]."\">".utf8_encode($row_ca["nombre"])."</option>";                      
                     }
              }
            }
            echo "</select>";
            }else{
              echo "<p class=\"alert\">Seleccionar Marcas</p>";
            }
         ?>
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/feather-icons/4.9.0/feather.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
  $( document ).ready(function() {

     $("#categoria_seleccionada").dblclick(function( event ) {
      $.post( "unset-category.php", { id: $(this).val() } );
      $("#categoria_seleccionada option:selected").removeClass("subsel");
      
     });
     $("#categoria_seleccionada").click(function( event ) {
      $.post( "set-category.php", { id: $(this).val() } );
      $("#categoria_seleccionada option:selected").addClass("subsel");
      
     });

     $("#categoria_seleccionada").change(function( event ) {

      $("#subcategorias_por_seleccionar").load("subcategorias-disponibles.php?id="+$(this).val());
     });

    
   });
</script>        
</body>
</html>