@extends('layouts.web')

@section('page-title', 'Autoridades')

@section('content')
	<h1 class="headerSection">Autoridades</h1>


	@if (Auth::user())

	<div class="fixed-action-btn toolbar">
	  <span class="new badge blue badgeButtom" style="float:left;"> Agregar</span>
	  <a class="btn-floating btn-large blue z-depth-4">
	    <i class="large material-icons">library_add</i> 
	  </a>
	  <ul>
	    <li class="waves-effect waves-light">
	        <a href="{{route('autoridades.create')}}"><i class="fa fa-user"></i> Registrar nueva Autoridad</a>
	    </li> 
	  </ul>
	</div>
	@endif

	    @if (count($authorities))
	        @foreach ($authorities as $authority)                 

		      <div class="row {{ $authority->status ?: 'despublicado' }}">
		        <div class="col s12 m6">
		          <div class="card teal darken-1">
		            <div class="card-content white-text">
		              <span class="card-title">
		              	<i class="material-icons small dp48">account_circle</i> {{$authority->name}} {{$authority->lastname1}} {{$authority->lastname2}} 

		              	@if (Auth::user())
			              	<div class="actions">
				              	<a onclick="trash('{{$authority->id}}','{{$authority->name}}')" class="pull-right btn-floating waves-effect waves-light btn-small red">
				              		<i class="fa fa-trash" aria-hidden="true"></i>
				              	</a>
				              	<a href="{{route('autoridades.edit',$authority->id)}}" class="pull-right btn-floating waves-effect waves-light btn-small blue">
				              		<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
				              	</a>   
				            @if($authority->status)
				              	<a onclick="statusChange('{{$authority->id}}','{{$authority->status}}','{{$authority->name}}')" class="pull-right btn-floating waves-effect waves-light btn-small green tooltipped" data-position="bottom" data-delay="50" data-tooltip="Este registro está PUBLICADO">
				              		<i class="fa fa-eye" aria-hidden="true"></i>
				              	</a>   	
				            @else 
				            	<a onclick="statusChange('{{$authority->id}}','{{$authority->status}}','{{$authority->name}}')" class="pull-right btn-floating waves-effect waves-light btn-small green tooltipped" data-position="bottom" data-delay="50" data-tooltip="Este registro está DESPUBLICADO">
				              		<i class="fa fa-eye-slash" aria-hidden="true"></i>
				              	</a>   	
				            @endif 
				            <div class="badge green"><small>publicado el: {{$authority->publish_up}}</small></div>
			              	</div>
			             @endif
		              </span>

		              <p>{{$authority->position}} - {{$authority->community}}</p>
		            </div>
		            <div class="card-action">

		              @if($authority->fb)
		             	<a href="{{$authority->fb}}" target="_new"><i class="fa fa-facebook-official"></i>/{{$authority->name}}</a>
		              @endif
		              @if($authority->email)
		             	<a class="text-lowercase" href="mailto:{{$authority->email}}?subject=Web Machareti e Ivo" target="_new"><i class="fa fa-envelope fa4"></i> {{$authority->email}}</a>
		              @endif
		              @if($authority->movil)
		              	<a href="#" target="_new"><i class="fa fa-phone"></i> {{$authority->movil}}</a>
		              @endif
		            </div>
		          </div>
		        </div>
		      </div>           
	 
	        @endforeach
	    @else
	       @include('partials.noData')
	    @endif
	    
	    @php $paginates=$authorities; @endphp
	    @include('partials.paginate')

@stop
   <hr>
@section('scripts')

<script>
function trash(id,name){ 
    var ident = id;
    swal({
     text: 'Esta seguro de realizar esta Acción?',
     title: 'Si procede <span class="btn btn-danger">'+name+'</span> será eliminado!',
     type: 'warning',
     showCancelButton: true,
     confirmButtonColor: '#DD6B55',
     confirmButtonText: 'Eliminar!',
     cancelButtonText: 'Cancelar', 
     closeOnConfirm: false,
     closeOnCancel: false,
     html: true

   },
   function(isConfirm){
       if (isConfirm) { 
       //	console.log(id+'-'+name)
           $.ajax({
               url: '{{ url('/autoridades/delete') }}' + '/' + id, 
               type: "GET",  
               dataType: "JSON", 
               headers: {
                'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
            },
            success: function (res) {
                console.log(res);
            },
            error: function(res){
                console.log("Error :" + res );
            }
               }); 
               swal(name+' eliminado!', 'Este registro fue eliminado.', 'success');
               window.location.reload(true);

       }else {
           swal('Cancelado!', 'Este registro NO fue eliminado.', 'error');
       }
   });
}
</script>
<script>
function statusChange(id,status,name){
   $.ajax({
       url: '{{ url('/autoridades/statusChange') }}' + '/' + id + '/' + status,
       type: 'GET', 
       headers: {
           'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
       },
       dataType: "JSON",
       success: function (res) {
           console.log(res);
            if(res.now){
                swal(name+', modificado!', 'este registro está publicado.', 'success');
                window.location.reload(true);
            }else{
                swal(name+', modificado!', 'este registro está despublicado.', 'warning');
                window.location.reload(true);
            }
       },
       error: function(res){
           console.log("Error :" + res );
       }
   }); 
}
</script>

@stop
