@extends('layout')
@section('title','Nouveau produit ')
@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 un nouveau produit (<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-4"> 
					  <div class="form-group">
                      <label>Catégorie <font color="red">*</font></label>
                      <select class="form-control select2" name="categorie">
                        <option value="">Sélectionner la catégorie</option>
						 @foreach($categorie as $categories) 
						<option value="{{$categories->idcat}}">{{$categories->libelle}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Référence </label>
                      <input type="text" class="form-control" name="code">
                    </div>
					</div>
                     <div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Désignation <font color="red">*</font></label>
                      <input type="text" class="form-control" name="designation">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Prix de revient/d'achat FCFA<font color="red">*</font></label>
                      <input type="text" class="form-control" name="pr" placeholder="">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Prix de revient/d'achat En Devise<font color="red">*</font></label>
                      <input type="text" class="form-control" name="pr_en_devise" placeholder="">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Prix détail<font color="red">*</font></label>
                      <input type="text" class="form-control" name="prix_detail" placeholder="">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4" hidden> 
					  <div class="form-group">
                      <label>Prix semi <font color="red">*</font></label>
                      <input type="text" class="form-control" name="prix_semigros" value="0">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Prix d'en gros <font color="red">*</font></label>
                      <input type="text" class="form-control" name="prix_gros" placeholder="">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4" hidden> 
					  <div class="form-group">
                      <label>Marque </label>
                      <input type="text" class="form-control" style="text-transform:uppercase" name="marque">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Stock minimal <font color="red">*</font></label>
                      <input type="text" class="form-control" name="stock_mini">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4" hidden> 
					  <div class="form-group">
                      <label>Caisse off <font color="red">*</font></label>
                      <select class="form-control select2" name="caisseoff">
                        <option value="Non">Non</option>
                        <option value="Oui">Oui</option>
                      </select>
                    </div>
					</div>
					<div class="col-lg-3 col-md-3"> 
					  <div class="form-group">
                      <label>Contenu du carton(en KG) <font color="red">*</font></label>
                      <input type="text" class="form-control" name="nbreencarton" value="1" placeholder="">
                    </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="addproduit"><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">Code</th>
                            <th class="text-center">Prix de revient</th>
                            <th class="text-center">Prix détail</th>
							<th class="text-center">Prix gros</th>
                            <th  style="text-align:center;">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($produit as $produits)
						@php
						$pdetail =  number_format($produits->prix_detail, 0, ' ', ' ');
						$pgros =  number_format($produits->prix_gros, 0, ' ', ' ');
						@endphp
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$produits->designation}}</td>
							<td style="text-align:center;">{{$produits->code}}</td>
							<td style="text-align:center;">{{number_format($produits->pr, 0, ' ', ' ')}}</td>
							<td style="text-align:center;">{{$pdetail}} </td>
							<td style="text-align:center;">{{$pgros}} </td>
							<td style="text-align:center;">
								<a class="btn btn-info" href="{{route('modifierproduit',['id'=>$produits->id])}}">
									<i class="fas fa-edit"></i>
								</a>
								<a class="btn btn-success" href="{{route('emballage-produit',['id'=>$produits->id])}}" data-toggle="tooltip" data-placement="bottom" title="Emballage(s) de production">
									<i class="fas fa-clipboard"></i>
								</a>
								<a target="_blank" class="btn btn-warning" href="{{route('listeproduitmagpdf22',['id'=>$produits->id])}}">
                                    <i class="fas fa-eye"></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  ?
              </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/Produit/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerproduitPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.produi.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
		<script>
		$( "#addproduit").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.categorie = $("select[name=categorie]").val();
			data.code = $("input[name=code]").val();
            data.designation = $("input[name=designation]").val();
            data.prix_detail = $("input[name=prix_detail]").val();
            data.pr = $("input[name=pr]").val();
            data.pr_en_devise = $("input[name=pr_en_devise]").val();
            data.prix_gros = $("input[name=prix_gros]").val();
            data.prix_semigros= $("input[name=prix_semigros]").val();
			data.stock_mini = $("input[name=stock_mini]").val();
			data.marque = $("input[name=marque]").val();
			data.nbreencarton = $("input[name=nbreencarton]").val();
			data.caisseoff = $("select[name=caisseoff]").val();

			if(!data.designation){
				toastr.error('Veuillez saisir la désignation', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.categorie){
				toastr.error('Veuillez sélectionner une catégorie ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.prix_detail){
				toastr.error('Veuillez saisir le prix de détail', 'Erreur !', {timeOut: 5000})
				return false;
			}
		
			 if(!data.stock_mini){
				toastr.error('Veuillez spécifier le stock minimal alerte', 'Erreur !', {timeOut: 5000})
				return false;
			}

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

			if(data.categorie && data.designation && data.prix_detail && data.stock_mini){
				$.post("{{route('nouveauproduitPost')}}", 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.reload();
					}

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

@endsection