<?= $this->include('themes/_commonPartialsBs/datatables') ?>
<?= $this->include("themes/_commonPartialsBs/select2bs5") ?>
<?= $this->include("themes/_commonPartialsBs/sweetalert") ?>
<?= $this->extend('themes/backend/vuexy/main/defaultlayout') ?>  


<?= $this->section("content") ?>

<div class="row">
    <div class="col-12">
		<div class="card card-info">
			<div class="card-header">
        		<h3 class="card-title"><?= $boxTitle ?? $pageTitle ?></h3>
			</div><!--//.card-header -->
            <form id="presupuestoForm" class="card-body" method="post" action="<?= $formAction ?>">
				<?= csrf_field() ?>
                <?= view("themes/_commonPartialsBs/_alertBoxes") ?>
                <?= !empty($validation->getErrors()) ? $validation->listErrors("bootstrap_style") : "" ?>
                <input type="hidden" name="tipo_impresion_id" id="tipo_impresion_id" class="form-control" value="<?= $tipo_impresion_id ?>"></input>
                <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosPresupuestoItems") ?>
                <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosLibroItems") ?>
                <?php if(str_contains($formAction,'edit')): ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosPresupuestoClienteItems") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_lineasPresupuestoItems") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_previewItems") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosServiciosItems") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosEnvios") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_comentariosItems") ?>
                    <?= view("themes/backend/vuexy/form/presupuestos/cosidotapablanda/_resumenPresupuestoItems") ?>
                <?php else: ?>
                    <input type="hidden" name="total_presupuesto" id="total_presupuesto" class="form-control" value="0.0"></input>
                <?php endif; ?>
                <div class="pt-4">
                    <input type="submit"
                        class="btn btn-primary float-start me-sm-3 me-1"
                        name="save"
                        id="saveForm"
                        value="<?= lang("Basic.global.Save") ?>"
                    />
                    <?php if(str_contains($formAction,'edit')): ?>
                        <?= anchor(route_to("presupuestoToPdf"), lang("Basic.global.Print"), ["class" => "btn btn-dark float-start me-sm-3 me-1",]) ?>
                        <?= anchor(route_to("#"), lang("Basic.global.Clone"), ["class" => "btn btn-info float-start me-sm-3 me-1",]) ?>
                    <?php endif; ?>
                    <?= anchor(route_to("cosidotapablandaList"), lang("Basic.global.Cancel"), ["class" => "btn btn-secondary float-start",]) ?>
                </div>
			</form>
        </div><!-- //.card -->
    </div><!--//.col -->
</div><!--//.row -->
    <?= view("themes/_commonPartialsBs/_modalConfirmDialog") ?>

<?= $this->endSection() ?>


<!------------------------------------------->
<!-- Código JS para los selects -->
<!------------------------------------------->
<?= $this->section("additionalInlineJs") ?>

    $(document).keypress(function (e) {
        var key = e.which;
        if(key == 13)  // the enter key code
        {   e.preventDefault()
            if($('#addressForm').hasClass('show')){
                $("#saveDireccionEnvio").click();

            }else{
                $('#saveForm').click();
            }
        }
    });  

    $('#clienteId').select2({
        allowClear: false,
        ajax: {
            url: '<?= route_to("menuItemsOfClientes") ?>',
            type: 'post',
            dataType: 'json',

            data: function (params) {
                return {
                    id: 'id',
                    text: 'nombre',
                    searchTerm: params.term,
                    <?= csrf_token() ?? "token" ?> : <?= csrf_token() ?>v
                };
            },
            delay: 60,
            processResults: function (response) {
                yeniden(response.<?= csrf_token() ?>);
                return {
                    results: response.menu
                };
            },
            cache: true
        }
    });

    /*******************************
    * Inicialización de los Select2
    *******************************/
    $('#incRei').select2({
        allowClear: false,
    });

    $('#paisId').select2({
        allowClear: false,
    });


    
<?= $this->endSection() ?>



<!------------------------------------------->
<!-- Acciones antes de submit... -->
<!------------------------------------------->
const url = window.location.href;
const url_parts = url.split('/');

<?php if(str_contains($formAction,'edit')): ?>
<?= $this->section("additionalInlineJs") ?>

if(url_parts[url_parts.length-2] == 'edit'){
    id = url_parts[url_parts.length-1];
}
else{
    id = -1;
}

$('#presupuestoForm').on( "submit", function( event ) {
    event.preventDefault();
    save_servicios();
    save_datos_envios();    

    
    $.when(fill_bbdd_from_lp(id).then(function (data, textStatus, jqXHR){
        generateCompJSON()
        form = $('#presupuestoForm').serialize() 
        form += getValuesResumenForm()
        
        $.ajax({
            type: "POST",
            url: "<?php  echo $formAction; ?>",
            data: form,
            success: function (data) {
                yeniden(data.<?= csrf_token() ?>)
                popSuccessAlert(data.mensaje)
            }
            }).fail(function (jqXHR, textStatus, error) {
                // Handle error here
                console.log(jqXHR)
            });;
    }))
    
     return false;  //stop the actual form post !important!
});


<?= $this->endSection() ?>
<?php endif; ?>



<?=$this->section('css') ?>

    <link rel="stylesheet" href="<?= site_url('themes/vuexy/css/datatables-editor/editor.dataTables.min.css') ?>">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.bootstrap5.min.css">
    <link rel="stylesheet" href="<?= site_url('themes/vuexy/css/sk-datatables.css') ?>">

<?=$this->endSection() ?>

<?= $this->section('additionalExternalJs') ?>
    <script src="https://cdn.datatables.net/buttons/2.2.3/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.bootstrap5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.print.min.js"></script>
    <script src="https://cdn.datatables.net/select/1.7.0/js/dataTables.select.min.js"></script>
    <script src="<?= site_url('themes/vuexy/js/datatables-editor/dataTables.editor.min.js') ?>"></script>    
    <script src="<?= site_url('themes/vuexy/vendor/libs/autosize/autosize.js') ?>"></script>
    <?php if(str_contains($formAction,'edit')): ?>
        <script src="<?= site_url('js_loader/comparadorCosidoTapaBlanda_js') ?>"></script>
        <script src="<?= site_url('js_loader/previsualizador_js') ?>"></script>
        <script src="<?= site_url('themes/vuexy/vendor/libs/two/two.js') ?>"></script>
    <?php endif; ?>
<?=$this->endSection() ?>
