import { InternalPageService } from './../../shared/internal-page.service';
import { AuthServiceService } from './../../shared/auth-service.service';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';

@Component({
  selector: 'app-news-pages',
  templateUrl: './news-pages.component.html',
  styleUrls: ['./news-pages.component.css']
})
export class NewsPagesComponent implements OnInit {

  constructor(private http: HttpClient, 
    private authSer: AuthServiceService,
    private spinner: NgxSpinnerService, 
    private internalSer: InternalPageService,
    private router: Router) { }

  myInnerHeight = window.innerHeight;

  dataPagesNews= [];
  internalService = [];
  perPagePagenation: number = 10;
  count: number;
  currentPage:number = 1;

  ngOnInit() {

    this.spinner.show();
    console.log(this.authSer.pathImg);
    this.http.get(this.authSer.pathApi + '/reports_list_gate/' + this.currentPage + '/' + this.perPagePagenation + '/internal').subscribe(
      (responce) => {
        console.log(responce);
        this.dataPagesNews = responce['reports'];
        this.count = responce['count'];
        for(let i = 0 ; i < this.dataPagesNews.length ; i++) {
          if(this.dataPagesNews[i].description.length > 60) {
            this.dataPagesNews[i].description = this.dataPagesNews[i].description.substring(0,200) + '........' ;
            this.dataPagesNews[i].description_en = this.dataPagesNews[i].description_en.substring(0,200) + '......';
          }
        }
        this.spinner.hide();
      },
      (error) => {
        console.log(error);      
      }
    );

    this.internalSer.getInternalServices().subscribe(
      (responce) => {
        console.log(responce);
        this.internalService = responce['external_services'];
      },
      (error) => {
        console.log(error);
      }
    );

  }

      // /change page 
      onPageChange(pagenationNumber) {
        this.spinner.show();
        this.http.get(this.authSer.pathApi + '/reports_list_gate/' + this.currentPage + '/' + this.perPagePagenation + '/internal').subscribe(
          (responce) => {
            console.log(responce);
            this.dataPagesNews = responce['reports'];
            this.count = responce['count'];
            for(let i = 0 ; i < this.dataPagesNews.length ; i++) {
              if(this.dataPagesNews[i].description.length > 60) {
                this.dataPagesNews[i].description = this.dataPagesNews[i].description.substring(0,200) + '........' ;
                this.dataPagesNews[i].description_en = this.dataPagesNews[i].description_en.substring(0,200) + '......';
              }
            }
          },
          (error) => {
            console.log(error);      
          }
        )
      };

  onGetNew(id) {
    this.router.navigate(['InternalPage/news/' + id])
  }

}
