@extends('layout')
@section('title','Nouveau magasin ')
@section('content')
		<br/>
          <div class="section-body">
		  <meta name="_token" content="{{ csrf_token() }}" />
            <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 magasin (<h7><strong>Les champs en * sont obligatoires</strong></h7>)</h4>
                  </div>
                  <div class="card-body">
				<form action="" method="POST" id="formulaire" enctype="multipart/form-data">
                {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
                    <div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Nom du magasin <font color="red">*</font></label>
                      <input type="text" class="form-control" name="nom" required>
                    </div>
					</div>
					@php
				        $d = date('Y-m-d');
					@endphp
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Numéros de téléphones <font color="red">*</font></label>
                      <input type="text" class="form-control" name="telephone" required>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Email <font color="red">*</font></label>
                      <input type="text" class="form-control" name="email" required>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Adresse <font color="red">*</font></label>
                      <input type="text" class="form-control" name="adresse" required>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Logo <font color="red">*</font></label>
					  <input type="file" id="my-file" class="form-control" name="my-file" required>
                    </div>
					</div>
                      </div>
					 <hr>  	
                   <div class="pull-left">
                    <input type="submit" class="btn btn-icon icon-left btn-success" id="bRegister" value="Enregistrer">
                  </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 magasins</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">Nom</th>
                            <th class="text-center">Téléphones</th>
                            <th class="text-center">Email</th>
                            <th class="text-center">Adresse</th>
                            <th class="text-center">Date création</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($magasin as $magasins)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$magasins->nom}}</td>
							<td style="text-align:center;">{{$magasins->telephone}}</td>
							<td style="text-align:center;">{{$magasins->email}}</td>
							<td style="text-align:center;">{{$magasins->adresse}}</td>
							<td style="text-align:center;">{{$magasins->date}}</td>
							<td style="text-align:center;">
							<a type="button" href="{{route('modifier-magasin',['id'=>$magasins->id])}}" data-toggle="tooltip" data-placement="bottom" title="Modifier infos" class="btn btn-icon btn-info"><i class="fas fa-edit"></i></a>
							<a type="button" href="{{route('modifier-magasin-logo',['id'=>$magasins->id])}}" data-toggle="tooltip" data-placement="bottom" title="Modifier logo" class="btn btn-icon btn-warning"><i class="fas fa-edit"></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 cet approvisionnement ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
		<script>
			$(document).ready(function(){
				$("#formulaire").on("submit", function(e) {
				e.preventDefault();
				
				var extension = $('#my-file').val().split('.').pop().toLowerCase();
				var file_data = $('#my-file').prop('files')[0];
				var nom = $("input[name=nom]").val();
				var adresse = $("input[name=adresse]").val();
				var telephone = $("input[name=telephone]").val();
				var email = $("input[name=email]").val();	

			var form_data = new FormData();
			form_data.append('file', file_data);
			form_data.append('nom', nom);
			form_data.append('adresse', adresse);
			form_data.append('telephone', telephone);
			form_data.append('email', email);
	        	
				$.ajaxSetup({
				headers: {
					'X-CSRF-Token': $('meta[name=_token]').attr('content')
				}
			});
				$.ajax({
				url: "{{url('nouveau_magasinPost')}}", // point to server-side PHP script
				data: form_data,
				type: 'POST',
				dataType: 'JSON',
				contentType: false, // The content type used when sending data to the server.
				cache: false, // To unable request pages to be cached
				processData: false,
				beforeSend: function(){
					$("#bRegister").attr("value", "Traitement en cours, veuillez patienter...");
				},
				success: function(data) {
					if(data.type == 'error'){
						toastr.error(data.response, data.title, {timeOut: 15000})
					} else {
					
					    toastr.success(data.response, data.title, {timeOut: 15000})
						$("#bRegister").attr("value", "Enregistrer");
						window.location.reload();
						//window.location.href = '/succes-demande';
					}
				}
                })
				});
				});
			
	    </script>
@endpush

@endsection