@extends('layout')
@section('title','Production')
@section('content')
<div class="section-header">
            <h1>Production</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Production</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>Nouveau</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Les champs en * sont obligatoires</strong></h7>
				  
				<form  action="" method="POST">
                         {{csrf_field()}}
				<input type="hidden" value="{{$produit}}" id="produits">
				  <div class="row venteProduits" style="padding-top: 30px">
					<div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Catégorie <font color="red">*</font></label>
                                    <select title="sélectionner la catégorie" class="form-control select2 selectcat-0"
                                         name="peps[0][categorie][]" required>
                                        <option>Sélectionner la catégorie</option>
                                        @foreach($categorie as $cats)
                                        <option value="{{$cats->idcat}}">{{$cats->libelle}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
					<div class="col-lg-3 col-md-3"> 
					  <div class="form-group">
                      <label>Produit obtenu <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2"
                                        id="selectproduit-0" name="produit_obtenu">
						<option>Sélectionner le produit</option>
                      </select>
                    </div>
					</div>
					<div class="col-lg-2 col-md-2"> 
					 <div class="form-group">
                      <label>Qté</label>
                      <input type="text" class="form-control" name="qte_obtenu">
                    </div>
					</div>
					@php
						$d = date('d/m/Y');
					@endphp
					<div class="col-lg-3 col-md-3"> 
					 <div class="form-group">
                      <label>Date</label>
                      <input type="text" class="form-control" name="date" id="champ_date2">
					  <div id="calendarMain2"></div>
                    </div>
					</div>
                      </div>
					   <hr>
					  <div class="row" style="margin-bottom:0px">
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> 
					  <div class="form-group">
                      <label>Matière <font color="red">*</font></label>
                      <select title="sélectionner le type d'emballage" class="form-control select2" name="approvision[0][matiere][]">
                        <option>Sélectionner matière</option>
						 @foreach($matiere as $matieres)
						<option value="{{$matieres->id}}">{{$matieres->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> 
					
					<div class="form-group">
                      <label>Quantité<font color="red">*</font></label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[0][qte][]">
                    </div>
					</div>
				
					 <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" data-placement="bottom" title="Ajouter produit" type="button" class="btn btn-icon btn-primary" id="addNewblock"><i class="fas fa-plus"></i></button>
                    
					</div>
				
                      </div>
					  <div class="addNouveau"></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="addappro"><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</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;">Produit fini obtenu</th>
							<th style="text-align:center;">Quantité produit</th>
							<th style="text-align:center;">Etat</th>
							<th style="text-align:center;">Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($production as $production)
						 @php
						 $cat_id = DB::table('produit_vendable')->where('id',$production->produit_id)->first()->categorie_id;
                         @endphp 
						 <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$production->date}}</td>
							<td style="text-align:center;">
							{{DB::table('categorie')->where('idcat',$cat_id)->first()->libelle}}/
							{{DB::table('produit_vendable')->where('id',$production->produit_id)->first()->designation}}
							</td>
							<td style="text-align:center;">{{$production->qte}}</td>
                    
                            <td style="text-align:center;">
							    @if( $production->etat ==  "Non validé")
                                    <div class="badge badge-warning badge-shadow">{{$production->etat}}</div>
							    @elseif( $production->etat ==  "Annulé")
                                    <div class="badge badge-danger badge-shadow">{{$production->etat}}</div>
								@else
                                    <div class="badge badge-success badge-shadow">{{$production->etat}}</div>
                                @endif
								  
                            </td>
                            <td>
							@if( session('user')->roles ==  "Admin")
							@if( $production->etat ==  "Non validé")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$production->id}}" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							<a type="button" href="#" data-id="{{$production->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							<a type="button" href="{{route('modifier-production',['id'=>$production->id])}}" data-toggle="tooltip" data-placement="bottom" title="Modifier" class="btn btn-icon btn-info"><i class="fas fa-edit"></i></a>
							@endif
							@if( $production->etat ==  "Validé")
							<a type="button" href="#" data-id="{{$production->id}}" data-toggle="tooltip" data-placement="bottom" title="Annuler" class="btn btn-icon btn-danger data-annuler"><i class="fas fa-times"></i></a>
							@endif
							@endif
							<a type="button" href="{{route('detail-production',['id'=>$production->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></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</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 cette production?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">
                
              </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 production?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="supprimer">
                
              </div>
            </div>
          </div>
        </div>
		<div class="modal fade" id="Annulermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Annulation</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 annuler cette production ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="annuler">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   
<script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/validerprod/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerprodPost')}}" method="post" style="display: inline">
                  {{csrf_field()}}
                      <input type="hidden" value="`+data.production.id+`" name="id" >
              <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script>

<script>
        $(".data-annuler").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Annulerproduction/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
            <form action="{{route('AnnulerproductionPost')}}" method="post" style="display: inline">
                {{csrf_field()}}
                <input type="hidden" value="`+data.prod.id+`" name="id" >
            <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#annuler').html('').append(html);
          })
          $('#Annulermodal').modal();
        });
    </script>
	<script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Supprimerproduction/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerproductionPost')}}" method="post" style="display: inline">
                  {{csrf_field()}}
                      <input type="hidden" value="`+data.production.id+`" name="id" >
              <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#supprimer').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
	   <script>

            function removeBlock() {
               $(".deleteblock").on("click", function () {
                           $(this).parent().parent().remove()
                       })
              }

              var i=0;
               $("#addNewblock").on("click", function ()
                {
                   i++;
                   var html = `
				   
				   <div class="row" style="margin-top:-20px">
                  
				   <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> 
					
					  <div class="form-group">
                      <label>Matière <font color="red">*</font></label>
                      <select title="sélectionner matière" class="form-control select2" name="approvision[`+ i +`][matiere][]">
                        <option value="">Sélectionner matiere</option>
						
						 @foreach($matiere as $matieres)
						 
						<option value="{{$matieres->id}}">{{$matieres->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> 
					<div class="form-group">
                      <label>Quantité<font color="red">*</font></label>
                      <input title="saisir la quantité" type="text" class="form-control" name="approvision[`+ i +`][qte][]">
                    </div>
					</div>
					<div class="col-lg-1 col-md-1 col-sm-1 col-xs-3" style="padding: 30px"> 
						<button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i></button>
					</div>
					
		 </div>
				  
                       `;

                   $('.addNouveau').append(html);
				   $('.select2').select2();
                   removeBlock();

             });
    </script>
	<script>

	function selectcat(iteration = 0) {
		$('.selectcat-'+iteration).change(function () {

			var typenivo11 = $('#produits').val();
			console.log(typenivo11)
			var typeselectionner11 = $(this).val();
			typenivo11 = JSON.parse(typenivo11);
			typenivo11 = typenivo11.filter(function (x) {
				return x.categorie_id == typeselectionner11;
			});
			var option = ' <option value="">Sélectionner le produit</option> ';
			for (var i = 0; i < typenivo11.length; i++) {
				option += `
			<option value="`+ typenivo11[i].id + `">` + typenivo11[i].designation + ` </option>
			`;

			}
			$('#selectproduit-'+iteration).html(option);

		})
	}

	selectcat(0);
    

</script>
	 <script>
        $( "#addappro").on('click', function( e ) {
        e.preventDefault();
        var data = {};
		var isValide = true;
        data.produit_obtenu = $("select[name=produit_obtenu]").val();
        data.qte_obtenu = $("input[name=qte_obtenu]").val();
        data.date = $("input[name=date]").val();
        data.approvision = [];

        if(!data.produit_obtenu){
            toastr.error('Veuillez sélectionner le produit obtenu', 'Attention !', {timeOut: 5000})
            return false;
        }
		 if(!data.qte_obtenu){
            toastr.error('Veuillez saisir la quantité obtenue', 'Attention !', {timeOut: 5000})
            return false;
        }
		if(!data.date){
            toastr.error('Veuillez sélectionner la date', 'Attention !', {timeOut: 5000})
            return false;
        }

        $("[name*=approvision]").each(function(index, element){
                if($(element).val() != ""){
					if(!Number($(element).val())){
						isValide = false; 
						toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
					}
					console.log($(element).val());
                    data.approvision.push($(element).val());
                }else{
                    toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
                   console.log($(element));
				   isValide = false;
                    return false;
                }
         });

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

        if(data.produit_obtenu && data.approvision.length && isValide){
            $.post("{{route('productionPost')}}", 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