<?php

if($_SESSION["tipo_usuario"] == "Vendedor"){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<style>
.select2-container--default .select2-selection--single {
    background-color: #fff;
    border: 1px solid #aaa;
    border-radius: 4px;
    height: calc(2.25rem + 2px);
}
.hidden {
  display: none !important;
}


.visible-xs,
.visible-sm,
.visible-md,
.visible-lg {
  display: none !important;
}
.visible-xs-block,
.visible-xs-inline,
.visible-xs-inline-block,
.visible-sm-block,
.visible-sm-inline,
.visible-sm-inline-block,
.visible-md-block,
.visible-md-inline,
.visible-md-inline-block,
.visible-lg-block,
.visible-lg-inline,
.visible-lg-inline-block {
  display: none !important;
}
@media (max-width: 767px) {
  .visible-xs {
    display: block !important;
  }
  table.visible-xs {
    display: table !important;
  }
  tr.visible-xs {
    display: table-row !important;
  }
  th.visible-xs,
  td.visible-xs {
    display: table-cell !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-block {
    display: block !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-inline {
    display: inline !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm {
    display: block !important;
  }
  table.visible-sm {
    display: table !important;
  }
  tr.visible-sm {
    display: table-row !important;
  }
  th.visible-sm,
  td.visible-sm {
    display: table-cell !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-block {
    display: block !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-inline {
    display: inline !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md {
    display: block !important;
  }
  table.visible-md {
    display: table !important;
  }
  tr.visible-md {
    display: table-row !important;
  }
  th.visible-md,
  td.visible-md {
    display: table-cell !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-block {
    display: block !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-inline {
    display: inline !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg {
    display: block !important;
  }
  table.visible-lg {
    display: table !important;
  }
  tr.visible-lg {
    display: table-row !important;
  }
  th.visible-lg,
  td.visible-lg {
    display: table-cell !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-block {
    display: block !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-inline {
    display: inline !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-inline-block {
    display: inline-block !important;
  }
}
@media (max-width: 767px) {
  .hidden-xs {
    display: none !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .hidden-sm {
    display: none !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .hidden-md {
    display: none !important;
  }
}
@media (min-width: 1200px) {
  .hidden-lg {
    display: none !important;
  }
}
.visible-print {
  display: none !important;
}
@media print {
  .visible-print {
    display: block !important;
  }
  table.visible-print {
    display: table !important;
  }
  tr.visible-print {
    display: table-row !important;
  }
  th.visible-print,
  td.visible-print {
    display: table-cell !important;
  }
}
.visible-print-block {
  display: none !important;
}
@media print {
  .visible-print-block {
    display: block !important;
  }
}
.visible-print-inline {
  display: none !important;
}
@media print {
  .visible-print-inline {
    display: inline !important;
  }
}
.visible-print-inline-block {
  display: none !important;
}
@media print {
  .visible-print-inline-block {
    display: inline-block !important;
  }
}
@media print {
  .hidden-print {
    display: none !important;
  }
}

</style>


<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header" style="padding: 2px;">
      <div class="container-fluid">
        <div class="row mb-2">
        </div>
      </div><!-- /.container-fluid -->
    </section>

    
    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="row">
        <!--=====================================
            EL FORMULARIO
            ======================================-->

        <div class="col-lg-5 col-xs-12">
        
          <div class="card card-outline card-primary" style="padding: 10px">
            
            <div class="box-header with-border"></div>

            <form role="form" method="post" class="formulariopedido">
              
              <div class="box-body">

                <div class="box">

                   <!--=====================================
                  ENTRADA DEL CODIGO
                  ======================================-->
                  <div class = "row">

                    <div class="col-6">
                      <div class="form-group">
                        <div class="input-group">
                          <div class="input-group-append">
                            <div class="input-group-text">
                              <span class="fas fa-key"></span>
                            </div>
                          </div> 
                          <?php
                          $item = null;
                          $valor = null;
                          $ventas = ControladorRemision::ctrMostrarPedido($item, $valor);
                          if(!$ventas){
                            echo '<input type="text" class="form-control form-control-sm" id="nuevaGuia" name="nuevaGuia" value="000001" readonly>';
                          }else{
                            foreach ($ventas as $key => $value) {
                            }
                            $codigo = $value["codigo_pedido"] + 1;
                            $id = $value["id"] + 1;
                            echo '<input type="text" class="form-control form-control-sm" id="nuevaGuia" name="nuevaGuia" value="'.$codigo.'" readonly>
                                  <input type="hidden" class="form-control" id="nuevoid" name="nuevoid" value="'.$id.'">';
                          }

                          ?>
                        </div>

                      </div> 
                    </div>

                    <div class="col-6">
                      <div class="form-group">
                        <div class="input-group">
                          <div class="input-group-append">
                            <div class="input-group-text">
                              <span class="fas fa-user"></span>
                            </div>
                          </div> 
                          <?php 
                              $itemEmpleado = "id";
                              $valorEmpleado = $_SESSION["id_empleado"];

                              $empleado = ControladorEmpleado:: ctrMostrarEmpleado($itemEmpleado, $valorEmpleado);
                              ?>
                              <input type="text" class="form-control form-control-sm" id="nuevoVendedor" name="seleccionarVendedor" value="<?php echo $empleado["nombre"]; ?>" readonly>
                              <input type="hidden" name="idVendedor" value="<?php echo $_SESSION["id"]; ?>">
                        </div>
                      </div> 
                   </div>

                  </div> 
                  <!--=====================================
                  ENTRADA DEL LAS FECHAS
                  ======================================-->

                  <div class="row">
                    <div class="col-md-6">

                                    <div class="form-group">
                                        <label>Fecha de Emision:</label>
                                        <div class="input-group">
                                        <div class="input-group-prepend">
                                            <div class="input-group-text">
                                            <span class="fa fa-calendar"></span> 
                                            </div>
                                        </div>
                                        <script>
                                          window.onload = function(){
                                          var fecha = new Date(); //Fecha actual
                                          var mes = fecha.getMonth()+1; //obteniendo mes
                                          var dia = fecha.getDate(); //obteniendo dia
                                          var ano = fecha.getFullYear(); //obteniendo año
                                          if(dia<10)
                                            dia='0'+dia; //agrega cero si el menor de 10
                                          if(mes<10)
                                            mes='0'+mes //agrega cero si el menor de 10
                                          document.getElementById('editarFecha').value=ano+"-"+mes+"-"+dia;
            }
                                        </script>

                                        <input type="date" class="form-control  form-control-sm"  name="editarFecha" id="editarFecha"  required readonly >
                                 
                                        </div>
                                    </div>

                    </div>

                    <div class="col-md-6">

                                      <div class="form-group">
                                        <label>Fecha de Traslado:</label>
                                        <div class="input-group">
                                        <div class="input-group-prepend" >
                                            <div class="input-group-text">
                                            <span class="fa fa-calendar"></span> 
                                            </div>
                                        </div>

                                        <input type="date" class="form-control form-control-sm"  name="editarFecha1" id="editarFecha1"  required>
                                        </div>
                                    </div>

                    </div>

                  </div>

                  <!--=====================================
                  ENTRADA DEL SUCURSAL
                  ======================================-->

                  <div class = "row">

                    <div class="col-md-6">

                          <div class="form-group">
                          
                            <div class="input-group">
                            <input type="hidden" id="moneda" value="<?php echo $_SESSION["cambio"] ?>" >
                           

                                <?php

                                $item = null;
                                $valor = null;

                                $sucursal = ControladorGeneral::ctrMostrarSucursal($item, $valor);


                                foreach ($sucursal as $key => $value) {

                                  echo '<input type="text" class="form-control form-control-sm" name="nuevaSerie" id="nuevaSerie" value="'.$value["direccion"].'" readonly >
                                        <input type="hidden" class="form-control" id="idSucursal" name="idSucursal" value="'.$value["id"].'">';


                                }

                                ?>

                          

                            </div>

                          </div>      
                    </div>
                    <div class="col-md-6">
                    <div class="form-group">
                          <div class="input-group">
                              <input type="text" class="form-control form-control-sm" id="seleccionarDestino" name="seleccionarDestino"   placeholder="Ingresar Direccion" required>
                            </div>
                          </div>
                    </div>
                  </div>

                   <!--=====================================
                  ENTRADA DEL DESTINATARIO
                  ======================================-->

                  <div class = "row">

                    <div class="col-md-6">

                          <div class="form-group">

                            <div class="input-group">
                              

                                <?php

                                $item = null;
                                $valor = null;

                                $sucursal = ControladorGeneral::ctrMostrarSucursal($item, $valor);


                                foreach ($sucursal as $key => $value) {

                                  echo '<input type="text" class="form-control form-control-sm" name="nuevaSerie" id="nuevaSerie" value="'.$value["razon_social"].'" readonly >
                                  ';
             

                                }

                                ?>

                          

                            </div>

                          </div>    
                          <div class="form-group">
                          
                            <div class="input-group">
                              

                                <?php

                                $item = null;
                                $valor = null;

                                $sucursal = ControladorGeneral::ctrMostrarSucursal($item, $valor);


                                foreach ($sucursal as $key => $value) {

                                  echo '<input type="text" class="form-control form-control-sm" name="nuevaSerie" id="nuevaSerie" value="'.$value["num_documento"].'" readonly >
                                  ';

                                }

                                ?>

                          

                            </div>

                          </div>    
                    </div>

                    <div class="col-md-6">

                    <div class="form-group">
 
                          <div class="input-group">
                            
                            <div class="input-group-append">

                              <div class="input-group-text">
                                <span class="fas fa-user"></span>
                              </div>

                            </div> 

                            <select class="form-control form-control-sm" id="seleccionarVehiculo" name="seleccionarVehiculo" required disabled>

                              <option value="">Seleccionar Vehiculo</option>

                  

                            </select>

                          </div>

                        </div>

                        <div class="form-group">
                          
                          <div class="input-group">
                            
                            <div class="input-group-append">

                              <div class="input-group-text">
                                <span class="fas fa-user"></span>
                              </div>

                            </div> 

                            <select class="form-control form-control-sm" id="seleccionarChofer" name="seleccionarChofer" required disabled>

                              <option value="">Seleccionar Chofer</option>

                              <?php

                              $item = null;
                              $valor = null;

                              $empleado = ControladorEmpleado:: ctrMostrarEmpleado1($item, $valor);


                              foreach ($empleado as $key => $value) {

                                echo '<option value="'.$value["id"].'">'.$value["nombre"].' - ' .$value["licencia"].'</option>';

                              }

                              ?>

                            </select>

                          </div>

                        </div>


                    </div>

                  </div>

                  <div class="row">
                    <div class="col-12">       
                        <div class="form-group">
                          <div class="input-group">
                            <div class="input-group-append">
                              <div class="input-group-text">
                                <span class="fas fa-user"></span>
                              </div>
                            </div> 

                            <select class="form-control select2 form-control-sm" id="seleccionarCliente" name="seleccionarCliente"  required>

                              <option value="">Seleccionar cliente</option>

                              <?php

                              $item = null;
                              $valor = null;

                              $categorias = ControladorClientes::ctrMostrarClientes($item, $valor);

                              

                              foreach ($categorias as $key => $value) {

                                echo '<option style="color: white;" estado="'.$value["estado"].'" value="'.$value["id"].'">'.$value["razon_social"]. ' - ' .$value["ruc_dni"].'</option>';

                              }

                              ?>

                            </select>
                          </div>

                        </div> 

                    </div>
                  </div>


                  <!--=====================================
                  ENTRADA PARA AGREGAR PRODUCTO
                  ======================================--> 
                  <div class="col-xs-8 pull-right">
                      
                      <table style="font-size:75%" class="table table-bordered productos">

                        <thead>

                          <tr>
                            <th style="width: 5px">Producto</th>
                            <th ><center>Cantidad</center></th>
                            <th>Precio</th>  
                            <th>Total</th>     
                          </tr>
                        </thead>
                        <tbody class="nuevoProducto" >

                        </tbody>
                      </table>

                      <div class="nuevoCodigo"></div>
                  </div>

                  <input type="hidden" id="listaProductos" name="listaProductos">
                 
                  <!--=====================================
                  BOTÓN PARA AGREGAR PRODUCTO
                  ======================================-->

                  <button type="button" class="btn btn-default btnAgregarProducto hidden-lg">Agregar producto</button>

                  <div class="row">

                    <!--=====================================
                    ENTRADA IMPUESTOS Y TOTAL
                    ======================================-->
                    
                    <div class="col-xs-8 pull-right">
                      
                      <table style="font-size:75%" class="table table-bordered">

                        <thead>

                          <tr>
                            <th><center>TOTAL</center></th>    
                          </tr>

                        </thead>

                        <tbody>
                        
                          <tr>  
                            
                            <td style="width: 30%">
                              
                              <div class="input-group">
                            
                                <div class="input-group-append">

                                  <div class="input-group-text">
                                    <span class="ion ion-social-usd"></span>
                                  </div>

                                </div> 

                                <input type="text" class="form-control form-control-sm" id="nuevoPrecioNeto" name="nuevoPrecioNeto" total="" placeholder="00000" readonly required>

                                <input type="hidden" name="totalVenta" id="totalVenta">
                                
                          
                              </div>

                            </td>

                          </tr>

                        </tbody>

                      </table>

                    </div>

                  </div>

                  <hr>
                </div>

              </div>
          

                  <div class = "row">

                    <div class="col-md-4">

                          <div class="form-group">
                          
                            <div class="input-group">

     
                              <input type="number" class="form-control form-control-sm" name="bulto" id="bulto" placeholder="Ingresar bulto" required>
                               
                        
                            </div>

                          </div>      
                    </div>

                    <div class="col-md-4">

                          <div class="form-group">
                          
                            <div class="input-group">

     
                              <input type="number" class="form-control form-control-sm" name="peso" id="peso" placeholder="Ingresar peso" required  >
                               
                        
                            </div>

                          </div>      
                    </div>
                    <div class="col-md-4">
                          <div class="form-group">
                            <div class="input-group">

                              <select class="form-control form-control-sm" id="seleccionarTransporte" name="seleccionarTransporte" required >

                                <option value="">Seleccionar Tipo Transporte</option>
                                <option value="TRANSPORTE PUBLICO">TRANSPORTE PUBLICO</option>
                                <option value="TRANSPORTE PRIVADO">TRANSPORTE PRIVADO</option>
                              </select>

                            </div>
                          </div>
                      </div>
                    </div>

                    <div class="pedidos">

                    </div>





              <div class="box-footer">
                <button type="submit" class="btn btn-primary btn-sm ">Guardar</button>
                <button type="button" class="btn btn-info btn-sm" onclick="history.back()">VOLVER</button>
              </div>

            </form>
            <?php
              $guardarRemision= new ControladorRemision();
              $guardarRemision -> ctrCrearPedido();
        
          ?>
          </div>
        </div>
        <!--=====================================
          LA TABLA DE PRODUCTOS
        ======================================-->

          <div class="col-lg-7 hidden-md hidden-sm hidden-xs">
            
            <div class="card card-outline card-warning" style="padding: 10px">

              <div class="box-header with-border"></div>

              <div class="box-body">
                
                <table style="font-size:75%" class="table table-bordered table-striped dt-responsive tablapedidos">
                  
                  <thead>

                    <tr>
                    <th>Imagen</th>
                      <th>Descripcion</th>
                      <th>Marca</th>
                      <th>Stock</th>
                      <th>Acciones</th>
                    </tr>

                  </thead>

                </table>

              </div>

            </div>


          </div>




      </div>
      <!-- /.row -->

    </section>
    <!-- /.content -->
  </div>

  <div class="modal fade" id="modalimagen">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body" style=" width: 400px; height: 450px;">
              <div id="carousel">
                <figure style=" width: 400px; height: 400px;"><img class="img-thumbnail verImagen" width="400px"></figure>
                <input type="hidden" name="verImagen" id="verImagen">
              </div>
            </div>

          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->


      <script>
      $(function () {
        //Initialize Select2 Elements
        $('.select2bs4').select2({
          theme: 'bootstrap4'
        })

        //Initialize Select2 Elements
        $('.select2').select2()


      })

    </script>

<script type="text/javascript">
$(document).ready(function() {
    $("form").keypress(function(e) {
        if (e.which == 13) {
            return false;
        }
    });
});

$('#reservationdate').datetimepicker({
        format: 'DD-MM-YYYY',
        language: "it",
    });

    $('#reservationdate1').datetimepicker({
        format: 'DD-MM-YYYY',
        language: "it"
    });


</script>
<script>
      
      var elemento = document.getElementById("pedidos");
      elemento.className += " active";

      var elemento = document.getElementById("pedido");
      elemento.className += " menu-open";

      var elemento = document.getElementById("crearpedido");
      elemento.className += " active";
</script>
<script src="vistas/js/pedidos.js"></script>