@extends('layout')
@section('title','Entrée - sortie caisse ')
@section('description','Entrée - sortie caisse')
@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/>
		@if(session('user')->roles == "Admin")
		<div class="card">
                  <div class="card-header">
                    <h4>Nouveau transfert</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Veuillez d'abord sélectionner le magasin, la date début et la date fin</strong></h7>
				  
				<form  action="{{route('recherche-caisse-depense')}}" method="POST">
                         {{csrf_field()}}
				
				  <div class="row" style="padding-top: 30px">
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                     <label>Magasin<font color="red">*</font></label>
                      <select  class="form-control select2" name="magasin_id" required>
                        <option value="">Sélectionner un magasin</option>
						@foreach($magasin as $magasins)
                        <option value="{{$magasins->id}}">{{DB::table('magasin')->where('id', $magasins->id)->first()->nom}}</option>
                        @endforeach
                      </select>
                    </div>
					</div>
				
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date début</label>
                      <input type="date" class="form-control" name="date_debut">
                    </div>
					
					</div>
					
					<div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date fin</label>
                      <input type="date" class="form-control" name="date_fin">
                    </div>
					
					</div>
					
					 <div class="col-lg-2 col-md-12" style="margin-top: 30px"> 
					
					 <button class="btn btn-icon  btn-primary " ><i  class="fa fa-search"></i> Rechercher</button>
					</div>
					
					
					</div>
		
                  
            
			<br><br>
					  
				</form>
					  
					    </div>
						 
                    </div>
		<br/>
		@endif
		@if(session('user')->roles == "Caissier")
                <div class="card">
                  <div class="card-header">
                    <h4>Enregistrer une dépense (<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-6 col-md-12"> 
					  <div class="form-group">
                      <label>Détails <font color="red">*</font></label>
                      <input type="text" class="form-control" name="note" placeholder="saisir les détails">
                    </div>
					</div>
					<div class="col-lg-6 col-md-12"> 
					  <div class="form-group">
                      <label>Montant <font color="red">*</font></label>
                      <input type="text" class="form-control" name="montant">
                    </div>
					</div>
					<div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date</label>
                      <input type="date" class="form-control" name="datedep">
                    </div>
					
					</div>
					
					@php
				        $d = date('Y-m-d');
					@endphp
                      </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="addsolde"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
					@endif
                  </div>
                </div>
				<div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Liste des dépenses</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th style="text-align:center;">N°</th>
							<th style="text-align:center;">Date</th>
							<th style="text-align:center;">Montant</th>
							<th style="text-align:center;">Note</th>
							<th style="text-align:center;">Etat</th>
							<th style="text-align:center;">Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($depense as $depenses)
						<tr>
							@php
							$dd = $depenses->date;
							$dd= explode('-', $dd);
							$dd= array_reverse($dd);
							$dd= implode('/', $dd);
							@endphp
							<td>{{$depenses->id}}</td>
							<td>{{$dd}}</td>
							<td style="text-align:center;font-size:12px;">
							@php
							$lhh = $depenses->montant;
							$lhh1 =  number_format($lhh, 0, ' ', ' ');
							echo $lhh1."&nbsp;F";
							@endphp
							</td>
							<td>{{$depenses->note}}</td>
							<td style="text-align:center;">
								@if( $depenses->etat == "En cours")
								<div class="badge badge-warning badge-shadow">{{$depenses->etat}}</div>
								@else
								<div class="badge badge-success badge-shadow">{{$depenses->etat}}</div>
								@endif
							</td>
							<td style="text-align:center;">
							@if( $depenses->etat == "En cours")
								<a type="button" href="#" data-toggle="tooltip" data-id="{{$depenses->id}}"
                                data-placement="bottom" title="Supprimer"
                                class="btn btn-icon btn-danger data-supprimer"><i
                                class="fas fa-trash"></i>
								</a>
							@endif
							</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 ?
            </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/Depense/' + id, function (data) {
            var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('supprimerdepPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+ data.depense.id + `" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

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

			if(!data.note){
				toastr.error('Veuillez préciser les détails', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.montant){
				toastr.error('Veuillez saisir le montant', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.datedep){
				toastr.error('Veuillez sélectionner la date', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
			data._token = "{{csrf_token()}}";
				
			if(data.montant){
				$.post("{{route('depensePost')}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 5000})
					}else{
					     toastr.success(result.response, result.title, {timeOut: 15000})
						 //window.location.href = '/liste_des_produits';
						 window.location.reload();
					}

				})
			}

		});
</script>
@endpush

@endsection