@extends('layouts.web')

@section('page-title', 'Galería de Fotos - Albums')

@section('content')
	<h1 class="headerSection">Galería de Fotos</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('fotos.create')}}"><i class="fa fa-picture-o"></i> Registrar nueva Fotografía</a>
	    </li> 
	    <li class="waves-effect waves-light">
	        <a href="{{route('albums.create')}}"><i class="fa fa-folder-open"></i> Registrar nuevo Album</a>
	    </li> 
	  </ul>
	</div>
	@endif
    <div class="row">
	    @if (count($albums)) 
	        @foreach ($albums as $album)               
			    
                <div class="col-md-12  text-center {{ $album->status ?: 'despublicado' }}">
                    <div class="card card-panel hoverable">
                        <div class="card-content">
                            
                            <div class="row">
                                <div class="col-xs-12 col-md-4"> 
                                    <a href="{{ route('fotos.index',['filter' => $album->id]) }}">
                                     <img  class="img-fluid rounded" src="assets/fotos/{{ DB::table('fotos')->where('album_id', $album->id)->value('file') }}" alt="{{$album->album}}"/>  
                                    </a>
                                    <span class="new badge">
                                        {{DB::table('fotos')->where('album_id', $album->id)->count()}} Fotos
                                    </span>
                                </div>    
                                <div class="col-xs-12 col-md-8">
                                    <a href="{{ route('fotos.index',['filter' => $album->id]) }}">
                                        <h4 class="headerSection">{{$album->album}}</h4>
                                    </a>
                                    <div class="text-justify">
                                        {!! $album->description !!}
                                    </div>
                                </div> 
                        
                            </div>
                            
                            
                        </div>
                    
                    
                        <div class="card-action teal darken-1"> 
                            <div class="chip pull-right">
            		            <i class='fa fa-calendar' aria-hidden='true'></i> Publicado el:
            		            {{date('d M Y',strtotime($album->publish_up))}}
            		            <span class="new badge">
            		            	{{\Carbon\Carbon::now()->subMinutes( \Carbon\Carbon::now()->diffInMinutes( \Carbon\Carbon::parse($album->publish_up)))->diffForHumans()}}
            		            </span>
            		       	</div>
                              <div class="pull-left">
                              @if (Auth::user())
                                    <a onclick="trash('{{$album->id}}','{{$album->album}}')" 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('albums.edit',$album->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($album->status)
                                    <a onclick="statusChange('{{$album->id}}','{{$album->status}}','{{$album->album}}')" 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('{{$album->id}}','{{$album->status}}','{{$album->album}}')" 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                        
            
                            @endif
                            </div>
                        </div>
                    </div>
                </div>
			    
	        @endforeach 
	    @else
	       @include('partials.noData')
	    @endif
	    
	    @php $paginates=$albums; @endphp
	    @include('partials.paginate')
    </div>
@stop
   <hr>
@section('scripts')

<script>
function trash(id,title){ 
    var ident = id;
    swal({
     text: 'Esta seguro de realizar esta Acción?',
     title: 'Si procede <span class="btn btn-danger">'+title+'</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+'-'+title)
           $.ajax({
               url: '{{ url('/albums/delete') }}' + '/' + id, 
               type: "GET",  
               dataType: "JSON", 
               headers: {
                'X-CSRF-TOKEN': $('meta[title="token"]').attr('content')
            },
            success: function (res) {
                console.log(res);
            },
            error: function(res){
                console.log("Error :" + res );
            }
               }); 
               swal(title+' 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,title){
   $.ajax({
       url: '{{ url('/albums/statusChange') }}' + '/' + id + '/' + status,
       type: 'GET', 
       headers: {
           'X-CSRF-TOKEN': $('meta[title="token"]').attr('content')
       },
       dataType: "JSON",
       success: function (res) {
           console.log(res);
            if(res.now){
                swal(title+', modificado!', 'este registro está publicado.', 'success');
                window.location.reload(true);
            }else{
                swal(title+', modificado!', 'este registro está despublicado.', 'warning');
                window.location.reload(true);
            }
       },
       error: function(res){
           console.log("Error :" + res );
       }
   }); 
}

</script>
    <script type="text/javascript">
$(document).ready(function(){
    $('.materialboxed').materialbox();
  });
        

    </script> 
@stop
