<?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">Orden de Compra
                                <a href="ordenc_index.php" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel='tooltip' title="Atras">
                                    <i class="glyphicon glyphicon-arrow-left"></i> VOLVER
                                </a> 

                                <?php $ordens = consultas::get_datos("select * from v_compras_orden where orden_cod=" . $_REQUEST['vcod'] . "order by orden_cod asc"); ?>
                                <?php foreach ($ordens as $orden) { ?> 
                                    <?php if ($orden['orden_estado'] == 'ACTIVO') { ?>
                                        <?php if ($orden['orden_total'] != 0) { ?>
                                        <!--    <a  onclick="estado(<?php echo "'" . $orden['orden_cod'] . "_" . $orden['orden_cod'] . "'"; ?>)"
                                                data-toggle="modal" data-target="#delete"
                                                class="btn btn-sm btn-success" rel='tooltip' 
                                                data-title="Confirmar">
                                                <i class="fa fa-check"></i> CONFIRMAR -->
                                            </a>
                                        <?php } ?>
                                    <?php } ?>
                                <?php } ?>

                            </h3>
                        </div>     
                    </div>
                    <!-- /.FIN TITULOS -->
                    <!-- /.CUERPO -->
                    <div class="row">
                        <div class="col-lg-12">
                            <!-- /.cabecera -->
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    Datos Cabecera
                                </div>
                                <?php
                                $ordens = consultas::get_datos("select * from v_compras_orden where orden_cod=" . $_REQUEST['vcod'] . "order by orden_cod asc");

                                // $pedidos = consultas::get_datos("select * from v_compras_pedidos order by codigo asc ");
                                ?>                         
                                <!-- /.panel-heading -->
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table width="100%"
                                               class="table table-bordered">
                                            <thead>
                                                <tr>
                                                    <th class="text-center">Nro Orden</th>
                                                    <th class="text-center">Proveedor</th>
                                                    <th class="text-center">Fecha</th>
                                                    <th class="text-center">Total</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($ordens as $orden) { ?> 
                                                    <tr>
                                                        <td class="text-center"><?php echo $orden['orden_cod']; ?></td>
                                                        <td class="text-left"><?php echo $orden['prv']; ?></td>
                                                        <td class="text-center"><?php echo $orden['fecdate']; ?></td>
                                                        <td class="text-center"><?php echo number_format($orden['orden_total'], 0, ',', '.'); ?></td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>                         
                                    </div>
                                </div>
                            </div>
                            <!-- /.FIN cabecera -->
                    <!--carga par el detalle-->
                    <?php if($orden['orden_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="detorden_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="vtotal" value="0">
                                                <input type="hidden" name="pagina" value="detorden_agregar.php" >
                                                <div class="row">
                                                    <div class="col-md-4 col-sm-4 col-lg-4">
                                                        <div class="form-group">
                                                            <label >Deposito: </label>
                                                            <?php $depositos = consultas::get_datos("select * from deposito where cod_suc=".$_SESSION['cod_suc']); ?>
                                                            <select name="vdep" class="form-control select2" id="depo" required="">
                                                                    <option value="">Seleccione Deposito</option>
                                                                    <?php
                                                                    if (!empty($depositos)) {
                                                                        foreach ($depositos as $deposito) { ?>
                                                                            <option value="<?php echo $deposito['cod_depo']; ?>">
                                                                                <?php echo $deposito['dep_descrip']; ?>
                                                                            </option>
                                                                            <?php
                                                                        }
                                                                    } else { ?>
                                                                        <option value="0">Debe insertar un deposito</option>
                                                                    <?php } ?>
                                                                </select>                                                            
                                                        </div>                                                    
                                                    </div>                                                            
                                                    <div class="col-md-6 col-sm-6 col-lg-6">
                                                        <div class="form-group">
                                                            <label class="control-label">Producto: </label>
                                                            <?php $articulos = consultas::get_datos("select * from v_articulos where stock = 1"); ?>
                                                            <select name="vart" class="form-control select2" id="vart" required="" onchange="obtenerprecio()" onkeyup="obtenerprecio()">
                                                                   <option value="">Seleccione Articulo</option>
                                                                   <?php
                                                                   if (!empty($articulos)) {
                                                                       foreach ($articulos as $articulo) { ?>
                                                                           <option value="<?php echo $articulo['id_articulo']."_".$articulo['pre_compra']; ?>">
                                                                               <?php echo $articulo['art_descri']." ".$articulo['mar_descrip']; ?>
                                                                           </option>
                                                                           <?php
                                                                       }
                                                                   } else { ?>
                                                                       <option value="0">Debe insertar un Articulo</option>
                                                                   <?php } ?>
                                                               </select>
                                                        </div>                                                    
                                                    </div>                                                    
                                                </div>                                                 
                                                <div class="row">                                               
                                                    <div class="col-md-4 col-sm-4 col-lg-4">
                                                        <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 } ?>  
                            <?php
                            $detorden = consultas::get_datos("select * from v_compras_orden_detalle "
                                            . " where orden_cod = " . $_REQUEST['vcod'] . " order by orden_cod asc");
                            ?>
                            <!--GRILLA DETALLE-->
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="panel panel-primary">
                                        <div class="panel-heading">
                                            Detalles
                                        </div>
                                        <?php if (!empty($detorden)) { ?>
                                            <div class="table-responsive">
                                                <div class="panel-body">
                                                <table class="table table-bordered table-striped tablas">
                                                    <thead> 
                                                        <tr>
                                                            <th class="text-center">Descripción</th>
                                                            <th class="text-center">Precio</th>
                                                            <th class="text-center">Cantidad</th>
                                                            <th class="text-center">Subtotal</th>
                                                            <th class="text-center">Accion</th>
                                                        </tr>   
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($detorden as $detor) { ?>
                                                            <tr>
                                                                <td class="text-left"><?php echo $detor['articulo']; ?></td>
                                                                <td class="text-center"><?php echo number_format($detor['precio_unit'], 0, ',', '.'); ?></td>
                                                                <td class="text-center"><?php echo $detor['cantidad']; ?></td>
                                                                <td class="text-center"><?php echo number_format($detor['subtotal'], 0, ',', '.'); ?></td>
                                                                <td class="text-center">
                                                                    <?php if ($orden['orden_estado'] === 'PENDIENTE') { ?>
                                                                        <a onclick="borrar(<?php
                                                                        echo"'" . $detor['id_articulo'] . "_" .
                                                                        $_REQUEST['vcod'] . "_" .
                                                                        $detor['cod_depo'] . "_" .
                                                                        $detor['cantidad'] . "_" .
                                                                        $detor['art_descri'] . "'";
                                                                        ?>)"
                                                                           class="btn btn-sm btn-danger"
                                                                           rel="tooltip" data-title="Borrar"
                                                                           data-toggle="modal" data-target="#delete">
                                                                            <i class="fa fa-trash"></i>
                                                                        </a>
                                                                    <?php } ?>
                                                                </td>
                                                            </tr>
                                                        <?php } ?>
                                                    </tbody>
                                                </table>
                                               </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>
                            </div> 
                            <!--GRILLA DETALLE-->

                        </div>
                    </div>
                    <!-- /.FIN CUERPO -->
                    <!--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-->
                </div>
            </div>
        </div>
        <!--archivos js-->  
        <?php require '../../menu/js_lte.ctp'; ?>
        <!-- /.JS -->
        <script>

            function obtenerprecio() {
                var dat = $('#vart').val().split("_");
                if (parseInt($('#vart').val()) > 0) {
                    $("#vprecio").val(dat[1]);
                }
            }

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



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


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

            function estado(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'orden_control.php?vcod=' + dat[0] +
                        //'&vpres=' + dat[1] +
                        '&vpres=null' +
                        '&vprv=null' +
                        '&vusu=null' +
                        '&vsuc=null' +
                        '&vtotal=null' +
                        '&accion=2' +
                        '&pagina=ordenc_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea confirmar el registro  <i><strong>' + dat[0] + '</strong></i> ?');
            }

        </script>
        <!-- /.JS -->
    </body>
</html>
