import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NavBarService } from '../admin.service';
import { AuthService } from '../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { environment } from '../../../environments/environment.dev';
@Component({
  selector: 'app-ccindex',
  templateUrl: './ccindex.component.html',
  styleUrls: ['./ccindex.component.css']
})
export class CCIndexComponent implements OnInit {

  img_path = environment.imageUrl+'/assets/img/admin/';
  prefs = [
    {name: 'INDIA'},
    {name: 'USA'}
   ];

  form: FormGroup;
  selectedFilter = 2;
  filters = [
    { name: 'Business', pWidth: 'auto' },
    { name: 'Technical', pWidth: '74px' },
    { name: 'All', pWidth: '60px' }
  ];

  locations = [];
  dismiss = 'none';
  errMsg = '';
  tableData = {
    headers: ['Skillmaster', 'Level', 'Heading', 'Sub-Heading'],
    rows: [],
    dialogData: { title: 'CC Index', text: 'Would you like to delete this CC Index from the list?'}
  };
  pageNumbers = [1, 2, 3];
  currentPage = 1;
  lastPage = 3;
  offset = 10;
  rows = [];

  constructor(
    private router: Router,
    private navBarService: NavBarService,
    private AuthService: AuthService) { }

  ngOnInit() {
    // Populate table with fake data
    const dummyData = ['Javascript', 'Technical Usecase', 'Digital', 'Managing Ambiguity'];
    for (let i = 0; i < 30; i++) {
      const row = [];
      for (let j = 0; j < dummyData.length; j++) {
        row.push(dummyData[j] + ' ' + i);
      }
      this.rows.push(row);
    }
    this.tableData.rows = this.rows.slice(0, 10);
    this.navBarService.setCurrentPage(13);
  }

  onSubmit(value: string): void {
    console.log('you submitted value: ', value);
  }

  applyFilter(event, newValue) {
    this.selectedFilter = newValue;
  }

  edit(index) {
    index = index + this.offset;
    console.log('Editing item: ', this.rows[index]);
    this.router.navigate(['/admin/cc-index-form/' + index]);
  }

  delete(index) {
    index = index + this.offset;
    console.log('Deleting item: ', this.rows[index]);
    this.tableData.rows.splice(index, 1);
  }

  setPage (index) {
    console.log('page' + index);
    this.currentPage = index;
    const start = (index - 1) * this.offset;
    this.tableData.rows = this.rows.slice(start, start + this.offset);
  }
}
