@extends('layout')
@section('title','Liste des ventes cloturées ')
@section('description','Liste des ventes cloturées')
@section('content')
			<br/>
          <div class="section-body">
		  <div class="row">
              <div class="col-12">
			  @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>
		  </div>
			<div class="row">
              <div class="col-12">
				<div class="card">
                  <div class="card-header">
                    <h4>Recherche</h4>
                  </div>
                  <div class="card-body">
				<form  action="{{route('rechcloturees')}}" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
                     <div class="col-lg-5 col-md-5"> 
					  <div class="form-group">
                      <label>Date début <font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_debut" id="champ_date" required>
					  <div id="calendarMain"></div>
                    </div>
					</div>
					<div class="col-lg-5 col-md-5"> 
					  <div class="form-group">
                      <label>Date fin <font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_fin" id="champ_date2" required>
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-2 col-md-2"> 
                      <button class="btn btn-icon icon-left btn-success"><i class="fas fa-search"></i> Rech</button>
					</div>
                      </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
			<a class="btn btn-icon btn-info pull-right" href="{{route('liste-des-versements')}}"> <i class="fa fa-list"></i> Voir liste des versements caissiers</a>
			<br/>
			<br/>
                <div class="card">
                  <div class="card-header">
                    <h4>Liste des ventes cloturées</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr style="text-align:center;">
                            <th>N°</th>
                            <th>Caissier</th>
                            <th>Date</th>
                            <th>Recette</th>
                            <th>Dépense</th>
                            <th>Caisse off</th>
                            <th>Versé</th>
                            <th>Reste</th>
                            <th hidden>Etat</th>
                            <th>Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($cloture as $clotures)
						@php
						$verse = DB::table('versement_caisier')->where('cloture_id', $clotures->id)->sum('montant');
						$rr = $clotures->caisseoff - $verse;
						@endphp
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{DB::table('users')->where('id',$clotures->user_id)->first()->login}}</td>
							@php
							$clotures->date= explode('-', $clotures->date);
							$clotures->date= array_reverse($clotures->date);
							$clotures->date= implode('/', $clotures->date);
							@endphp
							<td style="text-align:center;">{{$clotures->date}}</td>
							@php
							$lh = $clotures->montant;
							$lh1 =  number_format($lh, 0, ' ', ' ');
							@endphp
							<td style="text-align:center;">{{$lh1}} </td>
							<td style="text-align:center;">{{number_format($clotures->totaldep, 0, ' ', ' ')}} </td>
							<td style="text-align:center;">{{number_format($clotures->caisseoff, 0, ' ', ' ')}} </td>
							<td style="text-align:center;">{{number_format($verse, 0, ' ', ' ')}} </td>
							<td style="text-align:center;">{{number_format($rr, 0, ' ', ' ')}} </td>
							
							  @if( $clotures->etat == "Versement non effectué" )
								<td hidden style="text-align:center;">
								<span class="badge badge-warning badge-shadow">
								{{$clotures->etat}}
								</span>
								</td>
								@else
									
								<td hidden style="text-align:center;">
								<span class="badge badge-success badge-shadow">
								 @if( DB::table('versement_caisier')->where('clo_id', $clotures->id)->first()->ecart == "0" ) Versement effectué  @else  {{$clotures->etat}} avec {{DB::table('versement_caisier')->where('clo_id', $clotures->id)->first()->ecart}}  d'écart @endif
								</span>
								</td>
							   @endif
							
							<td style="text-align:center;">
							 <a type="button" href="#" data-toggle="tooltip" data-id="{{$clotures->id}}"
                                            data-placement="bottom" title="Ajouter versement"
                                    class="btn btn-icon btn-danger data-valider"><i
                                class="fas fa-plus"></i></a>
                                
							<a type="button" target="_blank" href="{{route('fichecloture',['id'=>$clotures->id])}}" data-toggle="tooltip" data-placement="bottom" title="Télécharger fiche" class="btn btn-icon btn-info"><i class="fas fa-file"></i></a>
							<a type="button" target="_blank" href="{{route('detailcloture',['id'=>$clotures->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détail clôture" class="btn btn-icon btn-success"><i class="fas fa-file"></i></a>
							</td>
                          </tr>
						  @endforeach
                        </tbody>
                        <tfoot hidden>
							<tr>
								<td style="text-align:right;" colspan="4"><b>Montant total des ventes journalières</b></td>
								@php
								$lhh = $total;
								$lhh1 =  number_format($lhh, 0, ' ', ' ');
								@endphp
								<td style="text-align:right;"><b> {{$lhh1}}  </b></td>
							</tr>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          </div>
		   <div class="modal fade" id="Validermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-body">
                <center>AJOUTER VERSEMENT</center>
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">
                
              </div>
            </div>
          </div>
        </div>
		
		@push('scripts')
		
		<script>
    $(".data-valider").on('click', function () {
        var id = $(this).data('id');
        $.get(location.origin + '/es/voircloture/' + id, function (data) {
            var html = `
            <form action="{{route('vdversementPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
						<div class="row">
						<div class="col-md-8">
                      <input type="hidden" value="`+ data.cloture.id + `" name="id" >
                      Date : <input type="date" name="datet" class="form-control" style="width:100%" required> <br/>
                      Montant versé : <input type="text" name="montant" class="form-control" style="width:100%" placeholder="Saisir montant versé" required>
                      Total remise sur articles .<input type="text" name="remise" class="form-control" style="width:100%" value="0" required>
                      </div>
					  <div class="col-md-4">
					  <button type="submit" class="btn btn-success">Oui</button>
						</div>
						</div>
            </form>`
                ;
            $('#valider').html('').append(html);
        })
        $('#Validermodal').modal();
    });
</script>
	
@endpush
@endsection