import { Component, OnInit } from '@angular/core';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
@Component({
  selector: 'app-engineering-env',
  templateUrl: './engineering-env.component.html',
  styleUrls: ['./engineering-env.component.scss']
})
export class EngineeringEnvComponent implements OnInit {
  projectRequestedListInfo;
  projectCreatedListInfo;
  monthNames = [
    'JAN', 'FED', 'MAR',
    'APR', 'MAY', 'JUN', 'JUL',
    'AUG', 'SEP', 'OCT',
    'NOV', 'DEC'
  ];
  constructor(public employeeService: EmployeeHttpService) { }

  ngOnInit() {
    this.getAllRequestedProjects();
    this.getAllCreatedProjects();
  }

  getAllRequestedProjects() {
    this.employeeService.getAllRequestedProjects().subscribe(resp => {
      this.projectRequestedListInfo = resp.projectList;
      this.projectRequestedListInfo.forEach(prj => {
        prj['formatedCreatedDate'] = this.formatDate(prj.createdDate);

      });
      console.log('projectRequestedListInfo', this.projectRequestedListInfo);
      }, err => {
      console.log('Error on gett all requested repo');
      });
  }

  getAllCreatedProjects() {
    this.employeeService.getAllCreatedProjects().subscribe(response => {
      this.projectCreatedListInfo = response.projectList;
      this.projectCreatedListInfo.forEach(prj => {
        prj['formatedCreatedDate'] = this.formatDate(prj.createdDate);
      });
      console.log('projectCreatedListInfo', this.projectCreatedListInfo);
      }, err => {
      console.log('Error on gett all created repo');
      });
  }

  getAllProjects($event) {
    this.getAllRequestedProjects();
    this.getAllCreatedProjects();
  }

  formatDate(date) {
    let dateSplit = [];
    let dateStr = [];
    let monthIndex = 0;
    dateStr = date.split('T');
    dateSplit = dateStr[0].split('-');
    monthIndex = parseInt(dateSplit[1], 10);
    return dateSplit[2] + ' ' + this.monthNames[monthIndex - 1] + ' ' + dateSplit[0];
  }

}
