import { Component, OnInit, ViewChild } from '@angular/core';
import { AuthService } from 'app/services/auth.service';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { Pipe, PipeTransform } from '@angular/core';
import { PagerService } from '../user-mgmt/_services/pager.service';
import { environment } from '../../../../../environments/environment.dev';
import { LocalStorageService } from 'angular-2-local-storage';

@Component({
  selector: 'app-college-recruitment',
  templateUrl: './college-recruitment.component.html',
  styleUrls: ['./college-recruitment.component.scss']
})


export class CollegeRecruitmentComponent implements OnInit {

  colleges: any = [];
  collegesCopy: any =[];
  showHide:boolean = true;
  rowIndex: number = -1;
  showFNm: boolean = false;
   // pager object
   pager: any = {};
   // paged items
   pagedItems: any[];
   collegeData: boolean = true;
   errMsg = '';
   coTilesData: any;
   coTilesIndex: number;
   addNewCollege: boolean = false;
   coEditDet: any;
   idToDelete: number;
   deletedIdFromMstr: number;
   deletedFromMstrname : string;
   dialogData: any;
   img_path = environment.imageUrl+'/assets/img/Taflow/';
   usr_img_path = environment.imageUrl+'/assets/img/admin/';
   tilesDisplay: boolean = true;
   myColor = ["#009587", "#01BCD5", "#663AB7", "#E6A9E6", "#3F51B5", "#F8A9CA", "#6FDCDE", "#F2D2CD", "#F2CCDB", "#BFE2F5", "#D8D0F7", "#6FDCDE", "#EC8ECC", "#009587", "#01BCD5", "#663AB7", "#E6A9E6", "#3F51B5", "#F8A9CA", "#6FDCDE", "#F2D2CD", "#F2CCDB", "#BFE2F5", "#D8D0F7", "#6FDCDE", "#EC8ECC"];
   reqData: any;
   inacClData: boolean = false;

  constructor(protected localStorageService: LocalStorageService, private AuthService: AuthService, private toastr: ToastsManager, private pagerService: PagerService) { 
    this.setPage(1);
    this.getAvlbColleges();
    this.reqData = this.localStorageService.get('userInfo');
    console.log(this.reqData);
  }

  ngOnInit() {
    this.dialogData = { title: 'College Delete Confirmation', text: 'Would you like to delete this College from the list?' };
  }

  getAvlbColleges(){
    this.AuthService.getAllColleges().then(resp => {
      if(!resp.error){
        this.colleges = resp.collegeList;
        this.collegesCopy = resp.collegeList;
        if(this.colleges.length !== 0) {
          this.setPage(1);
        }
        this.inacClData = false;
      }
      else{
        this.toastr.error(resp.error);
      }
    })
  }
  getInactiveColleges(){
    this.AuthService.getNonActiveColleges().then(resp => {
      if(!resp.error){
        this.colleges = resp.collegeList;
        this.collegesCopy = resp.collegeList;
        if(this.colleges.length !== 0) {
          this.setPage(1);
        }
        this.inacClData = true;
      }
      else{
        this.toastr.error(resp.error);
      }
    })
  }
  setPage(page: number) {
    if(this.pager.totalPages){
      if (page < 1 || page > this.pager.totalPages) {
        return;
      }
    }
    // get pager object from service
    this.pager = this.pagerService.getPager(this.colleges.length, page);
    // get current page of items
    this.pagedItems = this.colleges.slice(this.pager.startIndex, this.pager.endIndex + 1);
    console.log(this.pagedItems);
    this.rowIndex = -1;
    this.showFNm = false;
  }

  addCollegeData(){
    this.collegeData = false;
    this.addNewCollege = true;
  }

  toggleCoListView(){
    this.tilesDisplay = false;
  }

  toggleCoGridView(){
    this.tilesDisplay = true;
  }

  formCancelled(e){
    this.collegeData = true;
    this.getAvlbColleges();
  }

  showOtherIconsTable(index){
    this.rowIndex = index;
  }

  deleteTabCollegeData(coTilesData,index){
    coTilesData.isDelete = true;
    this.deleteCollegeData(coTilesData);
  }
  editTabCollegeData(coTilesData,index){
    this.editCollegeData(coTilesData);
  }

  editCollegeData(data){
    this.collegeData = false;
    this.coEditDet = data;
    this.addNewCollege = false;
  }
  deleteCollegeData(data){
    if (data.isDelete) {
      document.getElementById("delCoConfModal").click();
      //this.confirmDelete
    }
    this.idToDelete = data.id;
  }
  confirmJrDelete() {
    this.AuthService.deleteCollege(this.idToDelete, this.idToDelete).subscribe((resp:any) => {
      resp =  JSON.parse(resp._body);
      if (resp.status == 'error') {
        this.toastr.error(resp.message);
      } else {
        this.toastr.success(resp.successMessage);
        this.getAvlbColleges();
      }
    })
  }

  changeEvent(e){
    if(e.checked == true){
      this.colleges = [];
      this.setPage(0);
      this.getInactiveColleges();
    }
    else{
      this.getAvlbColleges();
    }
  }
}