<?= $this->include('themes/_commonPartialsBs/datatables') ?>
<?= $this->include("themes/_commonPartialsBs/select2bs5") ?>
<?= $this->include("themes/_commonPartialsBs/sweetalert") ?>
<?= $this->include('themes/_commonPartialsBs/_confirm2delete') ?>
<?=$this->extend('themes/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="addFacturaForm" class="card-body" method="post" action="<?= $formAction ?>">
				<?= csrf_field() ?>
                <?= view("themes/_commonPartialsBs/_alertBoxes") ?>
                <?= !empty($validation->getErrors()) ? $validation->listErrors("bootstrap_style") : "" ?>
                
                <?= view("themes/vuexy/form/facturas/_facturaCabeceraItems") ?>
                <?php if($facturaEntity->estado =='borrador') : ?>
                    <?= view("themes/vuexy/form/facturas/_addPedidosItems") ?>
                <?php endif; ?>

                <?= view("themes/vuexy/form/facturas/_facturaLineasItems") ?>
                <?php if((strpos($facturaEntity->numero, "REC ") !== 0) ) : ?>
                    <?= view("themes/vuexy/form/facturas/_pagosFacturasItems") ?>
                <?php endif; ?>
                <?php if($facturaEntity->estado !='borrador' && (strpos($facturaEntity->numero, "REC ") === 0) ) : ?>
                    <?= view("themes/vuexy/form/facturas/_rectificadaFacturasItems") ?>
                <?php endif; ?>
                <?= view("themes/vuexy/components/chat_internal_factura",data:["modelId" =>$facturaEntity->id,"type" => "factura"]) ?>

                <div class="container-fluid pt-4">
                    <?php if($facturaEntity->estado =='borrador') : ?>
                        <input type="button"
                                    class="btn btn-success float-start me-sm-3 me-1"
                                    id="validarFactura"
                                    name="validarFactura"
                                    value="<?= lang("Facturas.validarFactura") ?>"
                        />
                    <?php endif; ?>
                    
                    <?php if($facturaEntity->showDeleteButton == true && $facturaEntity->estado !='validada') : ?>
                        <input type="button"
                                    class="btn btn-danger float-start me-sm-3 me-1"
                                    id="borrarFactura"
                                    name="borrarFactura"
                                    value="<?= lang("Facturas.borrarFactura") ?>"
                        /> 
                    <?php endif; ?>
                    <?= anchor(
                        route_to("facturaToPdf", $facturaEntity->id),
                        lang("Facturas.imprimirFactura"),
                        [
                            "class" => "btn btn-info float-start me-sm-3 me-1",
                            "target" => "_blank"
                        ]
                    ) ?>
                    <?= anchor(
                            route_to("facturasList"),
                            lang("Basic.global.back"),
                            [
                                    "class" => "btn btn-secondary float-start"
                            ]
                    ) ?>
				</div><!-- /.card-footer -->
                <div class="container-fluid d-flex flex-md-row flex-column justify-content-between align-items-md-center gap-1 container-p-x py-3">
                    <div class="col-md-6">
                        <p>
                            <strong><?= lang("Presupuestos.created_by_at") ?></strong>
                            <span id="created_by"><?= $facturaEntity->created_by ?></span>, 
                            <span id="created_at"><?= $facturaEntity->created_at_footer ?></span>
                        </p>
                    </div>
                    <div class="col-md-6">
                        <p>
                            <strong><?= lang("Presupuestos.updated_by_at") ?></strong>
                            <span id="updated_by"><?= $facturaEntity->updated_by ?></span>, 
                            <span id="updated_at_footer"><?= $facturaEntity->updated_at_footer ?></span>
                        </p>
                    </div>
                </div>
			</form>
		</div><!-- //.card -->
    </div><!--//.col -->

    <?= view("themes/_commonPartialsBs/_modalConfirmDialog") ?>
    
</div><!--//.row --> 

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


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

$("#borrarFactura").on("click", function(){

    asyncConfirmDialog(
        "Borrar Factura",
        "¿Está seguro de borrar la factura? Esta acción no se puede deshacer.",
        deleteConfirmed, null)
});

function deleteConfirmed(){
    $.ajax({
        url: "<?= route_to("borrarFactura", $facturaEntity->id) ?>",
        type: 'GET',
        success: function(response){
            window.location.href = "<?= route_to("facturasList") ?>";
        }
    });
}

$("#validarFactura").on("click", function(){

asyncConfirmDialog(
    "Validar Factura",
    "¿Está seguro de pasar la factura a validada? Esta acción no se puede deshacer.",
    validatedConfirmed, null)
});

function validatedConfirmed(){
    $.ajax({
        url: "<?= route_to("validarFactura", $facturaEntity->id) ?>",
        type: 'GET',
        success: function(response){
            window.location.href = "<?= route_to("editarFactura", $facturaEntity->id) ?>";
        }
    });
}


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


<?=$this->section('css') ?>
    <link rel="stylesheet" href="<?= site_url("/themes/vuexy/vendor/libs/flatpickr/flatpickr.css") ?>">
    <link rel="stylesheet" href="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/buttons/buttons.bootstrap5.min.css") ?>">
    <link rel="stylesheet" href="<?= site_url('themes/vuexy/css/sk-datatables.css') ?>">
    <link rel="stylesheet" href="<?= site_url('themes/vuexy/css/datatables-editor/editor.dataTables.min.css') ?>">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/datetime/1.5.2/css/dataTables.dateTime.min.css">
<?=$this->endSection() ?>

<?= $this->section('additionalExternalJs') ?>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/flatpickr/flatpickr.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/buttons/dataTables.buttons.min.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/buttons/buttons.bootstrap5.min.js") ?>"></script>
    <script src="<?= site_url("themes/vuexy/vendor/libs/datatables-sk/plugins/select/dataTables.select.min.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/buttons/buttons.html5.min.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/buttons/buttons.print.min.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/jszip/jszip.min.js") ?>"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/pdfmake/pdfmake.min.js") ?>" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/pdfmake/vfs_fonts.js") ?>"></script>
    <script src="<?= site_url('themes/vuexy/js/datatables-editor/dataTables.editor.min.js') ?>"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/datetime/1.5.2/js/dataTables.dateTime.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

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


