@extends('layout')
@section('title','Liste des matieres première ')
@section('content')
		<br/>
          <div class="section-body">
            <div class="row">
              <div class="col-lg-10 offset-1 col-md-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
		<br/>
		<a style="margin-right:13px" class="btn btn-icon btn-danger pull-right" href="{{route('inventaire-matiere-premiere')}}"> <i class="fas fa-file-contract"></i> Inventaire matière première</a> &nbsp;
		  </div>
		</div>
          </div>
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Liste des matières premières</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 class="text-center">Code</th>
                            <th class="text-center">Désignation</th>
                            <th class="text-center">Unité de mesure</th>
                            <th class="text-center">En stock</th>
                            <th  style="text-align:center;">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($matiere as $matieres)
						@php
						$apc = DB::table('stock_matiere')->where('matiere_id', $matieres->id)->get()->count();
						if ($apc == 0){
							$ap = 0;
						} else {
							$ap = DB::table('stock_matiere')->where('matiere_id', $matieres->id)->orderBydesc('id')->take(1)->first()->apres;
						}
						@endphp
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$matieres->code}}</td>
							<td style="text-align:center;">{{$matieres->designation}}</td>
							<td style="text-align:center;">{{$matieres->unite_mesure}}</td>
							<td style="text-align:center;">
							 @if( $ap > $matieres->stock_mini)
                                    <div class="badge badge-success badge-shadow">{{$ap}}</div>
							    @else
                                    <div class="badge badge-danger badge-shadow">{{$ap}}</div>
                                @endif
							</td>
							<td style="text-align:center;">
								<a class="btn btn-info" href="{{route('modifier-matiere',['id'=>$matieres->id])}}">
									<i class="fas fa-edit"></i>
								</a>
							    <a type="button" href="#" data-id="{{$matieres->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							
							</td>
                          </tr>
						  @endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
 <div class="modal fade" id="Supprimermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Suppression</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 supprimer ce produit  ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   
	      <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Matiere/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('supprimermatierePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.matiere.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

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

@endsection