import { Component, OnInit } from '@angular/core';
import { AuthordashboardService } from './authordashboard.service';
import { ToastrService } from 'ngx-toastr';
import { ActivatedRoute, Router } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
import { Observable, Subject } from 'rxjs';
@Component({
  selector: 'app-authordashboard',
  templateUrl: './authordashboard.component.html',
  styleUrls: ['./authordashboard.component.css']
})
export class AuthordashboardComponent implements OnInit {
  // sorting variables
  key = 'projectname';
  reverse = false;
  // pagination variables
  p = 1;
  itemsPerPage = 10;
  searchText = '';
  readOnly: boolean; add: boolean; edit: boolean; delete: boolean;
  userid: number;
  projectDetails: any = [];
  show = false;
  totalCount = 0;
  public consoleMessages: string[] = [];
  public userQuestion: string;
  searchTextChange = new Subject<string>();
  constructor(private AuthorDashboardService: AuthordashboardService, private toastr: ToastrService,
    private router: Router, private SpinnerService: NgxSpinnerService) {
    // Debounce search.
    this.searchTextChange.pipe(
      debounceTime(500),
      distinctUntilChanged())
      .subscribe(value => {
        this.p = 1;
        this.loadgetAuthorDashboard();
      });
    // this.paginationPageSize = 10;
  }
  columnDefs1 = [
    { headerName: 'UserId', field: 'userId', sortable: true, filter: true, checkboxSelection: true },
    { headerName: 'Title', field: 'title', sortable: true },
    { headerName: 'Body', field: 'body', sortable: true, filter: 'agNumberColumnFilter' }
  ];
  authordashboardduedatemenid = 80;
  enableduedatefield = false;

  ngOnInit(): void {
    this.userid = +localStorage.getItem('userid');
    this.loadgetAuthorDashboard();
    this.getUserRights();

  }

  getUserRights() {

    this.AuthorDashboardService.GetRoleByUserId(this.userid).subscribe(res => {
      if (res.status === 200) {
        var actionRights = JSON.parse(res.body[0])[0].menumapped;
        var authordashboardduedattrights = actionRights.filter(d => d.menuid === this.authordashboardduedatemenid);

        if (authordashboardduedattrights.length > 0) {
          this.enableduedatefield = true;
        }

      }
    }, err => {
      this.toastr.error('Error while geting rights');


    });
  }

  sort(key) {
    this.key = key;
    this.reverse = !this.reverse;
  }

  loadgetAuthorDashboard() {
    this.SpinnerService.show();
    const input = [];
    input.push({ userid: + this.userid, searchtext: this.searchText });
    this.AuthorDashboardService.getAuthorDashboardrowcount(input[0]).subscribe(response => {
      if (response.body[0] != null && response.body[0] !== undefined && JSON.parse(response.body[0]) !== undefined &&
        JSON.parse(response.body[0]) !== null) {
        this.totalCount = JSON.parse(response.body[0])[0].count;
        if (this.totalCount > 0) {
          const inputvalue = [];
          inputvalue.push({ userid: + this.userid, pageno: this.p, itemsperpage: this.itemsPerPage, searchtext: this.searchText });
          this.AuthorDashboardService.getAuthorDashboardData(inputvalue[0]).subscribe(res => {
            if (res.body[0] != null && res.body[0] !== undefined) {
              this.projectDetails = JSON.parse(res.body[0]);
              //  console.log('this.projectDetails=', this.projectDetails);
            }
            this.SpinnerService.hide();
            // console.log('inputvalue[0]=', inputvalue[0]);
          }, err => {
            this.toastr.error('Error in Geting Data');
            this.SpinnerService.hide();
          });
        } else {
          this.SpinnerService.hide();
        }
        //
      }
    }, err => {
      this.toastr.error('Error in Geting Data');
      this.SpinnerService.hide();
    });
  }

  paginationChanged(params) {
    // params.api.paginationGetCurrentPage();
    console.log(params);
  }

  WorkArea(userid, jtid, spath, projectid) {
    this.show = true;
    this.router.navigate(['/dashboard/workarea'], {
      queryParams:
        { jtid: jtid, projectid: projectid, spath: spath }
    });
  }

  getPage(page: number) {
    this.p = page;
    this.loadgetAuthorDashboard();
  }

  changeitemsperpage(eve) {
    this.itemsPerPage = +eve.currentTarget.value;
    this.p = 1;
    this.loadgetAuthorDashboard();
  }
}

