@extends('layout')
@section('title','Liste des approvisionnements ')
@section('content')
<div class="section-header">
            <h1>Liste des approvisionnements</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Liste des approvisionnements</a></div>
              <div class="breadcrumb-item"><a href="#">Liste</a></div>
            </div>
          </div>
          <div class="section-body">
		    @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 class="section-body">
            <div class="row">
              <div class="col-12">
			  <br/>
		<div class="card">
                  <div class="card-header">
                    <h4>Recherche</h4>
                  </div>
                  <div class="card-body">
				<form  action="{{route('rechercheapproPost')}}" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Fournisseur <font color="red">*</font></label>
                      <select class="form-control select2" name="fou">
                        <option value="0">Tous les fournisseurs</option>
							@foreach($fournisseur as $fournisseurs)
							<option value="{{$fournisseurs->id}}">{{$fournisseurs->nom}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
                     <div class="col-lg-3 col-md-12"> 
					  <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-3 col-md-12"> 
					  <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-12"> 
                      <button class="btn btn-icon icon-left btn-success"><i class="fas fa-search"></i> Rech</button>
					</div>
                      </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
                <div class="card">
                  <div class="card-header">
                    <h4>Liste de tous les approvisionnements</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th class="text-center"> N° </th>
                            <th>Référence</th>
							<th class="text-center"> Fournisseur</th>
                            <th>Date</th>
                            <th>Montant</th>
                            <th>Etat</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($appro as $appros)
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$appros->numero}}</td>
							<td> {{DB::table('fournisseur')->where('id', $appros->fournisseur_id)->first()->nom}}</td>
							  @php
                                $dd = implode('/', array_reverse(explode('-', $appros->date_appro)));
                              @endphp
                            <td> {{$dd}} </td>
							@php
							$lh = DB::table('ligne_appro_vendable')->where('appro_id', $appros->id)->get()->sum('montligne');	
							$lh1 =  number_format($lh, 0, ' ', ' ');
							\Illuminate\Support\Facades\DB::table('appro_vendable')->where('id', $appros->id)->update(
							[
							'montant' => $lh,
							]);
							@endphp
                            <td>  {{$lh1}} FCFA</td>
                    
                            <td class="align-middle">
							    @if( $appros->etat ==  "Proforma")
									
                                  <div class="badge badge-warning badge-shadow">{{$appros->etat}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$appros->etat}}</div>
                                @endif
								  
                            </td>
                            <td>
							 @if( $appros->etat ==  "Proforma")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$appros->id}}" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							@endif
							<a type="button" href="{{route('consultation_appro',['id'=>$appros->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							<a type="button" target="_blank" href="{{route('factureappro',['id'=>$appros->id])}}" data-toggle="tooltip" data-placement="bottom" title="Bon de commande" class="btn btn-icon btn-info"><i class="fas fa-file"></i></a>
							<a type="button" href="#" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger"><i class="fas fa-trash"></i></a>
							
							</td>
                          </tr>
                      @endforeach
					  <tr>
						<td colspan="4"> </td>
						<td> <b>Total</b></td>
						<td> 
						@php
						$total = $total;
						$total =  number_format($total, 0, ' ', ' ');
						@endphp
						<b>{{$total}}</b>
						</td>
						<td colspan="2"></td>
					  <tr>
                        </tbody>
                      </table>
                    </div>
                  </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-header">
                <h5 class="modal-title" id="exampleModalLabel">Validation approvisionnement général</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                Voulez-vous valider cet approvisionnement ?
              </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+'/gd/validerappro/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerapproPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.appro.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script>
	   
@endpush

@endsection