import { Component, OnInit } from '@angular/core';
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { DatePipe } from "@angular/common";
import { Router } from '@angular/router';
import { ExcelService } from 'src/app/service/excel.service';
import { DocumentService } from 'src/app/service/document.service';
import { CourseService } from 'src/app/service/course.service';
import { WidgetService } from 'src/app/service/widgets.service';

@Component({
  selector: 'app-portal-create-category',
  templateUrl: './portal-create-category.component.html',
  styleUrls: ['./portal-create-category.component.scss']
})
export class PortalCreateCategoryComponent implements OnInit {

  constructor(private modalService: NgbModal,private routes: Router,
    private excelService:ExcelService,
    private documentService:DocumentService,
    private courseService:CourseService,
    private widgetService:WidgetService
  ) {}
  page = 1;
  pageSize = 10;

  testdata: {
    id:string;
    img: String;
    name: String;
    details: String;
    document: String;
  }[] = [];

  ngOnInit() {
    this.testdata = [
      {
        id:'1',
        img: "assets/images/icon/excel.png",
        name: "Excel",
        details: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        document: "0"
      },
      {
        id:'2',
        img: "assets/images/icon/document.png",
        name: "Document",
        details: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        document: "0"
      },
      {
        id:'3',
        img: "assets/images/icon/course.png",
        name: "Course",
        details: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        document: "0"
      },
      {
        id:'4',
        img: "assets/images/icon/widget.png",
        name: "Widgets",
        details: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        document: "0"
      },
      {
        id:'5',
        img: "assets/images/users/1.jpg",
        name: "BI",
        details: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        document: "0"
      }
    ];
    this.getListCount();
  }
  async getListCount(){
    try {
      const countExcel = await this.excelService.getCount().toPromise();
      const countDoc = await this.documentService.getCount().toPromise();
      const countCourse = await this.courseService.getCount().toPromise();
      const countWidgets= await this.widgetService.getCount().toPromise();
      this.testdata.forEach((x ,i) => {
        if(i == 0){
          x.document = countExcel.toString()
        }else if(i == 1){
          x.document = countDoc.toString()
        }else if(i == 2){
          x.document = countCourse.toString()
        }else if(i == 3){
          x.document = countWidgets.toString()
        }
      })
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  openView(id:string){
    if(id =='1'){
      this.routes.navigate(['apps/list-excel']);
    }else if(id =='2'){
      this.routes.navigate(['apps/list-doc']);
    }else if(id =='3'){
      this.routes.navigate(['apps/list-course']);
    }else if(id =='4'){
      this.routes.navigate(['apps/list-widgets']);
    }
  }
  openModal(targetModal: NgbModal) {
    this.modalService.open(targetModal, {
      centered: true,
      backdrop: "static",
    });
  }

  closeBtnClick() {
    this.modalService.dismissAll();
  }

}
