@extends(Auth::check() && Auth::user()->role->layout == 1 ? 'layouts.admin' : 'layouts.employee')

@section('content')

<div class="content-wrapper">

	<section class="content-header">

	<h1>{{trans('general.credit')}}</h1>

	<select id="anyo">
		<option value="">Seleccione un año</option>
		<option value="{{ date('Y') }}">{{ date('Y') }}</option>
		<option value="{{ date('Y')+1 }}">{{ date('Y')+1 }}</option>
	</select>

	<select id="barco">
	@foreach($boats as $boat)
	    <option value="{{ $boat->id }}">{{ $boat->name }} {{ $boat->model }}</option>
	@endforeach
	</select>

	<a class="btn btn-large" id="cargar" >Ver créditos</a>

	<div class="row" style="margin:15px;">
		<a href="#" class="btn btn-xs disabled" style="background-color:#605ca8;color:#fff;">Laborable</a>
		<a href="#" class="btn btn-xs disabled" style="background-color:#ff851b;color:#fff;">Fin de semana</a>
	</div>



	</section>

	<section class="content">
		<div id="contenedor"></div>
		<br style="clear:both" />
		<div class="alert alert-success" style="display:none; margin-top: 20px" role="alert">
			<p>Creditos guardados satisfactoriamente</p>
		</div>
		<div class="alert alert-danger" style="display:none; margin-top: 20px" role="alert">
			<p>Oops! algo fue mal los creditos no han sido guardados</p>
		</div>
	</section>

</div>
@endsection


@section('foot')

<script src="{{ asset('/plugins/select2/select2.min.js') }}"></script>
<script src="{{ asset('plugins/jQueryUI/jquery-ui.min.js') }}"></script>
<script type="text/javascript">
	  // {{$url_to_request_get = (string) env('APP_URL','http://skipper.komunica.es' )}}
	  // const url_to_request_get = "{{  $url_to_request_get}}";
		// const url_to_request_get = "http://localhost:8000";
		const url_to_request_get = "https://skipper.komunica.es";
		//$(document).ready( function() {
		$(document).ready( function() {
				$('.alert-success').hide();
				$('.alert-danger').hide();
		})

		$('#cargar').on('click', function(){
		    barco = $('#barco option:selected').val();
			$.ajax({
			  url: url_to_request_get+"/getcreditos.php",
			  method: "GET",
			  data: { usuario : {{ $customer->id }} , boat : barco , admin : {{ Auth::user()->role->layout  }}  },
			  dataType: "html"
			}).done(function(data) {
			  $('#contenedor').html(data);
			});
		});

		$('body').on('click', '#saveCredits #saveButton', function(event){
			$.ajax({
					url: url_to_request_get+"/saveCreditos.php",
					method: "POST",
					data: $('#saveCredits').serialize(),
					dataType: "text"
			}).done(function(data) {
				$('.alert-success').show();
				setTimeout(function() {
				    $('.alert-success').fadeOut('fast');
				}, 3000);
			}).fail(function(data) {
				$('.alert-danger').show();
				setTimeout(function() {
						$('.alert-danger').fadeOut('fast');
				}, 3000);
			});
			return false;
		});

	function cambiar( id ) {
		var opcion =  $('#anyo option:selected').val();
		var barco = $('#barco option:selected').val();
		$.ajax({
			  url: url_to_request_get+"/changeCredito.php",
			  method: "GET",
			  data: { usuario : {{ $customer->id }} , boat: barco, id : id },
			  dataType: "text"
		}).done(function(data){
			$.ajax({
			  url: url_to_request_get+"/getcreditos.php",
			  method: "GET",
			  data: { usuario : {{ $customer->id }} , anyo : opcion, boat: barco, admin : {{ Auth::user()->role->layout  }} },
			  dataType: "html"
			}).done(function(data) {
			  $('#contenedor').html(data);
			});
		});
	}



</script>

@endsection
