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

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

  constructor(private authSer: AuthServiceService, 
    private http: HttpClient,
    private router: Router,
    private externalSer: ExternalPageService,
    private spinner: NgxSpinnerService) {
      this.spinner.show();
     }

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

  ngOnInit() {

    this.authSer.homeActivate = false;

  

      this.externalSer.getExternalServices().subscribe(
        (responce) => {
          console.log(responce);
          this.externalService = responce['external_services'];
        },
        (error) => {
          console.log(error);
        }
      )
    
    
    

   

    this.http.get(this.authSer.pathApi + '/reports_list_gate/'+ this.currentPage + '/' + this.perPagePagenation + '/1').subscribe(
      (responce) => {
        console.log(responce);
        this.dataPagesNews = responce['reports'];
        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);
      }
    );
  }

   // /change page 
   onPageChange(pagenationNumber) {
    this.spinner.show();
    this.http.get(this.authSer.pathApi + '/get_reports_list/1/' + 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(['ExternalPage/newPage/' + id])
  }

}
