@extends('layout')
@section('title','Modifier article')
@section('content')
	@if (session('status'))
            <div class="alert alert-warning" style="font-size: 15px; background-color: #328039; color: white">
                <i class="fa fa-check"></i> &nbsp; {{ session('status') }}
            </div>
            @endif

            @if (session('error'))
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <i class="ti-na"></i> {{ session('error') }}
            </div>
            @endif
            @if ($errors->any())
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li> <i class="ti-na"></i> {{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif

    <div class="container mt-3">
	    <div class="row">
	     	<div class="col-md4"></div>
			<div class="col-md-8">
				<input type="text" list="searchi keyword" class="form-control" name="search" id="search">
				<datalist id="searchi keyword">
				@foreach($produit as $p)
				@php
				$v = DB::table('stock_magasin')->where('produit_id', $p->id)->where('magasin_id', session('user')->magasin_id)->first();
				if ($v){
					$st = DB::table('stock_magasin')->where('produit_id', $p->id)->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
				}else{
					$st=0;
				}
				@endphp
					<option value="{{ $p->desi}}">{{ $p->desi}} | Stock : {{$st}}</option>
					@endforeach
				</datalist>
			</div>
		</div>
	</div>

	<form action="{{ route('ventepostbc') }}" method="POST" id="sell-form">
		{{csrf_field()}}
		<div class="container">
			
			<div class="row mt-2">
				<div class="card col-md-8">

					<table class="table">
						<tr>
							<th>Produit</th>
							<th>Prix détail</th>
							<th>Prix semi</th>
							<th>Prix gros</th>
							<th>Montant</th>
							<th></th>
						</tr>
						<tbody id="item">
						</tbody>
					</table>
				</div>

				<div class="col-md-4">
					<div class="card">
						
							<div class="card-footer" style="background-color: #FAFAD2;border-radius: 10px">
								<div>
									<br>
									<button type="button" class="btn btn-success" onclick="submitForm()" style="width: 100%;font-weight: 800;font-size: 16px"><i class="fa fa-save"></i> Enregistrer la vente</button>
								</div>
							</div>
							
					</div>	
					
				</div>
			</div>
		</div>
	</form>
</div>
</div>


	@push('scripts')
	
 
		<script type="text/javascript">
			var iteration  = 0;
			var appendTr='';
			$("input[name=search]").on('change', function (event) {
				event.stopImmediatePropagation();
				var barcode = $(this).val()
				var data = {search : barcode};
				data._token = "{{ csrf_token() }}";
				
				$.post("{{ route('vente-code2') }}", data, function (result) {
					result = JSON.parse(result);
					if(result != undefined && result != null &&  result != ""){
						appendTr = `
							<tr style="margin-bottom:-5px">
								<td>
									${result.designation}
									<input type="hidden" class="form-control" value="${result.id}" name="item[${iteration}][]">
								</td>
								<td>
									<input type="number" class="form-control" value="${result.prix_detail}"  name="item[${iteration}][]">
								</td>
								<td>
									<input type="number" class="form-control" value="${result.prix_semisgros}"  name="item[${iteration}][]">
								</td>
								<td>
									<input type="number" class="form-control" value="${result.prix_gros}"  name="item[${iteration}][]">
								</td>
								<td>
									<input type="hidden"  name="item[${iteration}][]">
									<span></span>
								</td>
								<td>
									<button class="btn btn-danger" onclick="removeTr(this);onMouseMove();"><i class="fa fa-times"></i></button>
								</td>
							</tr>
						`;

						$("#item").append(appendTr);
						onMouseMove();

						$("input[name=search]").val('');
						document.getElementById('search').focus();

						//iteration++;
					}
				});
			});


			$("body").on('click', function () {
				onMouseMove();
			})

			function removeTr(td){
				$(td).parent().parent().remove();
			}


			function submitForm(){
				if($("#item>tr").length > 0){
					document.getElementById('sell-form').submit();
				}
				
			}


		</script>

	@endpush
@endsection