@extends('layout')
@section('title','Transfert  ')
@section('description','Transfert vers magasin de vente ')
@section('content')
<div class="section-header">
            <h1>Transferts </h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Transfert produits </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-lg-12 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Nouveau transfert</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" style="padding-top: 30px">
						<div class="col-lg-4 col-md-12">
					<div class="form-group">
                      <label>Transfert du magasin </label>
					   <select  class="form-control select2" name="magasinx">
                        <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-4 col-md-12"> 
					  <div class="form-group">
                      <label>Vers<font color="red">*</font></label>
                      <select  class="form-control select2" name="magasiny">
                        <option value="">Sélectionner un magasin</option>
						
						   @foreach($magasin2 as $magasin2s)
							<option value="{{$magasin2s->id}}">{{DB::table('magasin')->where('id', $magasin2s->id)->first()->nom}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
					
					@php
						$d = date('d/m/Y');
					@endphp
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date du transfert</label>
                      <input type="date" class="form-control datepicker" name="datetransfert">
                    </div>
					
					</div>
					</div>
					 <div class="row venteProduits">
                            <div class="col-lg-4 col-md-12">
                                <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="transmag[0][categorie][]" required>
                                        <option>Sélectionner la catégorie</option>
                                        @foreach($cat as $cats)
                                        <option value="{{$cats->idcat}}">{{$cats->libelle}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                    <div class="col-lg-4 col-md-12">
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2"
                            id="selectproduit-0" name="transmag[0][produit][]">
                        <option>Sélectionner le produit</option>
                      </select>
                    </div>
					</div>
					<div class="col-lg-2 col-md-12">
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité " type="text" class="form-control" name="transmag[0][qte][]">
                    </div>
					</div>
					
				 <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" 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-primary" id="addtransfert"><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>Derniers transferts</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">Référence</th>
							<th class="text-center">du magasin</th>
                            <th class="text-center">Vers </th>
							 <th class="text-center">Date </th>
							  <th class="text-center">Etat d'envoi</th>
							  <th class="text-center">Etat reception</th>
							  <th class="text-center">Enregistré par </th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($transfert as $transferts)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							
							<td style="text-align:center;">{{ $transferts->numero}} </td>
							
							<td style="text-align:center;">{{DB::table('magasin')->where('id', $transferts->magasinx_id)->first()->nom}}</td>
							
							<td style="text-align:center;">{{DB::table('magasin')->where('id', $transferts->magasiny_id)->first()->nom}}</td>
							
							@php
							
							$dd = implode('/', array_reverse(explode('-', $transferts->date)));
							$user = DB::table('users')->where('id', $transferts->user_id)->get()->count();
							if ($user >0){
							$user1 =DB::table('users')->where('id', $transferts->user_id)->first()->nom;
							}else{
								$user1 ="";
							}
							@endphp
							<td style="text-align:center;">{{$dd}} </td>
							 <td class="text-center">
							    @if( $transferts->etat1 ==  "En cours")
                                  <div class="badge badge-warning badge-shadow">{{$transferts->etat1}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$transferts->etat1}}</div>
                                @endif
								  
                            </td>
							<td class="text-center">
							    @if( $transferts->etat2 ==  "En cours")
									
                                  <div class="badge badge-warning badge-shadow">{{$transferts->etat2}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$transferts->etat2}}</div>
                                @endif
								  
                            </td>
                            <td style="text-align:center;">
                                
                                {{$user1}}
                            </td>
                            <td>
							 @if( $transferts->etat1 ==  "En cours")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$transferts->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="{{route('consultation_transfert_boutique',['id'=>$transferts->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							 @if( $transferts->etat1 ==  "En cours")
							<a type="button" href="#" data-id="{{$transferts->id}}" data-toggle="tooltip" 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="Validermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Validation du transfert</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <div class="progress progress-lg" id="pg" style="display:none">
					<div class="progress-bar progress-bar-warning progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%;height:50px">
						<br>Traitement en cours, veuillez patienter...
					</div>
				</div>
                <p id="nn" style="text-align:center">Voulez-vous valider ce transfert ?</p>
            </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 définitivement ce transfert ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   <script>
	function myFunction() {
		$('#bregister').hide();
		$('#bregister2').hide();
		$('#bregister3').hide();
		$('#pg').show();
		$('#mm').hide();
		$('#nn').hide();
		//$("#nn").attr("value", "Traitement en cours, veuillez patienter...");
		//document.getElementById("nn").value = "Traitement en cours, veuillez patienter...";
		//document.getElementById("nn").innerHTML = "Traitement en cours, veuillez patienter...";
	}
	</script>
	     <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/validertransfertboutique/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal" id="bregister2">Non</button>
                      <form action="{{route('validertransferteffecueboutiquePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.tran.id+`" name="id" >
                      <button type="submit" class="btn btn-success" onclick="myFunction()" id="bregister">Oui</button>

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

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

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

              var iteration = 1;
    $("#addNewblock").on("click", function () {
        var venteProduitsDiv = `<div class="row venteProduits">
                            <div class="col-lg-4 col-md-12">
                                <div class="form-group">
                                    <label>Catégorie <font color="red">*</font></label>
                                    <select title="sélectionner la catégorie" class="form-control select2 selectcat-`+ iteration +`"
                                         name="transmag[`+ iteration +`][categorie][]" required>
                                        <option>Sélectionner la catégorie</option>
                                        @foreach($cat as $cats)
                                        <option value="{{$cats->idcat}}">{{$cats->libelle}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-lg-4 col-md-12">
                                <div class="form-group">
                                    <label>Produit <font color="red">*</font></label>
                                    <select title="sélectionner le produit" class="form-control select2"
                                        id="selectproduit-`+ iteration +`" name="transmag[`+ iteration +`][produit][]" required>
                                        <option>Sélectionner le produit</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-2 col-md-12">
                                <div class="form-group">
                                    <label>Qte<font color="red">*</font></label>
                                    <input title="saisir la quantité" type="text" class="form-control"
                                        name="transmag[`+ iteration +`][qte][]" required>
                                </div>
                            </div>
					
					  <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i></button>
                   
					</div>
					
		 </div>
				  
                       `;

                  console.log(venteProduitsDiv)
        $('.addNouveau').append(venteProduitsDiv);
        if ($(".form-control select2").length) {
            $(".form-control select2").select2();
        }

        $(".addNouveau").find(".venteProduits").each(function (index, element) {
            //$(element).find(".form-control select2").children().eq(3).remove();

        });

		selectcat(iteration);
		$('.select2').select2();
        removeBlock();
		iteration++;

    });
    </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++) {
				var pvv = typenivo11[i].prix_detail * 0.2;
				var pvv1 = typenivo11[i].prix_detail + pvv;
				option += `
			<option value="`+ typenivo11[i].id + `">` + typenivo11[i].desi + ` / ` + typenivo11[i].code + ` / ` + typenivo11[i].marque + ` / ` + pvv1 + ` </option>
			`;

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

		})
	}

	selectcat(0);
    

</script>
		<script>
		$( "#addtransfert").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			var isValide = true;
			data.magasinx = $("select[name=magasinx]").val();
			data.magasiny = $("select[name=magasiny]").val();
			data.datetransfert = $("input[name=datetransfert]").val();
			data.transmag = [];
			if(!data.magasinx){
				toastr.error('Veuillez sélectionner le magasin de départ ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.magasiny){
				toastr.error('Veuillez sélectionner le magasin vers lequel les produits seront tranférés ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
           $("[name*=transmag]").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.transmag.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.magasinx && data.magasiny && data.transmag.length && isValide ){
				$.post("{{route('nouveau_transfertboutiquePost')}}", 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