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


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

  constructor(private authSer: AuthServiceService, 
    private spinner: NgxSpinnerService,
    private internalService: InternalPageService) { }
   
    checkShowData: boolean;
    selectedItem: any = 0 //check تعاميم active class
    perPagePagenation: number = 10;
    dataTableNumber:number = 5;
    count: number;
    currentPage:number = 1;
    data = [];


    typeCircu:number = 0;
  
    ngOnInit() {
      this.spinner.show();
      this.internalService.getCircularsData(this.currentPage, this.perPagePagenation, 0).subscribe(
      (responce) => {
        console.log(responce);
        this.data = responce['tabs'];
        this.count = responce['count'];
        this.perPagePagenation = responce['per_page'];
        if(this.data.length > 0) {
          this.checkShowData = false;
        } else {
          this.checkShowData = true;
        }
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    )
  };

    // /change page 
    onPageChange(pagenationNumber) {
      this.spinner.show();
      this.currentPage = pagenationNumber;
      this.data = [];
      //console.log(pagenationNumber);
      //console.log(this.pageId);
      this.internalService.getCircularsData(this.currentPage, this.perPagePagenation, this.typeCircu).subscribe(
        (responce) => {
          console.log(responce);
          this.data = responce['tabs'];
          this.count = responce['count'];
          this.perPagePagenation = responce['per_page'];
          //console.log(this.tabsList);
          this.spinner.hide();
        },
        (error) => {
          console.log(error);
          this.spinner.hide();
        }
      );
    };

  onGetData(id) {
    this.spinner.show();
    this.typeCircu = id;
    this.selectedItem = id;
    this.internalService.getCircularsData(this.currentPage, this.perPagePagenation, id).subscribe(
      (responce) => {
        console.log(responce);
        this.data = responce['tabs'];
        this.count = responce['count'];
        this.perPagePagenation = responce['per_page'];
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
        this.spinner.hide();
      }
    );

  }

    //determine the list count from select element 
    onGetValue(event) {
      this.spinner.show();
      this.data = [];
      this.dataTableNumber = event.target.value;
      this.internalService.getCircularsData(this.currentPage, this.perPagePagenation, this.typeCircu).subscribe(
        (responce) => {
          console.log(responce);
          this.data = responce['tabs'];
          this.count = responce['count'];
          this.perPagePagenation = responce['per_page'];
          this.spinner.hide();
        },
        (error) => {
          console.log(error);
          this.spinner.hide();
        }
      );
    }

}
