@extends('layout')
@section('title','Approvisionnement matiere')
@section('content')
<div class="section-header">
            <h1>Approvisionnement d'matiere</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Approvisionnement d'matiere</a></div>
              <div class="breadcrumb-item"><a href="#">Nouveau</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 class="row">
              <div class="col-12 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Nouveau</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Les champs en * sont obligatoires</strong></h7>
				  
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
					<div class="col-lg-3 col-md-3" hidden> 
					
					  <div class="form-group">
                      <label>Magasin <font color="red">*</font></label>
                      <select class="form-control select2" name="magasin" readOnly>
						@foreach($magasin as $magasins)
						<option value="{{$magasins->id}}">{{$magasins->nom}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> 
					
					  <div class="form-group">
                      <label>Fournisseur <font color="red">*</font></label>
                      <select class="form-control select2" name="fournisseur">
                        <option value="">Sélectionner le fournisseur</option>
						 @foreach($fournisseur as $fournisseurs)
						<option value="{{$fournisseurs->id}}">{{$fournisseurs->nom}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					@php
						$d = date('d/m/Y');
					@endphp
					 <div class="col-lg-3 col-md-3"> 
					 <div class="form-group">
                      <label>Date appro.</label>
                      <input type="text" class="form-control datepicker" name="dateappro" value="{{$d}}">
                    </div>
					</div>
                      </div>
					   <hr>
					  <div class="row" style="margin-bottom:0px">
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> 
					<div class="form-group">
                      <label>matiere <font color="red">*</font></label>
                      <select title="sélectionner le type d'matiere" class="form-control select2" name="approvision[0][produit][]">
                        <option>Sélectionner le type d'matiere</option>
						 @foreach($matiere as $matieres)
						<option value="{{$matieres->id}}">{{$matieres->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-2 col-md-2 col-sm-2 col-xs-4"> 
					<div class="form-group">
                      <label>Qté (Nombre de colis)</label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[0][qte][]">
                    </div>
					</div>
					<div class="col-lg-2 col-md-2 col-sm-2 col-xs-4"> 
					<div class="form-group">
                      <label>Contenu du sac/coli/bol</label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[0][contenu][]">
                    </div>
					</div>
					  <div class="col-lg-3 col-md-3 col-sm-3 col-xs-4"> 
					<div class="form-group">
                      <label>PU (par sac/coli/bol) </label>
                      <input title="saisir le prix unitaire" type="text" class="form-control" name="approvision[0][pu][]" value="1">
                    </div>
					</div>
					
					 <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" data-placement="bottom" title="Ajouter produit" type="button" class="btn btn-icon btn-primary" id="addNewblock"><i class="fas fa-plus"></i></button>
                    
					</div>
				
                      </div>
					  <div class="addNouveau"></div>
					  
					 <hr>  	

                   <div class="pull-left">
                 
                    <button type="reset" class="btn btn-icon icon-left btn-danger"><i class="fas fa-times"></i> Annuler</button>
                    <button class="btn btn-icon icon-left btn-success" id="addappro"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
            
			<br><br>
					  
				</form>
					  
					    </div>
						 
                    </div>
                  </div>
                </div>

			
           
          </div>
		  
		  
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Proforma</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>Date</th>
                            <th hidden>Magasin</th>
                            <th>Etat</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($appro as $appros)
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$appros->numero}}</td>
							  @php
                                $dd = implode('/', array_reverse(explode('-', $appros->date_appro)));
                              @endphp
                            <td> {{$dd}} </td>
                            <td hidden> {{DB::table('magasin')->where('id', $appros->magasin_id)->first()->nom}} </td>
							@php
							$lh = DB::table('ligne_appro_matiere')->where('appro_matiere_id', $appros->id)->get()->sum('montligne');	
							$lh1 =  number_format($lh, 0, ' ', ' ');
							\Illuminate\Support\Facades\DB::table('appro_matiere')->where('id', $appros->id)->update(
							[
							'montant' => $lh,
							]);
							@endphp
                    
                            <td class="align-middle">
							    @if( $appros->etat ==  "Proforma")
									
                                  <div class="badge badge-warning badge-shadow">{{$appros->etat}}</div>
							    @elseif ( $appros->etat ==  "Validée")
                                      <div class="badge badge-success badge-shadow">{{$appros->etat}}</div>
								@else
                                      <div class="badge badge-danger 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_matiere',['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>
							 @if( $appros->etat ==  "Proforma")
							<a type="button" href="#" data-id="{{$appros->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							@endif
							 @if( $appros->etat ==  "Validée")
							<a type="button" href="#" data-id="{{$appros->id}}" data-toggle="tooltip" data-placement="bottom" title="Annuler" class="btn btn-icon btn-danger data-annuler"><i class="fas fa-times"></i></a>
							@endif
							</td>
                          </tr>
                      @endforeach
                        </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>
		
		<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 cet approvisionnement ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		
		<div class="modal fade" id="Annulermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Annulation</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 annuler cet approvisionnement ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="annuler">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   
	    <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/validerappromatiere/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerappromatierePost')}}" 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>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
	    <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/validerappromatiere/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerappromatierePost')}}" 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>
	
	<script>
        $(".data-annuler").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/validerappromatiere/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('annulerappromatierePost')}}" 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>`
            ;
            $('#annuler').html('').append(html);
          })
          $('#Annulermodal').modal();
        });
    </script>
	   
	   <script>

            function removeBlock() {
               $(".deleteblock").on("click", function () {
                           $(this).parent().parent().remove()
                       })
              }

              var i=0;
               $("#addNewblock").on("click", function ()
                {
                   i++;
                   var html = `
				   
				   <div class="row" style="margin-top:-10px">
                  
				   <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12"> 
					
					  <div class="form-group">
                      <label>matiere <font color="red">*</font></label>
                      <select title="sélectionner matiere" class="form-control select2" name="approvision[`+ i +`][produit][]">
                        <option value="">Sélectionner matiere</option>
						
						 @foreach($matiere as $matieres)
						 
						<option value="{{$matieres->id}}">{{$matieres->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12"> 
					<div class="form-group">
                      <label>Qté (Nombre de colis)</label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[`+ i +`][qte][]">
                    </div>
					</div>
					<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12"> 
					<div class="form-group">
                      <label>Contenu du sac/coli/bol </label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[`+ i +`][contenu][]">
                    </div>
					</div>
					  <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> 
					<div class="form-group">
                      <label>Prix unitaire (en FCFA) </label>
                      <input title="saisir le prix unitaire" type="text" class="form-control" name="approvision[`+ i +`][pu][]" value="1">
                    </div>
					</div>
					
					  <div class="col-lg-1 col-md-1 col-sm-1 col-xs-12" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i></button>
                   
					</div>
					
		 </div>
				  
                       `;

                   $('.addNouveau').append(html);
				   $('.select2').select2();
                   removeBlock();

             });
    </script>
	
	 <script>
        $( "#addappro").on('click', function( e ) {
        e.preventDefault();
        var data = {};
		var isValide = true;
        data.magasin = $("select[name=magasin]").val();
        data.fournisseur = $("select[name=fournisseur]").val();
        data.dateappro = $("input[name=dateappro]").val();
        data.approvision = [];

        if(!data.magasin){
            toastr.error('Veuillez sélectionner un magasin', 'Attention !', {timeOut: 5000})
            return false;
        }
		if(!data.fournisseur){
            toastr.error('Veuillez sélectionner le fournisseur', 'Attention !', {timeOut: 5000})
            return false;
        }

        $("[name*=approvision]").each(function(index, element){
                if($(element).val() != ""){
					if(!Number($(element).val())){
						isValide = false; 
						toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
					}
					console.log($(element).val());
                    data.approvision.push($(element).val());
                }else{
                    toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
                   console.log($(element));
				   isValide = false;
                    return false;
                }
         });

        data._token = "{{csrf_token()}}";

        if(data.fournisseur && data.approvision.length && isValide){
            $.post("{{route('matiereapproPost')}}", data, function (result) {
            if(result.type == 'error'){
                toastr.error(result.response, result.title, {timeOut: 5000})
            }else{

                toastr.success(result.response, result.title, {timeOut: 15000})
                 location.reload();
            }

            })
        }
        });
</script>
	   
	   
@endpush

@endsection