@extends('layout')
@section('title','Nouveau ticket ')
@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>Enregistrer une nouvelle vente de ticket (<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-4 col-md-12"> 
					  <div class="form-group">
                      <label>Client <font color="red">*</font></label>
                      <select class="form-control select2" name="client">
                        <option value="">Sélectionner le client</option>
						   @foreach($client as $clients)
							<option value="{{$clients->id}}">{{DB::table('client_externe')->where('id', $clients->id)->first()->noms}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
				    <div class="col-lg-3 col-md-12"> 
					  <div class="form-group">
                      <label>Type de ticket <font color="red">*</font></label>
                      <select class="form-control select2" name="typeticket">
                        <option value="">Type de ticket</option>
						   @foreach($typeticket as $typetickets)
							<option value="{{$typetickets->id}}">{{DB::table('ticket')->where('id', $typetickets->id)->first()->lib}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
					<div class="col-lg-3 col-md-12"> 
					  <div class="form-group">
                      <label>N° début plage <font color="red">*</font></label>
                      <input type="text" class="form-control" name="debut_plage">
                    </div>
					</div>
                    <div class="col-lg-3 col-md-12"> 
					  <div class="form-group">
                      <label>Quantite <font color="red">*</font></label>
                      <input type="text" class="form-control" name="qte">
                    </div>
					</div>
					  @php
						     $d = date('d/m/Y');
					  @endphp
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date </label>
                      <input type="text" class="form-control" name="dateventetic" id="champ_date" value="{{$d}}">
					  <div id="calendarMain"></div>
                    </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="addventeticket"><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 ventes de tickets</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"> Client </th>
                            <th class="text-center">Montant</th>
							 <th class="text-center">Type de ticket</th>
							  <th class="text-center">Quantité</th>
                            <th class="text-center">Date</th>
                            <th  style="text-align:center;">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($ticket as $tickets)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;"> {{DB::table('client_externe')->where('id', $tickets->client_externe_id)->first()->noms}}</td>
							@php
							
							   $lh1 =  number_format($tickets->montant, 0, ' ', ' ');
							
							@endphp
							<td style="text-align:center;">{{$lh1}} FCFA</td>
							<td style="text-align:center;">{{DB::table('ticket')->where('id', $tickets->type_ticket)->first()->lib}} FCFA</td>
							<td style="text-align:center;">{{$tickets->qte}}</td>
							<td style="text-align:center;">{{ implode('/', array_reverse(explode('-', $tickets->date))) }}</td>
							
							<td style="text-align:center;"> 
							
							    <a type="button" href="{{route('Detail-vente',['id'=>$tickets->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails vente " class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							
							    <a type="button" href="#" data-id="{{$tickets->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 cette vente de ticket ?
              </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+'/Detailticket/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-light" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerventeticketPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.venteticket.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
		<script>
		$( "#addventeticket").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.typeticket = $("select[name=typeticket]").val();
			data.client = $("select[name=client]").val();
			data.qte = $("input[name=qte]").val();
			data.dateventetic = $("input[name=dateventetic]").val();
			data.debut_plage = $("input[name=debut_plage]").val();

            if(!data.client){
				toastr.error('Veuillez séléctionner le client ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.typeticket){
				toastr.error('Veuillez séléctionner le type de ticket ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			  if(!data.debut_plage){
				toastr.error('Veuillez saisir le début de plage ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			  if(!data.qte){
				toastr.error('Veuillez saisir la quantité ', 'Erreur !', {timeOut: 5000})
				return false;
			}

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

			if(data.client && data.typeticket && data.debut_plage && data.qte){
				$.post("{{route('nouveauventeticketPost')}}", 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