@extends('layout')
@section('title','Modification produit fini ')
@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 produit fini (<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>Produit  <font color="red">*</font></label>
                      <select class="form-control select2" name="pv">
                        <option value="{{$pf->pv_id}}">{{DB::table('produit_vendable')->where('id',$pf->pv_id)->first()->designation}}</option>
						 @foreach($pv as $pvs) 
						<option value="{{$pvs->id}}">{{$pvs->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
                     <div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Désignation <font color="red">*</font></label>
                      <input type="text" class="form-control" name="designation" value="{{$pf->designation}}">
                    </div>
					</div>
					
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Stock minimal <font color="red">*</font></label>
                      <input type="text" class="form-control" name="stock_mini" value="{{$pf->stock_mini}}">
                    </div>
					</div>
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Prix vip <font color="red">*</font></label>
                      <input type="text" class="form-control" name="pvip" value="{{$pf->prix_vip}}">
                    </div>
					</div>
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Prix ordinaire <font color="red">*</font></label>
                      <input type="text" class="form-control" name="pordi" value="{{$pf->prix_ordinaire}}">
                    </div>
					</div>
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Unité <font color="red">*</font></label>
                      <input type="text" class="form-control" name="unite_mesure" value="{{$pf->unite_mesure}}">
                    </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="editproduitfini"><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 produits</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">Désignation</th>
                            <th class="text-center">Prix ordi.</th>
                            <th class="text-center">Prix vip.</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($produit as $produits)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$produits->designation}}</td>
							<td style="text-align:center;">{{$produits->prix_ordinaire}}</td>
							<td style="text-align:center;">{{$produits->prix_vip}}</td>
							<td>
							
              <a type="button" href="{{route('modification-produit-fini',['id'=>$produits->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="{{$produits->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 produit fini ?
              </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+'/Produitfini/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerpfPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.prodfi.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
	   
	   
		<script>
		$( "#editproduitfini").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.pv = $("select[name=pv]").val();
            data.designation = $("input[name=designation]").val();
			data.stock_mini = $("input[name=stock_mini]").val();
            data.pordi = $("input[name=pordi]").val();
            data.pvip = $("input[name=pvip]").val();
            data.unite_mesure = $("input[name=unite_mesure]").val();

			if(!data.pv){
				toastr.error('Veuillez sélectionner le produit ', 'Erreur !', {timeOut: 5000})
				return false;
			}

            if(!data.designation){
				toastr.error('Veuillez saisir la designation ', 'Erreur !', {timeOut: 5000})
				return false;
			}
            if(!data.stock_mini){
				toastr.error('Veuillez spécifier le stock minimal', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
			
			 if(!data.pvip){
				toastr.error('Veuillez spécifier le prix vip', 'Erreur !', {timeOut: 5000})
				return false;
			}
			 if(!data.pordi){
				toastr.error('Veuillez spécifier le prix ordinaire', 'Erreur !', {timeOut: 5000})
				return false;
			}
			 if(!data.unite_mesure){
				toastr.error('Veuillez saisir l\'unité', 'Erreur !', {timeOut: 5000})
				return false;
			}
			

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

			if(data.pv && data.designation && data.stock_mini  && data.pvip && data.pordi && data.unite_mesure){
				$.post("{{route('modifierpfPost', ['id'=>$idpf])}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 5000})
						window.location.href = '/produit/produit_fini';
					}else{
					     toastr.success(result.response, result.title, {timeOut: 15000})
					 	window.location.href = '/produit/produit_fini';
					}

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

@endsection