<?php include '../../session.php'; ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php
        require '../../conexion.php';
        require '../../menu/css_lte.ctp';
        ?>
    </head>
    <body class="hold-transition skin-blue sidebar-mini">
        <div class="wrapper">
            <?php require '../../menu/cabecera.ctp'; ?><!--BARRA DE HERRAMIENTAS-->
            <?php require '../../menu/izquierda.ctp'; ?><!--BARRA DE HERRAMIENTAS-->
            <div class="content-wrapper">
                <div class="content">  
                    <!-- /.titulos -->
                    <div class="row">
                        <div class="col-lg-12">
                            <h3 class="page-header">Pedido de Servicios
                                <a href="pedido_index.php" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel='tooltip' title="Atras">
                                    <i class="glyphicon glyphicon-arrow-left"></i> VOLVER
                                </a> 
                                <?php $pedidos = consultas::get_datos("select * from v_servicios_pedidos where id_ped=" . $_REQUEST['vcod']);?>
                            </h3>
                        </div>     
                    </div>
                    <!-- /.FIN titulos -->

                    <!-- /.cabecera-detalle -->
                    <div class="row">
                        <div class="col-lg-12 col-sm-12 col-md-12">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    Datos Cabecera
                                </div>
                                <!-- /.panel-heading -->
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table width=""
                                               class="col-md-12 col-lg-12 col-xs-12 table-bordered table-condensed table-striped">
                                            <thead>
                                                <tr>
                                                    <th class="text-center">#</th>
                                                    <th class="text-center">Cliente</th>
                                                    <th class="text-center">Fecha</th>
                                                    <th class="text-center">Observación</th>
                                                    <th class="text-center">Total</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($pedidos as $ped) { ?> 
                                                    <tr>
                                                        <td class="text-center"><?php echo $ped['id_ped']; ?></td>
                                                        <td class="text-left"><?php echo $ped['cliente']; ?></td>
                                                        <td class="text-center"><?php echo date("d/m/Y", strtotime($ped['ped_fecha'])); ?></td>
                                                        <td class="text-left"><?php echo $ped['ped_obs']; ?></td>
                                                        <td class="text-center"><?php echo number_format($ped['ped_total'], 0, ',', '.'); ?></td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>                         
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.cabecera -->
                    </div>
                    <!--carga par el detalle-->
                    <?php if($pedidos[0]['ped_estado'] === 'PENDIENTE'){ ?>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="box box-default">                                                           
                                <div class="box-body">
                                    <div class="row">
                                        <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
                                            <!-- FORMULARIO DE PRODUCTOS -->
                                            <form class="" action="detped_control.php" method="post" id="frm_add_det">
                                                <input type="hidden" name="accion" value="1">
                                                <input type="hidden" name="vcod" value="<?php echo $_REQUEST['vcod']; ?>">  
                                                <input type="hidden" name="vitem" value="0">  
                                                <input type="hidden" name="pagina" value="detpedido_agregar.php?vcod=<?php echo $_REQUEST['vcod']; ?>" >
                                                <div class="row">
                                                    <div class="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label >Tipo: </label>
                                                            <?php $tipos = consultas::get_datos("select * from tipo_articulo"); ?>
                                                            <select class="form-control select2" id="tipo" required="" onchange="selarticulos()">
                                                                    <option value="">Seleccione un Tipo</option>
                                                                    <?php
                                                                    if (!empty($tipos)) {
                                                                        foreach ($tipos as $tipo) { ?>
                                                                            <option value="<?php echo $tipo['tarti_cod']; ?>">
                                                                                <?php echo $tipo['descripcion']; ?>
                                                                            </option>
                                                                            <?php
                                                                        }
                                                                    } else { ?>
                                                                        <option value="0">Debe insertar un tipo</option>
                                                                    <?php } ?>
                                                                </select>                                                            
                                                        </div>                                                    
                                                    </div>    
                                                    <div id="div_depositos">
                                                    <div class="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label >Deposito: </label>                                                            
                                                            <select name="vdep" class="form-control select2" id="depo" required="">
                                                                    <option value="">Seleccione un tipo</option>                                                            
                                                            </select>                                                            
                                                        </div>                                                    
                                                    </div>                                                      
                                                        <div class="col-md-6 col-sm-6 col-lg-6">
                                                                <label class="control-label">Producto: </label>                                                            
                                                                <select name="vart" class="form-control select2" id="vart" required="">
                                                                       <option value="">Seleccione un tipo</option>
                                                                </select>                                                        
                                                        </div>
                                                    </div>
                                                </div>                                                 
                                                <div class="row">                                               
                                                     <div class="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label>Precio:</label>
                                                            <input type="number" id="vprecio"class="form-control" name="vprecio" min="1" value="1" required=""/>
                                                        </div>                                                        
                                                    </div>                                                     
                                                    <div class="col-md-2 col-sm-2 col-lg-2 col-xs-5">
                                                        <div class="form-group">
                                                            <label>Cantidad:</label>
                                                            <input type="number" id="cant"class="form-control" name="vcant" min="1" value="1" required=""/>
                                                        </div>                                                        
                                                    </div>                                                                                                        
                                                    <div class="col-md-1 col-sm-1 col-lg-1">
                                                        <div class="form-group">
                                                            <label class="control-label hidden-xs">&nbsp;</label>
                                                            <button type="submit" class="btn btn-primary btn-sm pull-right"><i class="fa fa-plus"></i> AGREGAR</button>
                                                        </div>
                                                    </div>                                                    
                                                </div>                                                
                                           </form>
                                                <!-- FIN FORMULARIO DE PRODUCTOS -->                                             
                                        </div>                                        
                                    </div>                                    
                                </div>
                            </div>                                                
                        </div>
                    </div>   
                    <?php } ?>                       
                    <!-- /.fin cabecera-detalle -->

                    <!--GRILLA DETALLE-->
                    <div class="row">
                        <?php
                        $detpeds = consultas::get_datos("select * from v_servicios_pedidos_detalle where id_ped = " . $_REQUEST['vcod'] . " order by id_item asc");
                        ?>
                        <div class="col-lg-12">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    Detalles de Pedido
                                </div>
                                <?php if (!empty($detpeds)) { ?>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead> 
                                                <tr>
                                                    <th class="text-center">Articulo</th>
                                                    <th class="text-center">Cantidad</th>
                                                    <th class="text-center">Precio</th>
                                                    <th class="text-center">Subtotal</th>
                                                    <th class="text-center">Accion</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($detpeds as $detped) { ?>
                                                    <tr>
                                                        <td class="text-left"><?php echo $detped['art_descri']; ?></td>
                                                        <td class="text-center"><?php echo number_format($detped['ped_precio'], 0, ',', '.'); ?></td>
                                                        <td class="text-center"><?php echo $detped['ped_cant']; ?></td>
                                                        <td class="text-center"><?php echo number_format($detped['subtotal'], 0, ',', '.'); ?></td>
                                                        <td class="text-center">
                                                            <?php if ($ped['ped_estado'] == 'PENDIENTE') { ?>
                                                                <a onclick="editar(<?php echo $detped['id_ped']; ?>,<?php echo $detped['id_articulo']; ?>,<?php echo $detped['id_item']; ?>)"
                                                                   class="btn btn-sm btn-warning" rel='tooltip' data-title="Editar" 
                                                                   data-toggle="modal" data-target="#mymodal">
                                                                    <span class="glyphicon glyphicon-edit"></span>
                                                                </a>

                                                                <a onclick="borrar(<?php
                                                                echo"'" . $detped['id_ped'] . "_" . $detped['id_articulo']. "_" . $detped['id_item'] . "_" .$detped['art_descri']. "'";
                                                                ?>)" class="btn btn-sm btn-danger" rel="tooltip" data-title="Borrar"
                                                                   data-toggle="modal" data-target="#delete">
                                                                    <span class="glyphicon glyphicon-trash"></span>
                                                                </a>

                                                            <?php } ?>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                     </div>
                                    </div>
                                    <?php } else { ?>
                                            <div class="panel-body">
                                                <div class='alert alert-info'>
                                                    <span class='glyphicon glyphicon-info-sign'>
                                                    </span> A&uacute;n no se han registrado detalles...
                                                </div>                                            
                                            </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    <!--FIN GRILLA DETALLE-->
                </div>
            </div>  
        </div>
        <!--archivos js-->  
        <?php require '../../menu/js_lte.ctp'; ?>
            <!-- MODAL-->
            <div class="modal fade" id="mymodal" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content" id="det_modal">
                        
                    </div>
                </div>
            </div>
            <!-- FIN MODAL-->                
            <!--borrar-->
            <div class="modal fade" id="confirmar" tabindex="-1" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title custom_align" id="Heading">Atenci&oacute;n!!!</h4>
                        </div>
                        <div class="modal-body">

                            <div class="alert alert-success" id="cconfirmacion"></div>

                        </div>
                        <div class="modal-footer">
                            <a id="sic" role="button" class="btn btn-primary" ><span class="glyphicon glyphicon-ok-sign"></span> Si</a>
                            <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
                        </div>
                    </div>
                </div>
            </div> 
            <!--FIN borrar-->            
            <!--borrar-->
            <div class="modal fade" id="delete" tabindex="-1" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title custom_align" id="Heading">Atenci&oacute;n!!!</h4>
                        </div>
                        <div class="modal-body">

                            <div class="alert alert-danger" id="confirmacion"></div>

                        </div>
                        <div class="modal-footer">
                            <a id="si" role="button" class="btn btn-primary" ><span class="glyphicon glyphicon-ok-sign"></span> Si</a>
                            <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
                        </div>
                    </div>
                </div>
            </div> 
            <!--FIN borrar-->        
        <!--js-->  
        <script>            
            function selarticulos(){
                var vtipo = $("#tipo").val();
                if(vtipo !== null){
                $.ajax({
                    type: "POST", url: "cbo_depositos.php", data: {vtipo: vtipo}
                }).done(function (datos) {
                    $("#div_depositos").html(datos);
                });  
                }
            };            
            function obtener_articulos(){
                var vdepo = $("#depo").val();
                var vtipo = $("#tipo").val();
                if(vdepo !== null && vtipo !== null){
                $.ajax({
                    type: "POST", url: "cbo_articulos.php", data: {vdepo: vdepo,vtipo: vtipo}
                }).done(function (datos) {
                    $("#div_articulos").html(datos);
                });  
                }
            };             
              function obtenerprecio() {
                var dat = $('#vart').val().split("_");
                if (parseInt($('#vart').val()) > 0) {
                    $("#vprecio").val(dat[1]);
                }
            };
            
            function articulo() {
                if (parseInt($('#tipo_articulo').val()) > 0) {
                    $.ajax({
                        type: "GET",
                        url: "/farmmedi/servicios/presupuesto/lista_articulo_pres.php?vtipoart=" + $('#tipo_articulo').val(), cache: false,
                        beforeSend: function () {
                            $('#detalles').html('<img src="/farmmedi/img/ajax-loader.gif">\n\ <strong><i>Cargando...</i></strong>');
                        },
                        success: function (msg) {
                            $('#detalles').html(msg);
                        }
                    });
                }
            };

            function obtenerprecios() {
                var dat = $('#vart').val().split("_");
                $("#vprecio").val(dat[1]);
                calsubtotal();
            };

            function calsubtotal() {
                
                $('#subtotal').val(parseInt($('#vprecio').val()) * parseInt($('#cant').val()));
            };

            function calsubtotal1() {                
                $('#subtotal').val(parseInt($('#vprecio').val()) * parseInt($('#cant').val()));
            };

            function borrar(datos) {
                var dat = datos.split("_");
                $('#si').attr('href',
                        'detped_control.php?vcod=' + dat[0] +
                        '&vart=' + dat[1] +
                        '&vitem=' + dat[2] +
                        '&accion=3' +
                        '&pagina=detpedido_agregar.php?vcod='+dat[0]);
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea Borrar el Articulo <i><strong>' + dat[3] + '</strong></i> del detalle?');
            };

            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(1000).slideUp(200, function () {
                $(this).alert('close');
            });


            function editar(idped,idprod,iditem) {
              $.ajax({
                    type: "get",
                    url : "detedit?vcod="+idped+"&vart="+idprod+"&vitem="+iditem,
                    cache:false,
                    beforeSend:function(){
                        $("#det_modal").html('<img src="../../img/ajax-loader.gif"/><strong>Cargando...</strong>');
                    },
                    success:function(data){
                        $("#det_modal").html(data)
                    }
                });                
            };

            function div_deposito() {
                var vtipoarti = $("#tipo_articulo").val();
                $.ajax({
                    type: "POST", url: "div_deposito.php", data: {tipo_articulo: vtipoarti}
                }).done(function (datos) {
                    $("#div_deposito1").html(datos);
                });
            };
        </script>
        <!--js-->  
    </body>
</html>
