@extends('layouts.web')

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

@section('content')
<section class="page-header">
	<div class="page-header__bg" style="background-image: url({{asset('assets/images/header.jpg')}});"></div>
	<div class="container">
		<h2>{{$category}} </h2>
		<ul class="thm-breadcrumb list-unstyled">
			<li class="text-white"><a href="{{url('/')}}">Inicio</a></li>
		</ul>
	</div>
</section>

@if (Auth::user())
	<div class="row">
		<div class="container py-2">
			<div class="col text-right">
				<a href="{{route('documentos.create')}}" class="thm-btn">
				<i class="fas fa-plus"></i>  
				Registrar nuevo Documento
				</a>
			</div>
		</div>
	</div>
@endif


	    @if (isset($documentos))
        @foreach ($documentos as $documento)               
				 


            <section class="about-one" {{ $documento->status ?: 'despublicado' }}>
                <img src="assets/images/shapes/about-leaf-1-1.png" alt="" class="about-one__shape-1">
                <div class="container">
                    <div class="row">
                        <div class="col-md-12 col-lg-3">
                            {{ HTML::image('assets/img/logos/logo.jpg', 'a picture', array('class' => 'circle', 'width'=>'80%')) }}
                        </div><!-- /.col-md-12 -->
                        <div class="col-md-12 col-lg-9">
                            <div class="about-one__content">
                                <div class="block-title text-left">
                                    <div class="block-title__decor"></div><!-- /.block-title__decor -->
                                    <p><span class="new badge left">{{date('d M Y',strtotime($documento->publish_up))}}</span></p>
                                    <h3>{{$documento->title}}</h3>
                                    <p>Descargado: {{$documento->hits}} veces.</p>
                                </div><!-- /.block-title -->
                                <div>
                                    {!!$documento->description!!}
                                </div>
                                <div class="row">
                                    <div class="secondary-content"> 
                                        <a target="_new" href="assets/documents/{{$documento->file}}" onclick="downloadAdd('{{$documento->id}}')" class="btn btn-success" data-position="bottom" data-delay="50" data-tooltip="Descargar:  {{$documento->title}}">
                                            <i class="fa fa-download" aria-hidden="true"></i>
                                        </a> 
                                        @if (Auth::user())
                                            <a onclick="trash('{{$documento->id}}','{{$documento->title}}')" class="btn btn-danger">
                                                <i class="fa fa-trash" aria-hidden="true"></i>
                                            </a>
                                            <a href="{{route('documentos.edit',$documento->id)}}" class="btn btn-info">
                                                <i class="fa fa-edit" aria-hidden="true"></i>
                                            </a>   
                                        @if($documento->status)
                                            <a onclick="statusChange('{{$documento->id}}','{{$documento->status}}','{{$documento->title}}')" class="btn btn-light" 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('{{$documento->id}}','{{$documento->status}}','{{$documento->title}}')" class="btn btn-light" 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><!-- /.row -->
                            </div><!-- /.about-one__content -->
                        </div><!-- /.col-md-12 col-lg-6 -->
                    </div><!-- /.row -->
                </div><!-- /.container -->
            </section><!-- /.about-one -->
             
	 
        @endforeach
	    @else
	       @include('partials.noData')
	    @endif
	    
        @php $paginates=$documentos; @endphp
        <div class="container list-unstyled d-flex justify-content-center py-4">
            {{ $paginates->links() }}
        </div>

@stop
@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('/documento/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('/documento/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 );
       }
   }); 
}

function downloadAdd(id){
	console.log('down'+id);
   $.ajax({
       url: '{{ url('/documento/download') }}' + '/' + id,
       type: 'GET', 
       headers: {
           'X-CSRF-TOKEN': $('meta[title="token"]').attr('content')
       },
       dataType: "JSON",
       success: function (res) {
           console.log(res); 
       },
       error: function(res){
           console.log("Error :" + res );
       }
   }); 
}
</script>

@stop
