@extends('header_new')
<style>
    .text-left {
        text-align: left;
    }

    .text-right {
        text-align: right;
    }

    .text-center {
        text-align: center;
    }
</style>
@section('main')


<main class="container-fluid mt-1 pt-3">
    <!-- SECCION DATOS DEL CLIENTE -->
    <div class="container-fluid contenedores-navegador" id="seccion_datos_cliente">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-10 my-4">
                <div class='text-center my-2 pt-1 pt-xxl-5'>
                    <h5 id="tituloMod">
                        @traducir('Generar archivo de Pedidos de facturación para Tango', 'Generar archivo de Pedidos de facturación para Tango')
                    </h5>
                </div>

                <div class="row form-group pt-lg-1">
                    <div class="col-12 col-lg-6 form-group pt-lg-1 pt-sm-6 text-left">
                        <span
                            class="text-fidelitas fst-italic text-center"><b>@traducir('PEDIDOS PENDIENTES', 'PEDIDOS PENDIENTES'):</b>
                            @traducir('Seleccione los pedidos que desea pasar a Tango', 'Seleccione los pedidos que desea pasar a Tango')</span>
                    </div>
                    <div class="col-12 col-lg-6 form-group pt-lg-1 pt-sm-6 text-right">
                        <button type="button" class="btn btn-primary btn-danger"
                            id="btn-hacer-pedidos">@traducir('Generar archivo con pedidos seleccionados', 'Generar archivo con pedidos seleccionados')</button>
                    </div>

                </div>



                <div class="col-12 col-lg-12 form-group pt-lg-1 pt-sm-12">
                    <form id="formulario_archivo">
                        {{ csrf_field() }}
                        <table class="table table-condensed table-striped table-hover" id="tablaPedidos">
                            <thead>
                                <tr>
                                    <th class="text-fidelitas fst-italic text-center">
                                        @traducir('Seleccione', 'Seleccione')<br><input type="Checkbox" name="todos"
                                            id="todos" value="1"></th>
                                    <th class="text-fidelitas fst-italic text-right" width='8%'>
                                        @traducir('Nro. Pedido', 'Nro. Pedido')</th>
                                    <th class="text-fidelitas fst-italic text-right" width='8%'>
                                        @traducir('Cod. Cliente', 'Cod. Cliente')</th>
                                    <th class="text-fidelitas fst-italic text-left" width='30%'>
                                        @traducir('Razón Social', 'Razón Social')</th>
                                    <th class="text-fidelitas fst-italic text-left" width='30%'>
                                        @traducir('Detalle', 'Detalle')</th>
                                    <th class="text-fidelitas fst-italic text-right" width='8%'>@traducir('Neto', 'Neto')
                                    </th>
                                    <th class="text-fidelitas fst-italic text-right" width='8%'>
                                        @traducir('Total', 'Total')</th>
                                </tr>
                            </thead>
                            @foreach ($facturas as $key => $fact)
                                <tr>
                                    <td class="text-center"><input type="Checkbox" name="pedido" class="chk"
                                            value="{{$fact->fac_nro_pedido}}"></td>
                                    <td class="text-right">{{$fact->fac_nro_pedido}}</td>
                                    <td class="text-right">{{$fact->cli_codigo}}</td>
                                    <td class="text-left">{{$fact->nombre_cliente}}</td>
                                    <td>

                                        @foreach ($detalle as $key => $det)
                                            @if ($det->fac_nro_pedido == $fact->fac_nro_pedido)
                                                <div class='row'>
                                                    <div class="col-md-12" style="font-size: 11px; font-weight: bolder;">
                                                        {{$det->prod_texto}}</div>
                                                </div>
                                                <div class='row'>
                                                    <div class="col-md-2" style="font-size: 11px;">Cant.</div>
                                                    <div class="col-md-2" style="font-size: 11px;">Neto Uni.</div>
                                                    <div class="col-md-2" style="font-size: 11px;">Neto Prod.</div>
                                                    <div class="col-md-2" style="font-size: 11px;">% Desc.</div>
                                                    <div class="col-md-2" style="font-size: 11px;">Neto Total</div>
                                                </div>
                                                <div class='row'>
                                                    <div class="col-md-2" style="font-size: 11px;">
                                                        {{number_format($det->det_cantidad, 0)}}</div>
                                                    <div class="col-md-2" style="font-size: 11px;">
                                                        {{number_format($det->det_neto_uni, 2)}}</div>
                                                    <div class="col-md-2" style="font-size: 11px;">
                                                        {{number_format($det->det_neto, 2)}}</div>
                                                    <div class="col-md-2" style="font-size: 11px;">
                                                        {{number_format($det->det_porbon, 2)}}</div>
                                                    <div class="col-md-2" style="font-size: 11px;">
                                                        {{number_format($det->det_importe, 2)}}</div>
                                                </div>
                                            @endif
                                        @endforeach
                                    </td>
                                    <td class="text-right">{{number_format($fact->fac_neto, 2)}}</td>
                                    <td class="text-right">{{number_format($fact->fac_total, 2)}}</td>
                                </tr>
                            @endforeach
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
</main>



@endsection

@section('funcionesJs')
<script type="text/javascript" language="javascript">

    $('#todos').on('click', function () {
        $.each($('.chk'), function (i, el) {
            $(this).prop('checked', $('#todos').prop('checked'));
        });
    });

    $('#tablaPedidos').DataTable({
        "pagingType": "full_numbers",
        "dom": "if<t>lp",
        "ordering": true,
        "columns": [{ "orderable": false }, null, null, null, null, null, null],
        "searching": true,
        "order": [[1, "asc"]],
        "info": true,
        "lengthMenu": [[-1], ["Todos"]],
        "pageLength": -1,
        "language": {
            "lengthMenu": "Mostrar _MENU_ registros por página",
            "zeroRecords": "No se encontraron resultados",
            //"info": "Mostrando página _PAGE_ de _PAGES_",
            //"info": "Total de Registros: _MAX_", 
            "info": "Mostrando  _START_  de  _END_ - <b>Total:</b> _TOTAL_ registros",
            "infoEmpty": "No hay registros disponibles",
            "infoFiltered": "(filtered from _MAX_ total records)",
            "sSearch": "Buscar",
            "oPaginate": {
                "sFirst": "Primera",
                "sPrevious": "Anterior",
                "sNext": "Siguiente",
                "sLast": "Última"
            },
        }
    });


    //$(document).on('submit',"#formulario_archivo",function(e){
    $(document).on('click', "#btn-hacer-pedidos", function (e) {
        e.preventDefault();
        var cant = 0;
        $.each($('.chk'), function (i, el) {
            if ($(this).prop('checked')) {
                cant++;
            }
        });
        if (cant == 0) {
            alert("No ha seleccionado ningún pedido")
        }
        else {

            $.ajax({
                method: "POST",
                url: "/fact/genera-archivo-tango",
                data: $('#formulario_archivo').serialize(),

                beforeSend: function () {
                    $('#loading').removeClass('hidden').show();
                },
                success: function (data) {
                    alert('Se ha generado el archivo correctamente');
                    location.reload();

                },
                error: function (err) {
                    alert('No se ha podido generar el archivo');
                    location.reload();

                }
            });

        }

    }); 
</script>
@endsection