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


<?= $this->section('content'); ?>
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4><?= $boxTitle ?> <span class="badge text-bg-success fw-lg">FINALIZADO</span></h4>
            </div>
            <div class="card-body">

                <?= view("themes/_commonPartialsBs/_alertBoxes") ?>

                <input type="hidden" id="id" name="id" value="<?= $envioEntity->id ?>">
                <input type="hidden" id="nextCaja" name="next_caja" value="<?= $envioEntity->nextCaja ?>">

                <div class="accordion accordion-bordered">
                    <div class="card accordion-item active mb-5">
                        <h4 class="accordion-header px-4 py-3">
                            <?= lang("Logistica.datosEnvio") ?>
                        </h4>

                        <div id="accordionDatosEnvioTip" class="accordion-collapse collapse show">
                            <div class="accordion-body px-4 py-3">
                                <div class="d-flex flex-row mb-3">

                                    <div class="col-sm-4 px-3">
                                        <label for="att" class="form-label">
                                            <?= lang("Logistica.att") ?>
                                        </label>
                                        <input readonly id="att" name="att" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('att', $envioEntity->att) ?>">
                                    </div>

                                    <div class="col-sm-6 px-3">
                                        <label for="direccion" class="form-label">
                                            <?= lang("Logistica.direccion") ?>
                                        </label>
                                        <input readonly id="direccion" name="direccion" tabindex="1" maxlength="50"
                                            class="form-control"
                                            value="<?= old('direccion', $envioEntity->direccion) ?>">
                                    </div>

                                    <div class="col-sm-2 px-3">
                                        <label for="ciudad" class="form-label">
                                            <?= lang("Logistica.ciudad") ?>
                                        </label>
                                        <input readonly id="ciudad" name="ciudad" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('ciudad', $envioEntity->ciudad) ?>">
                                    </div>

                                </div>

                                <div class="d-flex flex-row mb-3">

                                    <div class="col-sm-3 px-3">
                                        <label for="cp" class="form-label">
                                            <?= lang("Logistica.cp") ?>
                                        </label>
                                        <input readonly id="cp" name="cp" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('cp', $envioEntity->cp) ?>">
                                    </div>

                                    <div class="col-sm-3 px-3">
                                        <label for="pais" class="form-label">
                                            <?= lang("Logistica.pais") ?>
                                        </label>
                                        <input readonly id="pais" name="pais" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('pais', $envioEntity->pais) ?>">
                                    </div>

                                    <div class="col-sm-3 px-3">
                                        <label for="email" class="form-label">
                                            <?= lang("Logistica.email") ?>
                                        </label>
                                        <input readonly id="email" name="email" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('email', $envioEntity->email) ?>">
                                    </div>

                                    <div class="col-sm-3 px-3">
                                        <label for="telefono" class="form-label">
                                            <?= lang("Logistica.telefono") ?>
                                        </label>
                                        <input readonly id="telefono" name="telefono" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('telefono', $envioEntity->telefono) ?>">
                                    </div>
                                </div>

                                <div class="d-flex flex-row mb-3">
                                    <div class="col-sm-9 px-3">
                                        <label for="comentarios" class="form-label">
                                            <?= lang("Logistica.comentariosEnvio") ?>
                                        </label>
                                        <input id="comentarios" name="comentarios" tabindex="1" maxlength="50"
                                            class="form-control"
                                            value="<?= old('comentarios', $envioEntity->comentarios) ?>">
                                    </div>

                                    <div class="col-sm-3 px-3">
                                        <button id="guardarComentarios" name="guardar_comentarios" tabindex="1"
                                            class="btn btn-primary mt-4 w-100">
                                            <?= lang("Logistica.guardar") ?>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>

                <?php if ($envioEntity->finalizado == 0): ?>
                    <div class="accordion accordion-bordered">
                        <div class="card accordion-item active mb-5">
                            <h4 class="accordion-header px-4 py-3">
                                <?= lang("Logistica.addLineasEnvio") ?>
                            </h4>

                            <div id="accordionaddLineasEnvioTip" class="accordion-collapse collapse show">

                                <div class="d-flex flex-row mb-3">
                                    <div class="col-sm-12 px-3">
                                        <p><?= lang('Logistica.addLineasText') ?></p>
                                    </div>
                                </div>

                                <div class="d-flex flex-row mb-3">
                                    <div class="col-sm-6 px-3">
                                        <label for="buscadorPedidos" class="form-label">
                                            <?= lang("Logistica.buscadorPedidosTitle2") ?>
                                        </label>
                                        <select id="buscadorPedidos" name="buscador_pedidos" tabindex="1" maxlength="50"
                                            class="form-control select2bs2" style="width: 100%;">
                                        </select>
                                    </div>
                                    <div class="col-sm-2 px-3">
                                        <button id="btnAddLinea" name="btnBuscar" tabindex="1"
                                            class="btn btn-primary mt-4 w-100">
                                            <?= lang("Logistica.add") ?>
                                            <ti class="ti ti-circle-plus"></ti>
                                        </button>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                <?php endif; ?>


                <div class="accordion accordion-bordered">
                    <div class="card accordion-item active mb-5">
                        <h4 class="accordion-header px-4 py-3">
                            <?= lang("Logistica.lineasEnvio") ?>
                        </h4>

                        <div id="accordionDatosEnvioTip" class="accordion-collapse collapse show">
                            <div class="accordion-body px-4 py-3">
                                <div class="d-flex flex-row">
                                    <p><?= lang('Logistica.buttonsActions') ?></p>
                                </div>
                                <div class="d-flex flex-row mb-3">
                                    <div class="col-sm-2 px-3">
                                        <button id="btnSelectAll" name="btnSelectAll" tabindex="1"
                                            class="btn btn-primary w-100">
                                            <?= lang("Logistica.selectAll") ?>
                                            <i class="ti ti-select"></i>
                                        </button>
                                    </div>
                                    <?php if ($envioEntity->finalizado == 0): ?>
                                        <div class="col-sm-2 px-3">
                                            <button id="btnEliminarLineas" name="btnEliminarLineas" tabindex="1"
                                                class="btn btn-danger w-100">
                                                <?= lang("Logistica.eliminar") ?>
                                                <i class="ti ti-trash"></i>
                                            </button>
                                        </div>
                                    <?php endif; ?>
                                    <div class="col-sm-2 px-3">
                                        <button id="btnGenerarAlbaran" name="btnGenerarAlbaran" tabindex="1"
                                            class="btn btn-success w-100">
                                            <?= lang("Logistica.generarAlbaran") ?>
                                            <i class="ti ti-file-check"></i>
                                        </button>
                                    </div>
                                    <div class="col-sm-2 px-3">
                                        <button id="btnImprimirEtiquetas" name="btnImprimirEtiquetas" tabindex="1"
                                            class="btn btn-info w-100">
                                            <?= lang("Logistica.imprimirEtiquetas") ?>
                                            <i class="ti ti-printer"></i>
                                        </button>
                                    </div>

                                </div>
                                <div class="row mb-3">

                                    <table id="tableLineasEnvio" class="table table-striped table-hover w-100">
                                        <thead>
                                            <tr>
                                                <th></th>
                                                <th><?= lang("Logistica.pedido") ?></th>
                                                <th><?= lang("Logistica.presupuesto") ?></th>
                                                <th><?= lang("Logistica.titulo") ?></th>
                                                <th class="text-center" style="width: 10%;">
                                                    <?= lang("Logistica.unidadesEnvio") ?>
                                                </th>
                                                <th class="text-center" style="width: 10%;">
                                                    <?= lang("Logistica.unidadesEnviadas") ?>
                                                </th>
                                                <th class="text-center" style="width: 10%;">
                                                    <?= lang("Logistica.unidadesTotales") ?>
                                                </th>
                                                <th></th>
                                                <th></th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th colspan="10">
                                                    <div class="text-end">
                                                        <?= lang("Logistica.unidadesTotalesFooter") ?>
                                                        <span id="footer-unidades-envio"></span>
                                                    </div>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th colspan="10">
                                                    <div class="text-end">
                                                        <?= lang("Logistica.peso") ?>
                                                        <span id="footer-peso"></span>
                                                    </div>
                                                </th>
                                            </tr>
                                        </tfoot>

                                    </table>

                                    <div class="col-sm-2 px-3">
                                        <label for="cajas" class="form-label">
                                            <?= lang("Logistica.cajas") ?>
                                        </label>
                                        <input type="number" id="cajas" name="cajas" tabindex="1" maxlength="50"
                                            class="form-control" value="<?= old('cajas', $envioEntity->cajas) ?>">

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="accordion accordion-bordered mt-3 mb-5" id="accordioAlbaranes">

                        <div class="card accordion-item active">
                            <h2 class="accordion-header" id="headingAlbaranes">
                                <button type="button" class="accordion-button" data-bs-toggle="collapse"
                                    data-bs-target="#accordionAlbaranesTip" aria-expanded="false"
                                    aria-controls="accordionAlbaranesTip">
                                    <h3><?= lang("Pedidos.albaranes") ?></h3>
                                </button>
                            </h2>

                            <div id="accordionAlbaranesTip" class="accordion-collapse collapse show"
                                data-bs-parent="#accordioAlbaranes">
                                <div id="contenedorAlbaranes" class="accordion-body">


                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="accordion accordion-bordered">
                        <div class="card accordion-item active mb-5">
                            <h4 class="accordion-header px-4 py-3">
                                <?= lang("Logistica.acciones") ?>
                            </h4>

                            <div class="d-flex flex-row mb-3">
                                <div class="col-sm-3 px-3">
                                    <label for="codigoSeguimiento" class="form-label">
                                        <?= lang("Logistica.codigoSeguimiento") ?>
                                    </label>
                                    <input type="text" id="codigoSeguimiento" name="codigo_seguimiento" tabindex="1"
                                        maxlength="100" class="form-control"
                                        <?= ($envioEntity->finalizado == 0) ? "" : "readonly" ?>
                                        value="<?= old('codigo_seguimiento', $envioEntity->codigo_seguimiento) ?>">
                                </div>
                                <div class="col-sm-3 px-3">
                                    <label for="empresaMensajeria" class="form-label">
                                        <?= lang("Logistica.empresaMensajería") ?>
                                    </label>
                                    <?php if ($envioEntity->finalizado == 0): ?>
                                        <select id="empresaMensajeria" name="empresa_mensajeria" tabindex="1" maxlength="50"
                                            class="form-control select2bs2" style="width: 100%;">
                                            <?php if ($envioEntity->proveedor_id): ?>
                                                <option value="<?= $envioEntity->proveedor_id ?>" "selected">
                                                    <?= $envioEntity->proveedor_nombre ?>
                                                </option>
                                            <?php endif; ?>
                                        </select>
                                    <?php else: ?>
                                        <input type="text" id="empresaMensajeriaInput" name="empresa_mensajeria_input"
                                            tabindex="1" maxlength="100" class="form-control" readonly
                                            value="<?= old('empresa_mensajeria', $envioEntity->proveedor_nombre) ?>">
                                    <?php endif; ?>
                                </div>
                                <?php if ($envioEntity->finalizado == 0): ?>
                                    <div class="col-sm-3 px-3">
                                        <button id="finalizarEnvio" name="finalizar_envio" tabindex="1"
                                            class="btn btn-primary mt-4 w-100 btn-finalizar">
                                            <?= lang("Logistica.finalizarEnvio") ?>
                                            <ti class="ti ti-check"></ti>
                                        </button>
                                    </div>
                                    <div class="col-sm-3 px-3">
                                        <button id="finalizarEnvioYOTs" name="finalizar_envio_ots" tabindex="1"
                                            class="btn btn-primary mt-4 w-100 btn-finalizar">
                                            <?= lang("Logistica.finalizarEnvioYOTs") ?>
                                            <ti class="ti ti-checks"></ti>
                                        </button>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

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

<?= $this->section('css') ?>
<link rel="stylesheet" href="<?= site_url('themes/vuexy/vendor/libs/sweetalert2/sweetalert2.css') ?>" />
<link rel="stylesheet" href="https://cdn.datatables.net/rowreorder/1.4.1/css/rowReorder.dataTables.min.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="<?= site_url("/themes/vuexy/vendor/libs/flatpickr/flatpickr.css") ?>">

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

<?= $this->section('additionalExternalJs') ?>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/sweetalert2/sweetalert2.js') ?>"></script>
<script src="https://cdn.datatables.net/rowgroup/1.3.1/js/dataTables.rowGroup.min.js"></script>
<script type="module" src="<?= site_url("assets/js/safekat/pages/logistica/envioEdit.js") ?>"></script>
<?= $this->endSection() ?>