@extends('layouts.app')

@section('content')

<div class="breadcomb-area">

    <div class="modals-default-notika">
        <div class="modal-inner-pro">

            @include('layouts.alertas')
            
        <form class="form-horizontal" method="POST" action="{{ route('PreVentas.Update') }}" enctype="multipart/form-data">
            @csrf
 
            @foreach($data as $value)
 
                <div class="title col-sm-12">
                    <h4>DATOS DEL CLIENTE</h4>
                </div>

                @php
                    $cliente = DB::table('clientes')->where('id', $value->cliente_id)->get()->all();
                @endphp


               <div class="grid-1-center clientes_show">

                    <!-- AQUI CARGAMOS LOS DATOS DEL CLIENTE-->
                    @foreach($cliente as $value_cliente)
                        
                        <div class="col-9"  id="contenedor_busqueda_cliente">
                            <input type="hidden" name="cliente_id" id="cliente_id" value="{{$value_cliente->id}}">
                            <div class="grid-1-noGutter-equalHeight">
                                <div class="campos_edit_cotizacion_user_show col-12">
                                     <h5>Nombre cliente:</h5>
                                     <p>{{$value_cliente->nombre}}</p>
                                </div>

                                <div class="campos_edit_cotizacion_user_show col-7_sm-12">

                                    @php 
                                        $tipo_documento = DB::table('tipo_documentos')->where('id', $value_cliente->id_tipo_documento)->get();
                                    @endphp

                                    @foreach($tipo_documento as $value_documento)
                                        <h6><i class="fas fa-address-card"></i> Documento identidad: </h6>
                                        <p> {{$value_documento->nombre}} / {{$value_cliente->documento }}</p>
                                    @endforeach
                                </div>

                                <div class="campos_edit_cotizacion_user_show col-5_sm-12">
                                    <h6>Correo:</h6>
                                    <p>{{$value_cliente->email}}</p>
                                </div>


                                <div class="campos_edit_cotizacion_user_show col-12_sm-12">
                                   <h6><i class="fas fa-map-marker-alt"></i> Dirección:</h6>
                                   <p>{{$value_cliente->direccion}}</p>
                                </div>

                                <div class="campos_edit_cotizacion_user_show col-4_sm-12">
                                   <h6><i class="fas fa-phone"></i> Telefono:</h6>
                                   <p>{{$value_cliente->tel}}</p>
                                </div>

                                <div class="campos_edit_cotizacion_user_show col-4_sm-12">
                                   <h6><i class="fas fa-mobile"></i> Movil:</h6>
                                   <p>{{$value_cliente->cel}}</p>
                                </div>

                                <div class="campos_edit_cotizacion_user_show col-4_sm-12">
                                   <h6><i class="fab fa-whatsapp"></i> WhatsApp:</h6>
                                   <p>{{$value_cliente->whatsapp}}</p>
                                </div>
<!--

                                <div class="linea"></div>
                                 @php 
                                    $tipo_impuesto = DB::table('tipo_impuestos')->where('id', $value_cliente->id_tipo_impuesto)->get();
                                @endphp

                                @foreach($tipo_impuesto as $value_impuesto)
                                    <div class="campos_edit_cotizacion_user_show col-6_sm-12">
                                        <h6><i class="fas fa-file-invoice-dollar"></i></i>Tipo de impuesto:</h6>
                                        <p> {{$value_impuesto->nombre}}</p>
                                    </div>

                                    <div class="campos_edit_cotizacion_user_show col-3_sm-12">
                                        <h6><i class="fas fa-file-invoice-dollar"></i></i>Secuencia:</h6>
                                        <p> {{$value_impuesto->secuencia}}</p>
                                    </div>

                                    <div class="campos_edit_cotizacion_user_show col-3_sm-12">
                                        <h6><i class="fas fa-file-invoice-dollar"></i></i>Inicio ncf:</h6>
                                        <p> {{$value_impuesto->inicio_ncf}}</p>
                                    </div>


                                    <div class="campos_edit_cotizacion_user_show col-12_sm-12">
                                        <h6>Descripción:</h6>
                                        <p> {{$value_impuesto->descripcion}}</p>
                                    </div>

                                    
                                @endforeach-->

                            </div>
                             
                        </div> 
                    @endforeach

                    <div class="new_user_option col-3-center">
                        <h5>Seleccione el cliente:</h5>

                        <!-- AQUI CARGAMOS EL SELECT DE LOS CLIENTES -->
                        <input list="brow_cliente" class="form-control" id="cliente">
                        @php $clientes = DB::table('clientes')->get()->all(); @endphp
                        <datalist id="brow_cliente">
                            @foreach($clientes as $value_cliente)
                                <option data-cliente="{{$value_cliente->id}}" class="opcion_evento_create">
                                    <h6>{{$value_cliente->nombre}} / {{$value_cliente->email}}</h6>
                                </option>
                            @endforeach
                        </datalist>

                        <div data-toggle="modal" data-target="#addClienteModal" data-placement="left" class="create_user">
                             <i class="far fa-user"></i> Crear Nuevo Usuario
                        </div>

                    </div>
               </div>


               <!-- PRESENTAMOS LA DATA DE LA COTIZACIÓN -->

                <div class="title col-sm-12">
                    <h4>DATOS DE LA COTIZACIÓN</h4>
                </div>

                <!-- AQUI PRESENTAMOS LOS DATOS DE LA COTIZACIÓN -->
                <div class="row">
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                        <div class="invoice-hs wt-inv sm-res-mg-t-30 tb-res-mg-t-30 tb-res-mg-t-0">
                            <span>Cotización #</span>
                            <h2>{{$value->numero_orden}}</h2> 
                            <input type="hidden" name="numero_cotizacion" value="{{$value->numero_orden}}">
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                        <div class="invoice-hs date-inv sm-res-mg-t-30 tb-res-mg-t-30 tb-res-mg-t-0">
                            <span>Fecha</span>
                            @php $todayDate = date("d-m-Y"); @endphp
                            <h2>{{ $todayDate }}</h2>   
                            <input type="hidden" name="date" value="{{ $todayDate }}">                         
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                        <div class="invoice-hs">
                            <span>Vendedor</span>                            
                            <h2>{{ auth()->user()->name }}</h2>
                            <input type="hidden" name="vendedor_id" value="{{ auth()->user()->id }}"> 
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                        <div class="invoice-hs gdt-inv sm-res-mg-t-30 tb-res-mg-t-30 tb-res-mg-t-0">
                            <span>Total General</span>
                            <h2 id="total_venta">{{$value->total}}</h2>

                            <!-- aqui debemos cargar el total -->
                            <input type="hidden" id="total_ventas" name="total_venta" value="{{$value->total}}">
                        </div>
                    </div>
                </div>

                <!-- AQUI ESTA EL FORMULARIO PARA REGISTRAR UN NUEVO PROUCTO Y AGREGARLO A LA COTIZACION -->
                <div class="row formulario_cotizacion">
                    
                    <div class="col-lg-5 col-sm-5 col-md-5 col-xs-12">
                        <label>Seleccione el producto</label>
                        <!-- onchange="funselprod(this.value)" -->
                        <select class="select_product_search selectpicker" id="pidproducto" name="pidproducto" data-live-search="true">
                            <option>Seleccione el producto</option>
                            @foreach ($productos as $pro)
                                @if(Auth::user()->profile == 'Administrador')
                                    <option value="{{ $pro->id }}_{{ $pro->stock }}_{{ $pro->precio_venta1 }}_{{ $pro->precio_venta2 }}_{{ $pro->precio_venta3 }}_{{ $pro->codigo }}_{{ $pro->nombre }}_{{ $pro->descripcion }}">{{ $pro->codigo }} | {{ $pro->nombre }}</option> 
                                @else
                                    <option value="{{ $pro->id }}_{{ $pro->stock }}_{{ $pro->precio_venta1 }}_{{ $pro->precio_venta2 }}_-_{{ $pro->codigo }}_{{ $pro->nombre }}_{{ $pro->descripcion }}">{{ $pro->codigo }} | {{ $pro->nombre }}</option> 
                                @endif 
                            @endforeach
                        </select>
                    </div>
       
                    <div class="col-lg-1 col-sm-1 col-md-1 col-xs-12">
                        <label>Cantidad</label>
                        <div class="form-group">
                            <div class="nk-int-st">
                                <input type="number" id="pcantidad" name="pcantidad" min="1" class="form-control" placeholder="Cantidad" value="1" required>
                            </div>
                        </div>
                    </div>
    
                    <div class="col-lg-2 col-sm-2 col-md-2 col-xs-12">
                        <label>Disponibilidad</label>
                        <div class="form-group">
                            <div class="nk-int-st">
                                <input type="number" id="pstock"  class="form-control" placeholder="Disponible" disabled>
                            </div>
                        </div>
                    </div>
    
                    <div class="col-lg-2 col-sm-2 col-md-2 col-xs-12">
                        <label>Precio Unidad</label>
                        <div class="form-group">
                            <div class="nk-int-st">
                                <select id="pprecio_venta" name="pprecio_venta" class="form-control"></select>
                            </div>
                        </div>
                    </div>
    
                    <div class="col-lg-1 col-sm-1 col-md-1 col-xs-12 button_agregar">
                        <!-- onclick="agregar();" -->
                        <div id="agregar" style="margin-top: 5px;" class="btn notika-btn-teal waves-effect agregar">
                            <i style="font-size: 15px; padding-rigth: 5px;" class="fas fa-plus-circle"></i>Agregar
                        </div>
                    </div>
    
                    <div class="col-lg-1 col-sm-1 col-md-1 col-xs-12 button_agregar">
                        <!-- onclick="agregar();" -->
                        <div id="agregar_externo" style="margin-top: 5px;" class="btn notika-btn-teal waves-effect agregar">
                            <i style="font-size: 15px; padding-rigth: 5px;" class="fab fa-product-hunt"></i> Externo
                        </div>
                    </div>
                </div>


 

                <!-- AQUI PRESENTAMOS LOS DATOS QUE SE TRAE DE LA TABLA -->
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="invoice-sp">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th style="width: 5%;">#</th>
                                        <th style="width: 10%;">Código</th>
                                        <th style="width: 50%;">Producto / Descripcion</th>
                                        <th style="width: 5%;">Cant.</th>
                                        <th style="width: 15%;">Precio U.</th>
                                        <th style="width: 15%;">Total</th>
                                        <th style="width: 5%;">Opciones </th>
                                    </tr>
                                </thead>
                                <tbody id="detalles">


                                    @php
                                        $id_producto = explode(',', $value->id_producto); 
                                        $codigo = explode(',', $value->codigo); 
                                        $nombre = json_decode($value->nombre); 
                                        
                                        $descripcion = json_decode($value->descripcion);
                                        $cantidad = explode(',', $value->cantidad); 
                                        $precio_total = explode(',', $value->precio_total); 
                                        $subtotal = explode(',', $value->subtotal); 
                                        $inicio = 1;
                                        $count = count($codigo);
                                    @endphp
 

                                    <!-- HACEMOS UN FOR PARA PRESENTAR LOS PRODUCTOS EN LISTA -->

                                    @for( $i = 0; $i < count($codigo); $i ++ )

                                        <!-- CARGAMOS LOS DATOS INICIALES ($i = $inicio; $i >= $count; $i--) -->
                                        <tr class="selected" id="fila{{$i}}"> 

                                            <td>
                                                <input type="hidden" name="count[]" value="{{$i}}">
                                                <input type="hidden" name="idproducto[]" value="{{$id_producto[$i]}}">
                                            </td>  
                                            <td>
                                                {{ $codigo[$i] }}
                                                <input type="hidden" name="codigo[]" value="{{ $codigo[$i] }}">
                                            </td>  
                                            <td>
                                                <strong>{{ $nombre[$i] }}</strong>, {{ $descripcion[$i] }}
                                                <input type="hidden" name="nombre[]" value="{{ $nombre[$i] }}">
                                                <input type="hidden" name="descripcion[]" value="{{ $descripcion[$i] }}">
                                            </td>  
                                            <td>
                                                {{ $cantidad[$i] }}
                                                <input type="hidden" name="cantidad[]" value="{{ $cantidad[$i] }}">
                                            </td>
                                            <td>
                                                RD$ {{ $precio_total[$i] }}
                                                <input type="hidden" name="precio_venta[]" value="{{ $precio_total[$i] }}">
                                            </td>   
                                            <td class="precio">
                                                RD$ {{ $subtotal[$i] }}
                                                <input type="hidden" class="subtotal" name="subtotal[]" value="{{ $subtotal[$i] }}">
                                            </td>    
                                            <td>
                                                <i class="fa fa-trash fa-lg eliminar trash_button" id="{{$i}}"></i>
                                            </td>
                                        </tr>


                                    @endfor


                                    


                                </tbody>

                                <tfoot>
                                    <tr>
                                        <th style="width: 5%;"></th>
                                        <th style="width: 10%;"></th>
                                        <th style="width: 40%;"></th>
                                        <th style="width: 15%;"></th>
                                        <th style="width: 15%;">Sub Total</th>
                                        <th style="width: 20%;">
                                        @php $total = $value->total; @endphp
                                        @php $subtotal = $value->total - $value->itbis; @endphp
                                            <h6 id="precio_total" style="color: green; font-size: 15px;">{{ $subtotal }}</h6>
                                        </th>
                                        <th style="width: 0%;"></th>
                                    </tr>
                                     <tr>
                                        <th style="width: 5%;"></th>
                                        <th style="width: 10%;"></th>
                                        <th style="width: 40%;"></th>
                                        <th style="width: 15%;"></th>
                                        <th style="width: 15%;">ITBIS </th>
                                        <th style="width: 20%;"><h6 id="precio_itbis" style="color: red; font-size: 15px;">{{ $value->itbis }}</h6></th>
                                        <th style="width: 0%;"></th>
                                    </tr>
                                    <tr>
                                        <th style="width: 5%;"></th>
                                        <th style="width: 10%;"></th>
                                        <th style="width: 40%;"></th>
                                        <th style="width: 15%;"></th>
                                        <th style="width: 15%;">Precio Total</th>
                                        <th style="width: 20%;">
                                            <h6 id="total_final" style="color: green; font-size: 20px;">{{ $total }}</h6>
                                        </th>
                                        <th style="width: 0%;"></th>
                                    </tr>
                                </tfoot>

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

                <input type="hidden" name="id_cotizacion" value="{{$value->id}}">
                <input type="hidden" name="total" id="total_final">
            @endforeach
 

             <div class="modal-ft">
                 <div class="btn btn-danger notika-btn-danger waves-effect limpiar_tabla">Limpiar</div>
                <button type="button" class="btn btn-default waves-effect" onclick="location.href='{{ url()->previous() }}';">volver atras</button>
                <button type="submit" class="btn btn-primary waves-effect" >Actualizar Pre venta</button>
            </div>

            

        </form>

        </div>
    </div>

    @include('layouts.formulario_externo')
    @include('layouts.formularios_emergentes')
    @yield('form_user')

              
</div>


<script>
    

    function limpiar(){
       $("#pcantidad").val();
       $("#pstock").val("");
       $("#pprecio_venta").val(""); 
    }

    function evaluar(){
        if(+total > 0){
            $("#guardar").show();
        }else{
            $("#guardar").hide();
        }
    }



</script>

@endsection