<?php use_helper( 'UrlFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>

<style type="text/css">
/*
#example_wrapper .row
{
    margin-right: 0;
    margin-left: 0;
}
table.dataTable tbody tr:hover 
{
   background-color: #DAE6FE !important;;
}
*/
.table td, .table th 
{
    /*padding: 8px;*/
    font-size: 13px;
}
th { white-space: nowrap; }

fieldset.scheduler-border 
{
    border: 1px solid #ccc !important;
    padding: 0 0.5em 0.5em 0.5em !important;
    margin: 0 0 0.5em 0 !important;
    -webkit-box-shadow: 0px 0px 0px 0px #000;
    box-shadow: 0px 0px 0px 0px #000;
}

legend.scheduler-border 
{
    font-size: 1em !important;
    font-weight: bold !important;
    text-align: left !important;
    width: auto;
    padding: 0 5px;
    border-bottom: none;
    margin-bottom: 0;    
}
</style>

<style type="text/css">
    .table td
    {
        vertical-align: middle;
    }
</style>

<script type="text/javascript">
guardarDevolucion = function()
{
	var b_Control = true;
     
    if( b_Control )
    {
        x = confirm( "Esta seguro de imprimir la Guia?" );
        if( x )
        {
            var strFormSerialize = $( '#form1' ).serialize();
            executeAjaxJson
            ( 
                "<?php echo buildUrl( 'transporte/generarGuiaRem' ) ?>", 
                strFormSerialize,
                function( o_json )
                {
                    unBlockContent( "form1" );
                    if( o_json.error )
                    {
                    	showAlertBootStrap( 'content_alert', 'alert-danger', o_json.mensaje_error );
                        //mostrarMensajeError(  );
                        return false;
                    }
                    window.open( o_json.enlace_pdf, '_blank', '' );
                    parent.realizarNuevaBusqueda();
                    parent.closeWindow();
                } 
            );
        }
    }
    return false;
}

realizarDevolucion = function()
{
	x = confirm( "Esta seguro de registar la Devolución?" );
    if( x )
    {
        $( "#form1" ).submit();
    }
}
</script>

<div class="shadow-sm p-3 mb-2 bg-white rounded card card-body">
    <div class="row">
        <div class="col mb-1">
            <h4 class="text-info text-nowrap mb-0">Nueva Devoluci&oacute;n</h4>
        </div>
    </div>
</div>

<form id="form1" name="form1" method="POST" onsubmit=""  enctype="multipart/form-data">
    <input type="hidden" name="Control" id="Control" value="1">
    <input type="hidden" name="OrdenId" id="IdProyecto" value="<?=$s_IdProyecto?>">

    <div class="marco">
        <div class="table-responsive">

            <table class="table table-borderless" style="margin-bottom: 0px; line-height: 0.5; padding: .50rem; ">
                <tbody>
                    <tr>
                        <th class="text-nowrap" scope="row">Proyecto:</th>
                        <td class="text-nowrap" style="color: #fc6d00;"><?=$o_VTA_PrsProyectosBN->getNombreProyecto()?></td>
                        <th class="text-nowrap" scope="row"><?=$o_VTA_PrsProyectosBN->getClienteTipoDocu()?>:</th>
                        <td class="text-nowrap"><?=$o_VTA_PrsProyectosBN->getClienteNroDocu()?></td>
                        <th class="text-nowrap" scope="row">Cliente:</th>
                        <td class="text-nowrap" ><?=$o_VTA_PrsProyectosBN->getClienteNombre();?></td>
                        <th class="text-nowrap" scope="row">Estado:</th>
                        <td class="text-nowrap" >PENDIENTE</td>
                    </tr>

                    <tr>
                        <th class="text-nowrap" scope="row"> Guia Transportista:&nbsp;</th>
                        <td class="text-nowrap" style="color: #fc6d00;" colspan="3" >
                            <div class="form-row mx-0">
                                <div class="col-md-3 mb-1">
                                    <input type="text" class="form-control form-control-sm text-center" name="TransGuiaSerie" id="GuiaSerieTrans" value="" placeholder="SERIE" autocomplete="off" >
                                </div>
                                <div class="col-md-9 mb-1">
                                    <input type="text" class="form-control form-control-sm text-center" name="TransGuiaNumero" id="GuiaNumeroTrans" value="" placeholder="NUMERO" autocomplete="off" >
                                </div>
                            </div>
                        </td>
                        <th class="text-nowrap" scope="row">Fecha Inicio Alq:</th>
                        <td class="text-nowrap" style="vertical-align: top" ><?=$o_VTA_PrsProyectosBN->getFechaInicioDespacho()?></td>
                        <th class="text-nowrap" scope="row">Fecha Devoluci&oacute;n:</th>
                        <td class="text-nowrap" style="vertical-align: top" >

                            <input type="text" class="form-control form-control-sm" name="FechaDevolucion" id="FechaDevolucion" size="12" style="text-align:center;" value="<?=date( "Y-m-d" )?>">
                            <script type="text/javascript">
                                $( "#FechaDevolucion" ).datepicker
                                (
                                    {
                                        dateFormat: "yy-mm-dd", changeYear:true, yearRange: "-1:+1", changeMonth: true,
                                        onClose: function ()
                                        {
                                            this.focus();
                                        },
                                        onSelect: function()
                                        {
                                        }
                                    }
                                );
                            </script>
                        </td>
                    </tr>

                    <tr>
                        <th class="text-nowrap" scope="row"> Guia Obra:&nbsp;</th>
                        <td class="text-nowrap" style="color: #fc6d00;" colspan="3" >
                            <div class="form-row mx-0">
                                <div class="col-md-3 mb-1">
                                    <input type="text" class="form-control form-control-sm text-center" name="ObraGuiaSerie" id="GuiaSerieObra" value="" placeholder="SERIE" autocomplete="off" >
                                </div>
                                <div class="col-md-9 mb-1">
                                    <input type="text" class="form-control form-control-sm text-center" name="ObraGuiaNumero" id="GuiaNumeroObra" value="" placeholder="NUMERO" autocomplete="off" >
                                </div>
                            </div>
                        </td>
                        <th class="text-nowrap" scope="row">Observaciones:</th>
                        <td class="text-nowrap" style="vertical-align: top"  colspan="3">
                            <textarea class="form-control" id="Obervaciones" name="Observaciones" rows="1"  ></textarea>
                        </td>

                    </tr>

                </tbody>
            </table>

        </div>
    </div>

    <div class="dropdown-divider"></div>

    <div id="content_alert" style=""></div>

    <div id="content_result_grid1">
        <div class="table-responsive text-right">
            <input type="button" class="btn btn-primary btn-sm mb-1" value="AGREGAR + " onclick="agregarLineaDetalle();">
            <table class="table table-sm table-bordered table-hover">
                <thead class="thead-light">
                    <tr>
                        <th width="25%" style="text-align: center;" scope="col"><b>C&oacute;digo - Descripci&oacute;n</b></th>
                        <th width="5%" style="text-align: center;" scope="col"><b>Despachado</b></th>
                        <th width="5%" style="text-align: center;" scope="col"><b>Por Devolver</b></th>
                        <th width="5%" style="text-align: center;" scope="col"><b>Cantidad</b></th>
                        <th width="5%" style="text-align: center;" scope="col"><b>Estado</b></th>
                        <th width="15%" style="text-align: center;" scope="col"><b>Comentario</b></th>
                        <th width="5%" style="text-align: center;" scope="col"><b>T Coment</b></th>
                        <th width="2%" style="text-align: center;" scope="col">&nbsp;</th>
                    </tr>
                </thead>

                <tbody id="content_detail">

                </tbody>
                
            </table>
        </div>
    </div>
    
    <div class="form-row mx-0" style="padding: 10px;" id="content_buttons">
        <div class="col-md-6 mb-1">
            <button type="button" class="btn btn-danger btn-lg btn-block" onclick="parent.closeWindow();">CANCELAR</button>
        </div>
        <div class="col-md-6 mb-1">
            <button type="button" class="btn btn-primary btn-lg btn-block" onclick="realizarDevolucion();">GUARDAR</button> <!-- AGREGAR CONFIRM -->
        </div>
    </div>
</form>

<table style="display: none;">
    <tbody id="content_detail_copia">
    <td>
        <select class="form-control form-control-sm" name="Producto[]" onchange="actualizarDatos( this )" >
            <option value=""> - SELECCIONE - </option>
            <?php foreach ( $a_SR_CotizacionDevolucionCab as $i_Key => $o_SR_CotizacionDevolucionCabBN ){ ?>
                <option value="<?=$o_SR_CotizacionDevolucionCabBN->getIdItem()?>" cantDespachado="<?=$o_SR_CotizacionDevolucionCabBN->getCantidadPedida()?>"
                        cantPorDevolver="<?=( $o_SR_CotizacionDevolucionCabBN->getCantidadPedida() - $o_SR_CotizacionDevolucionCabBN->getCantidadDevuelto() ) ?>"
                >
                    <?=$o_SR_CotizacionDevolucionCabBN->getIdItem()?> - <?=strtoupper( $o_SR_CotizacionDevolucionCabBN->getItemDescripcion() )?>
                </option>
            <?php } ?>
        </select>
    </td>
    <td>
        <input type="text" class="form-control form-control-sm text-center" name="Despachado[]" readonly >
    </td>
    <td>
        <input type="text" class="form-control form-control-sm text-center" name="PorDevolver[]" readonly >
    </td>
    <td>
        <input type="text" class="form-control form-control-sm text-center" name="Cantidad[]" autocomplete="off" >
    </td>
    <td>
        <select class="form-control form-control-sm" name="Estado[]" >
            <option value=""> - SELECCIONE - </option>
            <?php foreach ( $a_SR_EstadoDevolucion as $a_SR_EstadoDevolucionBN ){ ?>
                <?php
                $s_EstadoDev = $a_SR_EstadoDevolucionBN->getEstadoDevolucionId();
                ?>
                <option value="<?=$s_EstadoDev?>"> <?=strtoupper( $a_SR_EstadoDevolucionBN->getDescripcion() )?> </option>
            <?php } ?>
        </select>
    </td>
    <td>
        <input type="text" class="form-control form-control-sm text-center" name="Comentario[]" autocomplete="off" >
    </td>
    <td>
        <select class="form-control form-control-sm" name="TipoComentario[]" >
            <option value=""> - NINGUNO - </option>
            <option value="TIPO_1"> LEVE </option>
            <option value="TIPO_2"> MODERADO </option>
            <option value="TIPO_3"> FUERTE </option>
        </select>
    </td>
    <td style="text-align: center;">
        <img src="<?=getUrlImage( "btnDelete.gif" )?>" title="Eliminar" onclick="$( $( this ).closest( 'tr' ) ).remove();" style="cursor:pointer">
    </td>
    </tbody>
</table>

<script>
agregarLineaDetalle = function()
{
    $( "#content_detail" ).append( $( "#content_detail_copia" ).html() );
}

actualizarDatos = function( elm_sel )
{
    pr( $( elm_sel ).val() );
    if( $( elm_sel ).val() != "" )
    {
        var cantDespachado = $( elm_sel ).find( 'option:selected' ).attr( 'cantDespachado' );
        var PorDevolver = $( elm_sel ).find( 'option:selected' ).attr( "cantPorDevolver" );
        pr( PorDevolver );

        $( $( elm_sel ).closest( 'tr' ) ).find( "input[name='Despachado[]']" ).val( cantDespachado );
        $( $( elm_sel ).closest( 'tr' ) ).find( "input[name='PorDevolver[]']" ).val( PorDevolver );

        var item_id = $( $( elm_sel ).closest( 'tr' ) ).find( "input[name='IdItem[]']" ).val();
    }
}
</script>

<script type="text/javascript">
<?php if( $s_MensajeError ){ ?>
showAlertBootStrap( 'content_alert', 'alert-danger', "<?=$s_MensajeError?>" );
<?php } ?>

<?php if( $b_Control ){ ?>
parent.closeWindow();
parent.findControl();
<?php } ?>
</script>

    