@extends('layout')
@section('title','Modification client ')
@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/>
                <div class="card">
                  <div class="card-header">
                    <h4>Modifier un client (<h7><strong>Les champs en * sont obligatoires</strong></h7>)</h4>
                  </div>
                  <div class="card-body">
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
                     <div class="col-lg-5 col-md-5"> 
					  <div class="form-group">
                      <label>Nom complet <font color="red">*</font></label>
                      <input type="text" class="form-control" name="nom" style="text-transform:uppercase" value="{{$clien->noms}}">
                    </div>
					</div>
					 <div class="col-lg-3 col-md-3"> 
					 <div class="form-group">
                      <label>Téléphone <font color="red">*</font></label>
                      <input type="text" class="form-control" name="tel" value="{{$clien->tel}}" >
                    </div>
					
					</div>
					 <div class="col-lg-4 col-md-4"> 
					 <div class="form-group">
                      <label>Adresse </label>
                      <input type="text" class="form-control" name="adresse" value="{{$clien->adresse}}" >
                    </div>
					
					</div>
                      </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="editclient"><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 clients externes</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">Noms</th>
                            <th class="text-center">Téléphone</th>
                            <th  style="text-align:center;">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($client as $clients)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$clients->noms}} </td>
							<td style="text-align:center;">{{ $clients->tel }} </td>
							
							<td style="text-align:center;"> 
							
								<a type="button" href="{{route('modifierclient',['id'=>$clients->id])}}" data-toggle="tooltip" data-placement="bottom" title="Modifier" class="btn btn-icon btn-primary"><i class="fas fa-edit"></i></a>
					
							    <!--<a type="button" href="#" data-id="{{$clients->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 client ?
              </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/Client/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerclientPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.cli.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
		<script>
		$( "#editclient").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.nom = $("input[name=nom]").val();
			data.tel = $("input[name=tel]").val();
			data.adresse = $("input[name=adresse]").val();

            if(!data.nom){
				toastr.error('Veuillez saisir le nom du client ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			 if(!data.tel){
				toastr.error('Veuillez saisir le numéro du client ', 'Erreur !', {timeOut: 5000})
				return false;
			}

			data._token = "{{csrf_token()}}";
			
			                 @php
                               $idclient = $clien->id;
                             @endphp

			if(data.nom && data.tel ){
				$.post("{{route('modifierclientPost', ['id'=>$idclient])}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 5000})
						window.location.href = '/es/client_externe/nouveau_client';
					}else{
					     toastr.success(result.response, result.title, {timeOut: 15000})
						window.location.href = '/es/client_externe/nouveau_client';
					}

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

@endsection