<?php

use Core\Helpers\Moneda;

?>
<div class="container" id="appPresupuestoCrear" v-cloak>
    <input type="hidden" value="<?= $cliente['cli_id'] ?>" id="id_cliente_fac">
    <input type="hidden" id="ruta_documento" value="presupuesto">
    <div class="page-inner">
        <div class="row">
            <div class="col">
                <div class="d-flex justify-content-between">
                    <div class="col-5 pl-0 row-card-no-pd" style="margin-bottom: 0; padding-top: 0; padding-bottom: 0">
                        <div class="list-group list-group-messages list-group-flush">
                            <div class="list-group-item unread">
                                <div class="list-group-item-figure">
                                    <div class="avatar">
                                        <span class="avatar-title rounded-circle border border-white bg-info"><?= strtoupper($cliente['cli_razon_social'][0]) ?></span>
                                    </div>
                                </div>
                                <div class="list-group-item-body pl-3 pl-md-4">
                                    <div class="row">
                                        <div class="col-12 col-lg-10" id="cabecera_cliente">
                                            <h4 class="list-group-item-title">
                                                <?= $cliente['cli_razon_social'] ?> <span class="text-info pl-2"><?= $cliente['cli_dni'] ?></span>
                                                <a href="#" data-toggle="modal" data-target="#modal_buscar_cliente_generico"><span class="fa fa-edit"></span></a>
                                            </h4>
                                        </div>
                                        <div class="col-12 col-lg-10" id="cabecera_cliente">
                                            <h5 class="list-group-item-title text-warning">
                                                VENTAS - NUEVO PRESUPUESTO
                                                <div class="<?= ($doc_pendientes > 1) ? 'text-danger' : 'text-dark' ?>">
                                                    <small>Pendientes: <?= $doc_pendientes ?></small>
                                                    <?php foreach ($total_doc_pendientes['divisas_totales'] as $row) : ?>
                                                        <small><?= Moneda::moneda($row['total'], $row['div_locale'], $row['div_simbolo']); ?></small>
                                                    <?php endforeach; ?>
                                                    <?php if ($cliente['grup_id']) : ?>
                                                        <small> Venta Al <?= $cliente['grup_nombre'] ?> <?= $cliente['grup_descuento'] ?>%</small>
                                                    <?php endif; ?>
                                                </div>
                                            </h5>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-7 p-0">
                        <div class="row">
                            <div class="col-3">
                                <div class="form-group">
                                    <label for="fecha">Fecha</label>
                                    <input type="date" id="fecha" class="form-control" value="<?= date('Y-m-d') ?>">
                                </div>
                            </div>
                            <div class="col-3">
                                <div class="form-group">
                                    <label for="divisa">Divisa</label>
                                    <select id="divisa" ref="divisa_predeterminada" @change="predeterminarDivisa" class="form-control">
                                        <?php foreach ($divisas as $row) : ?>
                                            <option value="<?= $row['div_id'] ?>" <?= ($row['div_predeterminado'] == 1) ? 'selected' : '' ?>><?= $row['div_nombre'] ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-3">
                                <div class="form-group">
                                    <label for="sucursal">Sucursal</label>
                                    <select id="sucursal" ref="sucursal_predeterminada" class="form-control" @click.prevent="obtenerSeriesAlmacenesEmpleado()" v-model="sucursals" :disabled="disabled">
                                        <option disabled value="">Seleccione un elemento</option>
                                        <option v-for="row in sucursales_empleado" :value="row.su_id">{{row.su_nombre}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-3">
                                <div class="form-group">
                                    <label for="serie">Serie</label>
                                    <select id="serie" ref="serie_predeterminada" class="form-control" v-model="serie_predeterminada">
                                        <option disabled value="">Seleccione un elemento</option>
                                        <option v-for="row in series_empleado" :value="row.ser_id">{{row.ser_descripcion}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-3">
                                <div class="form-group">
                                    <label for="almacen">Almacén</label>
                                    <select id="almacen" ref="almacen_predeterminado" class="form-control" v-model="almacen_predeterminado" @change="cambiarAlmacen">
                                        <option disabled value="">Seleccione un elemento</option>
                                        <option v-for="row in almacenes" :value="row.alm_id">{{row.alm_nombre}}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-12 mt-3">
                <nav>
                    <div class="nav nav-tabs" role="tablist">
                        <a class="nav-item nav-link active" data-toggle="tab" href="#nav-lista" role="tab" aria-controls="nav-home" aria-selected="true">Productos <span class="badge badge-info ml-2">{{ articulosTotal }}</span></a>
                        <a class="nav-item nav-link" data-toggle="tab" href="#nav-detalles" role="tab" aria-controls="nav-profile" aria-selected="false">Detalles</a>
                    </div>
                </nav>
                <div class="tab-content mt-2">
                    <div class="tab-pane fade show active" id="nav-lista" role="tabpanel" aria-labelledby="nav-home-tab">
                        <div class="row row-card-no-pd">
                            <div class="col-12 mt-2">
                                <div class="col-12 mt-2 mb-2" style="padding-left: 0px;">
                                    <div class="d-flex justify-content-between">
                                        <div class="input-group mr-3 mr-md-2">
                                            <div class="suiche">
                                                <label for="" class="etiqueta icon-preview"><i class="fas fa-barcode"></i></label>
                                                <input type="checkbox" v-model="toggleBarcode" id="switch" @change="focusBuuscar">
                                                <label for="switch" class="lbl"></label>
                                            </div>
                                            <div class="col pl-0">
                                                <div class="input-icon">
                                                    <span class="input-icon-addon">
                                                        <i :class="'fa fa-search'"></i>
                                                    </span>
                                                    <input type="text" id="buscador" class="form-control" placeholder="buscar productos" @input="buscarProducto" @keydown="seleccionarProducto">
                                                </div>
                                            </div>
                                        </div>
                                        <label class="selectgroup-item mr-3 mr-md-2">
                                            <input type="checkbox" class="selectgroup-input" v-model="chequear_stock">
                                            <span class="selectgroup-button btn btn-sm btn-round" style="max-height: 2rem; padding: 0.25rem 1rem; font-size: 12px;">Chequear Stock</span>
                                        </label>
                                        <button type="button" class="btn btn-primary" disabled v-if="cargando">
                                            <i class="loader loader-info"></i>
                                        </button>
                                        <button type="button" class="btn btn-info btn-sm btn-round" v-else @click="guardarFactura" style="max-height: 2rem;">Guardar</button>
                                    </div>
                                </div>
                                <div class="scroll" style="height: 300px;width: 100%;padding: 0;margin: 0;overflow-y: scroll;overflow-x: hidden;">
                                    <div class="table-responsive">
                                        <table class="bg-white table-bordered-bd-black" cellspacing="1" cellpadding="1" width="100%" tabindex="2">
                                            <thead>
                                                <tr class="text-center">
                                                    <th></th>
                                                    <th>Código</th>
                                                    <th>Artículo</th>
                                                    <th>Precio</th>
                                                    <th>Cantidad</th>
                                                    <th>% Dcto.</th>
                                                    <th>Neto</th>
                                                    <th>IVA</th>
                                                    <th>Total</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr v-for="row in carrito" class="text-right">
                                                    <td class="text-center">
                                                        <a href="#" @click.prevent="eliminarProducto(row.unique)">
                                                            <i class="fa fa-trash text-danger"></i>
                                                        </a>
                                                    </td>
                                                    <td class="text-left">{{ row.codigo }}</td>
                                                    <td><input type="text" class="form-control text-left" style="border-color: #bbb; background-color:#fff;" v-model="row.nombre" @keyup.prevent="actualizarDescripcion(row.unique, $event)"></td>
                                                    <td width="13%"><input type="text" class="form-control text-right" style="border-color: #bbb; background-color:#fff;" v-model="row.precio" v-money="money" @keyup.prevent="actualizarPrecio(row.unique, $event)"></td>
                                                    <td width="7%"><input type="text" class="form-control text-right" style="border-color: #bbb; background-color:#fff;" v-model="row.cantidad" @keyup.prevent="actualizarCantidad(row.id, $event)"></td>
                                                    <td width="7%"><input type="text" class="form-control text-right" style="border-color: #bbb; background-color:#fff;" v-model="row.descuento" @keyup.prevent="aplicarDescuentoProducto(row.unique, $event)"></td>
                                                    <td width="12%">{{ moneda(row.neto, money.locale, money.prefix, money.precision) }}</td>
                                                    <td width="7%">
                                                        <select id="impuestos" class="form-control text-center input-border-bottom" v-model="row.id_iva" @change="aplicarNuevoIva(row.unique, row.id_iva)">
                                                            <?php foreach ($impuestos as $row) : ?>
                                                                <option value="<?= $row['imp_id'] ?>"><?= $row['imp_descripcion'] ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                    <td width="12%" class="text-right">{{ moneda(row.total, money.locale, money.prefix, money.precision) }}</td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row row-card-no-pd">
                            <div class="col-12">
                                <div class="table-responsive" style="padding-left: 30%;">
                                    <table class="bg-white table-bordered-bd-black" cellspacing="1" cellpadding="2" width="100%" tabindex="2">
                                        <thead>
                                            <tr class="text-right">
                                                <th>Subtotal</th>
                                                <th>% Dcto.</th>
                                                <th>Neto</th>
                                                <th>IVA</th>
                                                <th>Total</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr class="text-right">
                                                <td width="20%">{{ moneda(subtotal, money.locale, money.prefix, money.precision) }}</td>
                                                <td width="20%"><input type="text" class="form-control text-right" style="border-color: #bbb; background-color:#fff;" v-model="descuento" @keyup.prevent="aplicarDescuentoAdicional"></td>
                                                <td width="20%">{{ moneda(neto, money.locale, money.prefix, money.precision) }}</td>
                                                <td width="20%">{{ iva }}</td>
                                                <td width="20%">{{ moneda(total, money.locale, money.prefix, money.precision) }}</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3 row-card-no-pd">
                            <div class="col">
                                <div class="form-group form-group-default">
                                    <label>Observaciones</label>
                                    <textarea class="form-control" v-model="observaciones" rows="3"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane" id="nav-detalles" role="tabpanel" aria-labelledby="nav-detalles-tab">
                        <form action="#" autocomplete="off" @submit.prevent="actualizarDetalles">
                            <div class="card">
                                <div class="card-body">
                                    <div class="card-category"> <i class="fa fa-user"></i> Cliente</div>
                                    <div class="separator-solid"></div>
                                    <div class="form-row">
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_tipodoc">Tipo de documento:</label>
                                                <select id="cli_tipodoc" class="form-control">
                                                    <?php foreach ($tipos_de_documentos as $row) : ?>
                                                        <option value="<?= $row['tipodoc_id'] ?>" <?= ($cliente['tipodoc_codigo'] == $row['tipodoc_id']) ? 'selected' : '' ?>><?= $row['tipodoc_codigo']; ?></option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_dni"># Documento:</label>
                                                <input type="text" id="cli_dni" class="form-control" value="<?= $cliente['cli_dni'] ?>">
                                            </div>
                                        </div>
                                        <div class="col-sm-6">
                                            <div class="form-group form-group-default">
                                                <label for="cli_razon_social">Razón social:</label>
                                                <input type="text" id="cli_razon_social" class="form-control" value="<?= $cliente['cli_razon_social'] ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-row">
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_pais">País:</label>
                                                <select id="cli_pais" class="form-control">
                                                    <?php foreach ($paises as $row) : ?>
                                                        <option value="<?= $row['pa_id'] ?>" <?= ($cliente['cli_paisid'] == $row['pa_id']) ? 'selected' : '' ?>><?= $row['pa_nombre']; ?></option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_estado">Estado:</label>
                                                <input type="text" id="cli_estado" class="form-control" value="<?= $cliente['cli_estado'] ?>">
                                            </div>
                                        </div>
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_ciudad">Ciudad:</label>
                                                <input type="text" id="cli_ciudad" class="form-control" value="<?= $cliente['cli_ciudad'] ?>">
                                            </div>
                                        </div>
                                        <div class="col-sm-3">
                                            <div class="form-group form-group-default">
                                                <label for="cli_codigopostal">Código Postal:</label>
                                                <input type="text" id="cli_codigopostal" class="form-control" value="<?= $cliente['cli_codpostal'] ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-row">
                                        <div class="col">
                                            <div class="form-group form-group-default">
                                                <label for="cli_direccion">Dirección:</label>
                                                <input type="text" id="cli_direccion" class="form-control" value="<?= $cliente['cli_direccion'] ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-row">
                                        <div class="col-12 text-right">
                                            <button type="button" class="btn btn-info btn-border btn-round btn-sm" disabled v-if="cargando">
                                                <span class="btn-label"><i class="loader loader-info"></i></span>
                                            </button>
                                            <button type="submit" class="btn btn-info btn-border btn-round btn-sm" v-else>
                                                <span class="btn-label"><i class="fa fa-edit"></i></span> Actualizar
                                            </button>
                                        </div>
                                    </div>
                                    <div class="card-category"> <i class="fa fa-user-tie"></i> Empleado</div>
                                    <div class="separator-solid"></div>
                                    <div class="form-row">
                                        <div class="col-sm-6">
                                            <label for="empleado">Empleado</label>
                                            <input type="text" id="empleado" class="form-control" disabled value="<?= $_SESSION['user_data']['einfo_apellidos']; ?> - <?= $_SESSION['user_data']['einfo_email']; ?>">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" href="<?= asset('assets/buscadorProductos.css') ?>">
<script src="<?= ruta_base() ?>public/assets/v-money.js"></script>
<script src="<?= ruta_base() ?>public/assets/underscore-min.js"></script>
<script src="<?= ruta_base() ?>public/assetsvuejs/presupuesto/crear.js"></script>
<?php include 'app/notasdeentrega/vista/guardar_cambiar_cliente.php' ?>