@extends('layout')
@section('title','Ajuster stock ')
@section('content')
<div class="section-header">
            <h1>Ajuster stock</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Ajuster stock</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>Ajuster stock</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-12"> 
					  <div class="form-group">
                      <label>Magasin <font color="red">*</font></label>
                      <input type="text" class="form-control datepicker" name="magasin" value="{{$magasin}}" readOnly>
                    </div>
					</div>
                    <div class="col-lg-3 col-md-12"> 
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <input type="text" class="form-control" name="produit" value="{{DB::table('produit_vendable')->where('id', $produit)->first()->designation}}" readOnly>
                    </div>
					</div>
					@php
						$d = date('d/m/Y');
					@endphp
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Inventaire ID.</label>
                      <input type="text" class="form-control datepicker" name="inventaire" value="{{$inventaire}}" readOnly>
                    </div>
					</div>
					<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> 
					
					<div class="form-group">
                      <label>Quantité à ajuster<font color="red">*</font></label>
                      <input title="sélectionner la quantité" type="text" class="form-control" name="qte">
                    </div>
					</div>
                      </div> 
                   <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="addajust"><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>Magasin</th>
                            <th>Date</th>
                            <th>Produit</th>
                            <th>Qté</th>
                            <th>Etat</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($ajustement as $ajustements)
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$ajustements->numero}}</td>
							<td>{{DB::table('magasin')->where('id', $ajustements->magasin_id)->first()->nom}}</td>
							 @php
                                $dd = implode('/', array_reverse(explode('-', $ajustements->date)));
                              @endphp
                            <td>{{$dd}}</td>
                            <td>{{DB::table('produit_vendable')->where('id', $ajustements->produit_id)->first()->designation}}</td>
                            <td>{{$ajustements->qte}}</td>
                            <td class="align-middle">
							    @if( $ajustements->etat ==  "Proforma")	
                                  <div class="badge badge-warning badge-shadow">{{$ajustements->etat}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$ajustements->etat}}</div>
                                @endif 
                            </td>
                            <td>
							 @if( $ajustements->etat ==  "Non validé")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$ajustements->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="#" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger"><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="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>
	   @push('scripts') 
	   
	    <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/validerajust/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerajustPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.ajust.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script>
	 <script>
        $( "#addajust").on('click', function( e ) {
        e.preventDefault();
        var data = {};

        data.inventaire = $("input[name=inventaire]").val();
        data.magasin = $("input[name=magasin]").val();
        data.produit = $("input[name=produit]").val();
        data.qte = $("input[name=qte]").val();
		
        if(!data.qte){
            toastr.error('Veuillez saisir la quantité', 'Attention !', {timeOut: 5000})
            return false;
        }

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

        if(data.qte){
            $.post("{{route('ajustPost')}}", 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