@extends('layout')
@section('title','Nouvelle prestation ')
@section('content')
<div class="section-header">
            <h1>Prestation</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Prestation</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 col-sm-3 col-xs-3"> 
					  <div class="form-group">
                      <label>Client <font color="red">*</font></label>
                      <select class="form-control select2" name="client_id">
                        <option value="">Sélectionner le client</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 col-sm-3 col-xs-3"> 
					  <div class="form-group">
                      <label>Appliquer taxes? <font color="red">*</font></label>
                      <select class="form-control select2" name="taxe">
                        <option value="">Sélectionner</option>
						<option value="Oui">Oui</option>
						<option value="Non">Non</option>
                      </select>
                    </div>
					</div>
					 <div class="col-lg-3 col-md-3"> 
					 <div class="form-group">
                      <label>Date prestation.</label>
                      <input type="text" class="form-control datepicker" name="datep" value="{{$d}}">
                    </div>
					</div>
					<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> 
					<div class="form-group">
                      <label>Remise <font color="red">*</font></label>
                      <input type="text" class="form-control" name="remise" value="0">
                    </div>
					</div>
                      </div>
					   <hr>
					  <div class="row">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> 
					<div class="form-group">
                      <label>Désignation <font color="red">*</font></label>
                      <input title="saisir la désignation" type="text" class="form-control" name="prestation[0][designation][]">
                    </div>
					</div>
					 <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2"> 
					
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité" type="text" class="form-control" name="prestation[0][qte][]">
                    </div>
					</div>
					
					  <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> 
					
					<div class="form-group">
                      <label>Prix unitaire (en FCFA) <font color="red">*</font></label>
                      <input title="saisir le prix unitaire" type="text" class="form-control" name="prestation[0][pu][]">
                    </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="addprestation"><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>Liste des factures</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>Montant HT</th>
                            <th>Montant Taxe</th>
							<th>Remise</th>
                            <th>TTC</th>
                            <th>Etat</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($prestation as $prestations)
						@php
                            $dd = implode('/', array_reverse(explode('-', $prestations->date_prestation)));
                        @endphp
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$prestations->numero}}</td>
							<td> {{$dd}} </td>
                            <td>  {{number_format($prestations->montant, 0, ' ', ' ')}}</td>
                            <td>  {{number_format($prestations->montant_taxe, 0, ' ', ' ')}}</td>
                            <td>  {{number_format($prestations->remise, 0, ' ', ' ')}}</td>
                            <td>  {{number_format($prestations->ttc, 0, ' ', ' ')}}</td>
                    
                            <td class="align-middle">
							    @if( $prestations->etat ==  "Proforma")
									
                                  <div class="badge badge-warning badge-shadow">{{$prestations->etat}}</div>
							    @elseif ( $prestations->etat ==  "Validée")
                                      <div class="badge badge-success badge-shadow">{{$prestations->etat}}</div>
								@else
                                      <div class="badge badge-danger badge-shadow">{{$prestations->etat}}</div>
                                @endif
								  
                            </td>
                            <td>
							 @if( $prestations->etat ==  "Proforma")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$prestations->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" target="_blank" href="{{route('factureprestation',['id'=>$prestations->id])}}" data-toggle="tooltip" data-placement="bottom" title="Imprimer" class="btn btn-icon btn-info"><i class="fas fa-file"></i></a>
							<a type="button" href="{{route('consultation_prestation',['id'=>$prestations->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							 @if( $prestations->etat ==  "Proforma")
							<a type="button" href="#" data-id="{{$prestations->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( session('user')->roles ==  "Admin")
							@if( $prestations->etat ==  "Validée")
							<!--<a type="button" href="#" data-id="{{$prestations->id}}" data-toggle="tooltip" data-placement="bottom" title="Annuler" class="btn btn-icon btn-danger data-annuler"><i class="fas fa-times"></i></a>-->
							<a type="button" target="_blank" href="{{route('paiement-facture',['id'=>$prestations->id])}}" data-toggle="tooltip" data-placement="bottom" title="Ajouter un paiement" class="btn btn-icon btn-warning"><i class="fas fa-dollar"></i>Paiement</a>
							@endif
							@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 prestation</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 enregistrement ?
              </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 enregistrement ?
              </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 enregistrement ?
              </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/Prestation/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerprestationPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.prestation.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   <script>
        $(".data-annuler").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Prestation/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('AnnulerprestationPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.prestation.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#annuler').html('').append(html);
          })
          $('#Annulermodal').modal();
        });
    </script>
	    <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Prestation/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerprestationPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.prestation.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').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="padding: 12px">
                  
				   <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> 
					<div class="form-group">
                      <label>Désignation <font color="red">*</font></label>
                      <input title="saisir la désignation" type="text" class="form-control" name="prestation[`+ i +`][designation][]">
                    </div>
					</div>
					 <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12"> 
					
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité" type="text" class="form-control" name="prestation[`+ i +`][qte][]">
                    </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) <font color="red">*</font></label>
                      <input title="saisir le prix unitaire" type="text" class="form-control" name="prestation[`+ i +`][pu][]">
                    </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>
        $( "#addprestation").on('click', function( e ) {
        e.preventDefault();
        var data = {};
		var isValide = true;
        data.client_id = $("select[name=client_id]").val();
        data.taxe = $("select[name=taxe]").val();
        data.datep = $("input[name=datep]").val();
        data.remise = $("input[name=remise]").val();
        data.prestation = [];

        if(!data.client_id){
            toastr.error('Veuillez sélectionner le client', 'Attention !', {timeOut: 5000})
            return false;
        }
		if(!data.taxe){
            toastr.error('Veuillez préciser si la taxe doit être appliquée', 'Attention !', {timeOut: 5000})
            return false;
        }

        $("[name*=prestation]").each(function(index, element){
                if($(element).val() != ""){
					if(!String($(element).val())){
						isValide = false; 
						toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
					}
					console.log($(element).val());
                    data.prestation.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.client_id && data.prestation.length ){
            $.post("{{route('prestationPost')}}", 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