import { AuthServiceService } from './../../../shared/auth-service.service';
import { NgxSpinnerService } from 'ngx-spinner';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { DashboardService } from './../../../shared/dashboard.service';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Modal } from 'ngx-modialog/plugins/bootstrap';
import { UserService } from '../../../shared/user.service';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-view-requests-for-tenant',
  templateUrl: './view-requests-for-tenant.component.html',
  styleUrls: ['./view-requests-for-tenant.component.css']
})
export class ViewRequestsForTenantComponent implements OnInit {


  dataList = [];
  pages= [];
  pageId: number;
  Ids = [];
  count: number;
  perPagePagenation: number;
  currentPage:number = 1;
  to_whom : number ;
  status = 'all';
  filtterStatus = 'all';
  selectedAll: any;
  userLoginId:number;
  serviceId:number;
  dataTableNumber: number = 5;
  absorpationListIds = [];
  dataListIds = [];

  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private modal: Modal,
    private userSer: UserService,
    private spinner: NgxSpinnerService,
    public authSer: AuthServiceService,
    private toastr: ToastrService,
    private dashboardSer: DashboardService,
    private http: HttpClient
  ) { }

  ngOnInit() {


           //init the values of permision boolean
   this.authSer.showAddBtn = false;
   this.authSer.showDeleteBtn = false;
   this.authSer.showEditBtn = false;
    //show / hide notification search in header
    this.authSer.notificationLogin = true;
    this.authSer.showSearchHeader = false;
    this.authSer.showHeaderLogin = false;
    this.authSer.showHeaderDashBoard = true;
    this.authSer.showDashboardHeader = true;
    this.authSer.internalHeader = false;


    this.route.params.subscribe(
      (parmas: Params) => {
        this.pageId =  +parmas['listPageId'];
        localStorage.setItem('pageIdActive', parmas['listPageId']);
        console.log(this.pageId);
      }
    );

    this.route.parent.params.subscribe(
      (params:Params) => {
          this.userLoginId = params['userID'];
          this.serviceId = params['serviceID'];
          this.to_whom = this.userLoginId;
  
          this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
            (responce) => {
              console.log(responce);
              this.pages = responce['pages'];
              for(let i = 0; i< this.pages.length; i++) {
                if(this.pages[i].id == 71){
                  if(this.pages[i]['permissions'][0].name == 'view_requests_for_tenant') {
                    this.authSer.showAddBtn = true;
                    this.authSer.showEditBtn = true;
                    this.authSer.showDeleteBtn = true;
                  } else {
                    console.log('no permissions');
                  }
                } else {
                  console.log('no events');
                }
              }
              this.spinner.hide();
            },
            (error) => {console.log(error)}
          );
        }
      );

      console.log('current page',this.pageId)
      this.dashboardSer.getHousingRequestsList( this.currentPage ,this.dataTableNumber, this.to_whom, this.status).subscribe(
        res => {
          console.log('x',res);
          this.dataList = res['requests'];
          this.count = res['count'];
          this.perPagePagenation = res['per_page'];
        },
        err => {
          console.log(err);
          
        }

      );

  }

  //make all checkbox of user checked 
  selectAll() {
    for (var i = 0; i < this.dataList.length; i++) {
      this.dataList[i].selected = this.selectedAll;
    }
  };

  checkIfAllSelected() {
    this.selectedAll = this.dataList.every(function(item:any) {
      return item.selected == true;
    });
  };

  filtterFunc(data) {
    this.dataList =[];
    this.absorpationListIds = [];
    console.log(data.target.value);
    const dataSearch = data.target.value;     
    this.currentPage = 1;
    console.log('search curent page', this.currentPage);
    console.log('search page id', this.pageId);
    this.dashboardSer.getDataUSerSearchBarhousing(dataSearch, this.currentPage, this.dataTableNumber, this.to_whom , this.status).subscribe(
      res => {
        console.log(res);
        this.dataList = res['requests'];
        console.log('hey',this.dataList)
        this.count = res['count'];
        this.perPagePagenation = res['per_page'];
        console.log('filtter count', this.count);
        console.log('filtter perPagePAgenation', this.perPagePagenation);
      },
      err => {
        console.log(err);
      }
    );
  }

  onGetStatus(event){
    this.spinner.show();
    this.status = event;
    this.dashboardSer.getHousingRequestsList( this.currentPage ,this.dataTableNumber, this.to_whom, this.status).subscribe(
      res => {
        console.log('x',res);
        this.dataList = res['requests'];
        this.count = res['count'];
        this.perPagePagenation = res['per_page'];
        this.spinner.hide();

      },
      err => {
        console.log(err);
        this.spinner.hide();
        
      }

    );
  }



  onPageChange(pagenationNumber) {
    this.spinner.show();
    this.currentPage = pagenationNumber;
    this.absorpationListIds = [];
    console.log(pagenationNumber);
    console.log(this.pageId);
    this.dashboardSer.getHousingRequestsList( this.currentPage ,this.dataTableNumber, this.to_whom, this.status).subscribe(
      res => {
        console.log('x',res);
        this.dataList = res['requests'];
        this.count = res['count'];
        this.perPagePagenation = res['per_page'];
      },
      err => {
        console.log(err);
        
      }

    );
  }

  onGetValue(event) {
    this.spinner.show();
    this.absorpationListIds = [];
    this.dataTableNumber = event.target.value;
    this.dashboardSer.getHousingRequestsList( this.currentPage ,this.dataTableNumber, this.to_whom, this.status).subscribe(
      res => {
        console.log('x',res);
        this.dataList = res['requests'];
        this.count = res['count'];
        this.perPagePagenation = res['per_page'];
      },
      err => {
        console.log(err);
        
      }

    );
  }

  onGetValueFiltter(event) {
    console.log(event.target.value)
    this.spinner.show();
    this.dataList = [];
    const typeId = event.target.value;
    this.filtterStatus = event.target.value;
    console.log(this.filtterStatus);
    this.dashboardSer.getHousingRequestsList( this.currentPage ,this.dataTableNumber, this.to_whom, this.status).subscribe(
      res => {
        console.log('x',res);
        this.dataList = res['requests'];
        this.count = res['count'];
        this.perPagePagenation = res['per_page'];
      },
      err => {
        console.log(err);
        
      }

    );
  }

  onEdit(editId: number) {
    this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'viewRequestsForTenantDetails/' + editId]);
  }

}
