@extends('layouts.app')

@section('title',"Editar venta")

@section('content')
	@section('scripts_url')
	<script src="{!! asset('assets/file/js/piexif.min.js') !!}" type="text/javascript"></script>
	<link href="{!! asset('assets/file/css/fileinput.min.css') !!}" rel="stylesheet" type="text/css">
    <script src="{!! asset('assets/file/js/fileinput.min.js') !!}"></script>
	<script>
		// url para consultas
		const cambiourl = "{{ url('tcambio') }}",
			  tipdocurl = "{{ url('tipdoc') }}",
			  url_local = "{{ url('ventas') }}",
			  modalsurl="{{ url('ventas/modals')}}";
	</script>
	@endsection
	@if (session('create_compra'))
	    <div class="alert alert-success">
	        {{ session('create_compra') }}
	    </div>
	@endif
<div class="alert alert-secundary p-2 mb-0"><a href="{{ url('compras') }}"><i class="icon-arrow-left13"></i> Volver al listado de Ventas</a></div>
<div class="card">
	<ul class="nav nav-tabs nav-tabs-highlight">
			<li class="nav-item">
				<a href="#basic-tab1" class="nav-link active" data-toggle="tab">Venta detalle</a>
			</li>
			<li class="nav-item"><a href="#basic-tab2" class="nav-link" data-toggle="tab">Archivos</a></li>
	</ul>
	<div class="card-body">
		<div class="tab-content">
			<div class="tab-pane fade show active" id="basic-tab1">

				<div class="response-data">
					
				</div>
				<form action="{{ url('ventas/'.$data['cabecera']->id) }}" class="form-login" onsubmit="update_contable(this); return false">
					@csrf
					<input type="hidden" name="action" value="update">
					<input type="hidden" name="id_venta" id="id_contable" value="{{ $data['cabecera']->id }}">
					<input type="hidden" name="idfile" id="idfile" value="{{ $data['cabecera']->id }}">
					<input type="hidden" name="codigo_num" id="codigo_num" value="{{ $data['cabecera']->codigo_num }}">
					<div class="content-cuentas"></div>
					<div class="row">
						<div class="col-2">
							<a href="#" style="color: black;" data-status="{{(empty($data['cabecera']->status)) ? '0' : $data['cabecera']->status }}" data-id="{{ $data['cabecera']->id }}" id="lock_reg"><i  style="font-size: 26px;" class="{{(empty($data['cabecera']->status) OR $data['cabecera']->status == 0) ? 'icon-unlocked'  : 'icon-lock4' }}"></i></a>
						</div>
						<div class="col-8 text-center text-uppercase ">
							<h3 class="font-weight-light">Editar nueva venta</h3>
						</div>
					</div>
					<br>
					<div class="row">
						<div class="col-3">
							<div class="input-group">
								  <div class="input-group-prepend">
								    <span class="input-group-text" id="basic-addon1">Fecha</span>
								  </div>
								  <input type="text" class="form-control fec" id="input-cont"  name="create_at" placeholder="00/00/0000" aria-describedby="basic-addon1" value="{{$data['cabecera']->create_at}}">
							</div>
						</div>
						<div class="col-4" style="position: relative;">
							<div class="input-group ">
							  <div class="input-group-prepend">
							    <span class="input-group-text" id="basic-addon1">Nombre o razon social</span>
							  </div>
							  <input type="text" class="form-control mynombre"  data-tipe="entidad" data-action="{{ url('entidad') }}"  name="nombre" onkeyup="search_razon(this); return false" placeholder="Nombre" aria-describedby="basic-addon1" autocomplete="off" value="{{$data['cabecera']->nombre}}">
							</div>
							<div class="row row-search" >
								<div class="col-4">		
								</div>
								<div class="col-8">
									<div class="content-search entidad-search" >
									</div>
								</div>
							</div>		
						</div>
						<div class="col-2">
							<select name="identidad_document_id" class="form-control"  id="identidad_select" >
								<option value="">Tipo</option>
								@if($complet['identidad']->isNotEmpty())
									@foreach ($complet['identidad'] as $i)
										@if($data['cabecera']->identidad_document_id == $i->codigo)
											<option value="{{$i->codigo}}" data-id="{{$i->id}}" selected="">{{ $i->abrv }} || {{ $i->descripcion }}</option>
										@else
											<option value="{{$i->codigo}}" data-id="{{$i->id}}" >{{ $i->abrv }} || {{ $i->descripcion }}</option>
										@endif
									@endforeach
								@endif
							</select>
						</div>
						<div class="col-3 text-left" >
							<div class="input-group ">
								<input type="text" class="form-control identidad_numero" id="identidad_num"   name="numero_identidad" placeholder="Numero " aria-describedby="basic-addon1" value="{{$data['cabecera']->numero_identidad}}">
							</div>
						</div>
					</div>
					<hr>
					<div class="row">
						<div class="col-3">
							<div class="input-group ">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Documento</span>
							  </div>
							  <select name="tipdocs_id" class="form-control" id="tipdocs_campo" onchange="tipdocs(this); return false">
							  	<option value="">Seleccionar</option>
							  	@if($complet['tipdocs']->isNotEmpty())
							  		@foreach ($complet['tipdocs'] as $t)
							  			@if($t->id == $data['cabecera']->tipdocs_id )
							  				<option value="{{$t->id}}" data-cambio="{{$t->codsun}}" data-descor="{{$t->descor}}" {{ ($t->relacional == 1) ? 'data-relacional="$t->relacional" ' : '' }} selected="selected">{{$t->descor}}</option>
							  			@else
							  				<option value="{{$t->id}}" data-cambio="{{$t->codsun}}"  data-descor="{{$t->descor}}"  {{ ($t->relacional == 1) ? 'data-relacional='.$t->relacional  : '' }} >{{$t->descor}}</option>
							  			@endif
							  		@endforeach
							  	@endif
							  </select>
							</div>
						</div>
						<div class="col-2">
							<input type="text" class="form-control"  name="serie" id="docserie" value="{{$data['cabecera']->serie}}">
						</div>
						<div class="col-2">
							<input type="text" class="form-control"  name="numero" id="numero_serie" value="{{$data['cabecera']->numero}}">
						</div>
						<div class="col-5">
							<select name="forpags" class="form-control" id="">
							  	<option value="">Forma de pago</option>
							  	@if($complet['forpag']->isNotEmpty())
							  		@foreach ($complet['forpag'] as $f)
							  			@if($f->descripcion == $data['cabecera']->forpags)
							  				<option value="{{$f->descripcion}}" data-codigo="{{$f->codigo}}" selected="selected">{{$f->descripcion}}</option>
							  			@else
							  				<option value="{{$f->descripcion}}" data-codigo="{{$f->codigo}}">{{$f->descripcion}}</option>
							  			@endif
							  		@endforeach
							  	@endif
							</select>
						</div>
					</div>
						<div class="row relacional-content mt-2" style="display: none;">
							<div class="col-3">
								<div class="input-group ">
								  <div class="input-group-prepend">
								    <span class="input-group-text " id="basic-addon1">Relacional</span>
								  </div>
								  <select class="form-control" data-relacion="1"  id="relacional_select" onchange="tipdocs(this); return false">
								  	<option value="">Seleccionar</option>
								  	@if($complet['tipdocs']->isNotEmpty())
								  		@foreach ($complet['tipdocs'] as $t)
								  			@if($t->id == $data['cabecera']->tipdoc_relacional )
								  				<option value="{{$t->codsun}}" selected="">{{$t->descor}}</option>
								  			@else
								  				<option value="{{$t->codsun}}" >{{$t->descor}}</option>

								  			@endif
								  		@endforeach
								  	@endif
								  </select>
								</div>
							</div>
							<div class="col-2">
								<input type="text" class="form-control"  id="docserie_relacional" value="{{$data['cabecera']->serie_relacional}}">
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="input-cont" id="docnumero_relacional" value="{{$data['cabecera']->numero_relacional}}">
							</div>
						</div>
					
					<hr>
					<div class="row">
						<div class="col-2">
							<div class="input-group ">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Moneda</span>
							  </div>
							  <select name="divisa" class="form-control" id="divisa_change" onchange="change_divisa(this); return false" >
							  	<option value=""></option>
							  	<option value="{{ $data['cabecera']->divisa }}"  data-divisa="{{$data['cabecera']->divisa}}" selected="selected">{{$data['cabecera']->divisa}}</option>
							  	@if($complet['divisas']->isNotEmpty())
							  		@foreach ($complet['divisas'] as $d)
							  			@if($d->divisa == $data['cabecera']->divisa)
							  				
							  			@else
							  				<option value="{{$d->divisa}}" data-cambio="{{$d->compra}}" data-divisa="{{$d->divisa}}" >{{$d->divisa}}</option>
							  			@endif
							  		@endforeach
							  	@endif
							  </select>
							</div>			
						</div>
						<div class="col-3">
							<div class="input-group">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Fec emision</span>
							  </div>
							  <input type="text" class="form-control fec" id="emision" onchange="cambious(this); return false" name="fec_emision" placeholder="00/00/0000" value="{{$data['cabecera']->fec_emision}}" >
							</div>			
						</div>
						<div class="col-2">
							<div class="input-group ">
							  <div class="input-group-prepend">
							    <span class="input-group-text" id="basic-addon1">Cambio</span>
							  </div>
							  <input type="text"  name="cambio"  id="cambioinput" value="{{$data['cabecera']->cambio}}" class="form-control divisaval">
							</div>		
						</div>
						<div class="col-2">
							<div class="input-group ">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Dias</span>
							  </div>
							  <input type="text" name="dias" id="input-cont" class="form-control" value="{{$data['cabecera']->dias}}">
							</div>			
						</div>
						<div class="col-3">
							<div class="input-group">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Fec vencimiento</span>
							  </div>
							  <input type="text" class="form-control fec" id="input-cont" name="fec_vencimiento" placeholder="00/00/0000" value="{{$data['cabecera']->fec_vencimiento}}">
							</div>			
						</div>
					</div>
					<hr>
					<div class="row">
						<div class="col-4">
							<div class="input-group">
							  <div class="input-group-prepend">
							    <span class="input-group-text " id="basic-addon1">Glosa</span>
							  </div>
							  <input type="text" class="form-control"  name="glosa" value="{{$data['cabecera']->glosa}}"  id="glosa_concept">
							</div>			
						</div>
						<div class="col-2">
							 <input type="text" class="form-control" id="input-cont" name="detracciones1" placeholder="Detracciones" value="{{$data['cabecera']->detracciones1}}">
						</div>
						<div class="col-2">
							 <input type="text" class="form-control" id="input-cont" name="detracciones2" placeholder="Detracciones" value="{{$data['cabecera']->detracciones2}}">
						</div>
					</div>
					<div class="igv_no  {{($data['cabecera']->doc_sig=='FEX') ? '' : 'display_none'}}"  >
						<hr>
						<div class="row justify-content-center">
							<div class="col-2">
								<select class="form-control" name="nodomiselect" id="nodomiselect">
									<option value=""></option>
								  	@if($complet['tipdocs']->isNotEmpty())
								  		@foreach ($complet['tipdocs'] as $t)
								  			@if($t->flag_tipe == 'flag01')
								  				<option value="{{$t->codigo}}">
								  					{{$t->codsun}} | {{$t->descrip}}
								  				</option>
								  			@endif
								  		@endforeach
							  		@endif
								</select>
							</div>
							<div class="col-2">
								<input type="text" name="ndomiserie" class="form-control" id="ndomiserie">

							</div>
							<div class="col-3">
								<input type="text" name="ndominumero" class="form-control" id="nodomiselect">

							</div>
						</div>
					</div>
					<hr>
					<div class="conten-compra-detalle" >
						<div class="row">
							<div class="col-6 ">
								<button class="btn btn-primary" data-modal="addconcepto" data-action="createModal" data-compra="{{ $data['cabecera']->id }}" data-idn="{{ $data['cabecera']->numero_identidad }}"   onclick="modals_show(this); return false">Adicionar concepto</button> 
								<button class="btn btn-primary " data-modal="addarticulo" data-action="createModal" data-compra="{{ $data['cabecera']->id }}" data-idn="{{ $data['cabecera']->numero_identidad }}" onclick="modals_show(this); return false">Adicionar Articulo</button>
							</div>
							<div class="col-6 text-right">
								<a href="#" class="btn btn-warning buttons-contables" data-toggle="modal" data-target="#modalFiles">Adicionar Archivos</a>
							</div>
						</div>
						<br/>
						<div class="content-contable">
							<table class="table table-bordered">
								<thead>
									<th>Codigo</th>
									<th width="20%">Concepto</th>
									<th>Cantidad</th>
									<th>U/M</th>
									<th>Precio</th>
									<th>Cuenta</th>
									<th>Dolares</th>
									<th>Debe</th>
									<th>Haber</th>
									<th>Accion</th>
								</thead>
								<tbody class="content-lineas-table">
							
									
								</tbody>
							</table>
						</div>
					</div>
					<div class="content-values" >
						<hr>
						<div class="row">
							<div class="table-responsive">
								<table border="0" class="table">
									<thead>
										<th>Inafecto</th>
										<th>Afecto</th>
										<th>Anticipo</th>
										<th>Descuento</th>
										<th>V.compra</th>
										<th>Impuesto</th>
										<th>Sub total</th>
										<th>Percepcion</th>
										<th>Totales</th>
									</thead>
									<tbody>
										<tr>
											<td>
												<input type="text" class="form-control" id="inafecto_v" value="0">
												<input type="hidden" name="inafecto" class="form-control" id="inafecto" value="0">
											</td>
											<td>
												<input type="text" class="form-control"  id="afecto_v" value="0">
												<input type="hidden" name="afecto" class="form-control"  id="afecto" value="0">
											</td>
											<td>
												<input type="text" class="form-control" id="anticipo_v" value="0">
												<input type="hidden" name="anticipo" class="form-control" id="anticipo" value="0">
											</td>
											<td>
												<div class="row">
													<div class="col-12">
														<input type="text" class="form-control" id="descuento_v" value="0">
														<input type="hidden" name="descuento" class="form-control" id="descuento" value="0">
													</div>
												</div>
				
											</td>
											<td>
												<input type="text" class="form-control" id="vcompra_v" value="0">
												<input type="hidden" name="vcompra" class="form-control" id="vcompra" value="0">
											</td>
											<td>
												<div class="row">
													<div class="col-5">
														<input type="text"  value="18" class="form-control" id="igv_v" value="0">
														<input type="hidden" name="igv_percent" value="18" class="form-control" id="igv" value="0">
													</div>
													<div class="col-7">
														<input type="text" class="form-control" id="impuesto_v" value="0">
														<input type="hidden" name="impuesto" class="form-control" id="impuesto" value="0">
													</div>
													
												</div>
											</td>

											<td>
												<input type="text" class="form-control" id="subtotal_v" value="0" >
												<input type="hidden" name="sub_total" class="form-control" id="subtotal" value="0" >
											</td>
											<td>
												<input type="text" name="percepcion" class="form-control" id="percepcion_v" value="0">
												<input type="hidden" class="form-control" id="percepcion" value="0">
											</td>
											<td>
												<input type="text" class="form-control" id="total_v" value="0">
												<input type="hidden" name="totales" class="form-control" id="total" value="0">
											</td>
										</tr>
										<tr class="p-0 section_sol">
											<td colspan="9" class="p-0"><h4 class="text-center mb-0 text-uppercase">Soles</h4></td>
										</tr>
										<tr class="section_sol">
											<td>
												<input type="text" id="inafecto_cambio_v" class="form-control" value="0">
												<input type="hidden" name="inafecto_cambio" id="inafecto_cambio" class="form-control" value="0">
											</td>
											<td>
												<input type="text" id="afecto_cambio_v" class="form-control" value="0">
												<input type="hidden" name="afecto_cambio" id="afecto_cambio" class="form-control" value="0">
											</td>
											<td>
												<input type="text"   class="form-control" id="anticipo_cambio_v" value="0">
												<input type="hidden" name="anticipo_cambio" class="form-control" id="anticipo_cambio" value="0">
											</td>
											<td>
												<div class="row">
													<div class="col-12">
														<input type="text" id="descuento_cambio_v" class="form-control" value="0">
														<input type="hidden" name="descuento_cambio" id="descuento_cambio" class="form-control" value="0">
													</div>
												</div>
												
											</td>
											<td>
												<input type="text" id="vcompra_cambio_v" class="form-control" value="0">
												<input type="hidden" name="vcompra_cambio" id="vcompra_cambio" class="form-control" value="0">
											</td>
											<td>
												<input type="text" id="impuesto_cambio_v" class="form-control" value="0">
												<input type="hidden" name="impuesto_cambio" id="impuesto_cambio" class="form-control" value="0">
											</td>

											<td>
												<input type="text"  id="subtotal_cambio_v" class="form-control" value="0">
												<input type="hidden" name="subtotal_cambio" id="subtotal_cambio" class="form-control" value="0">
											</td>
											<td>
												<input type="text"  id="percepcion_cambio_v" class="form-control" value="0">
												<input type="hidden" name="percepcion_cambio" id="percepcion_cambio" class="form-control" value="0">
											</td>
											<td>
												<input type="text" id="total_cambio_v" class="form-control" value="0">
												<input type="hidden" name="total_cambio" id="total_cambio" class="form-control" value="0">
											</td>
										</tr>
									</tbody>
								</table>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-12 text-center">
							<br>
							<button class="btn btn-primary"><i class="icon-rotate-cw2 "></i> Actualizar</button>
						</div>
					</div>
				</form>
				@include('Libros_electronicos.views.utilitarios.files_document',['tipe'=>'ventas'])
			</div>
			<div class="tab-pane fade" id="basic-tab2">
				<div class="row">
					@include('Utilitarios.views.list.listfile')
				</div>
			</div>
		</div>
	</div>
</div>
<div class="response-ajax">
	
</div>
	@section('scripts')
		<script src="{!! asset('assets/views_functions/contable/functions.js') !!}"></script>
		<script src="{!! asset('assets/views_functions/contable/ventas.js') !!}"></script>
		<script>
			//funciones automaticas para  esta vista
			locks_reg();
			document.addEventListener('DOMContentLoaded', function(event) {
				tipdocajax();
				cambious();
				get_lineas();
				change_divisa();
				$(document).on("click",".entidadf",function(e){
					$.ajaxblock();e.preventDefault();e.stopImmediatePropagation();
			    	$(this).attr('disabled', 'true');
					$.ajax({
						url: "{{ url('ventas/modals') }}",
						type: 'POST',
						data: {'action': 'entidad_fac',
							   'identidad':"{{ $data['cabecera']->numero_identidad }}",
							   'id_venta':"{{ $data['cabecera']->id }}"
							},
					})
					.done(function(data) {
						$('.reponse-ajax-relacional').html('').html(data);
						$('#modalEntidad').modal('toggle');
						$('button').removeAttr('disabled');
						$.ajaxunblock();
					})
					return false;
				});
			});
		</script>
	@endsection
@endsection