import { Component, OnInit ,ViewChild} from '@angular/core';
import { HyperService } from '../../core/services/http.service';
import { Router, NavigationEnd,ActivatedRoute } from "@angular/router";
import { LocalStorage } from '../../core/services/local_storage.service';
import { ContentPopupComponent } from '../content-popup/content-popup.component';
import * as moment from 'moment';

@Component({
  selector: 'app-mytraining',
  templateUrl: './mytraining.component.html',
  styleUrls: ['./mytraining.component.scss']
})
export class MytrainingComponent implements OnInit {
  @ViewChild(ContentPopupComponent) content_modal: ContentPopupComponent;
  category_list: any = [];
  topic_list: any = [];
  sales_date: any;
  user_dashboard:any;
  loader:boolean;
  percentage:any;
  no_data:number=1;
  active_menu:string='0';
  main_list:boolean=true;
  sub_list:boolean=false;
  content_list:any=[];
  topicid: number;
  topic: any;
  topic_title: any;
  send_data: any = {};
  i:string='0';
  previousUrl:string;
  user_role:any;
  show_dashboard_dse: boolean=false;
  selectedcolor:any='';
  selectedtypeval:any='';
  initialload:boolean=true;
  constructor(private server: HyperService, private router: Router,private route: ActivatedRoute ) {
    if (LocalStorage.getValue('user_data') != undefined){
      this.user_role = LocalStorage.getValue('user_data').role;
  }
  if(this.user_role!=3 && this.user_role!=4 && this.user_role!=5 && this.user_role!=6 && this.user_role!=7 && this.user_role!=14){
    this.show_dashboard_dse = true;
  }else{
    this.show_dashboard_dse = false;   
  }
    // router.events
    // .filter(event => event instanceof NavigationEnd)
    // .subscribe(e => {
    //   console.log(e ,"dsfds") 
    // });
    this.categoryList()
   }

  ngOnInit() {
    // this.is_selected[0] = "selected"
    if( LocalStorage.getValue('last_url')!='' && LocalStorage.getValue('last_url')!=undefined){
      this.is_selected[LocalStorage.getValue('active_menu_training')] = "selected"
     }else if(LocalStorage.getValue('last_url')=='' || LocalStorage.getValue('last_url')==undefined){
       this.is_selected[0] = "selected"
     }
     
    
    // console.log(LocalStorage.getValue('active_menu_training'),"url")
    // this.active_menu=LocalStorage.getValue('active_menu_training');
    // console.log(this.active_menu,"this.active_menu")
  }


  categoryList() {
    this.main_list=true;
    this.i='0';
    this.sub_list=false;
    this.server.get("category")
      .then((data) => {
        if (data.status == 200 && data.result.response != 'failed' && data.result.data != null && data.result.data.length > 0) {
          this.category_list = data.result.data;
          LocalStorage.setValue('category_list', this.category_list);
          if( LocalStorage.getValue('last_url')!='' && LocalStorage.getValue('last_url')!=undefined){
            this.is_selected[LocalStorage.getValue('active_menu_training')] = "selected";
            this.getTrainingList(LocalStorage.getValue('active_menu_training')+1)
           let ter= LocalStorage.getValue('active_menu_training');
           this.selectTopic(this.category_list[ter].cateid,ter,this.category_list[ter].percentage)
            LocalStorage.setValue('last_url', '')
           }else if(LocalStorage.getValue('last_url')=='' || LocalStorage.getValue('last_url')==undefined){
             this.is_selected[0] = "selected"
             this.getTrainingList(this.category_list[0].cateid);
             this.selectTopic(this.category_list[0].cateid,0,this.category_list[0].percentage)
           }
          
        }
        else if (data.result.status == 401 && data.result.response == 'failed') {
          this.router.navigateByUrl('');
        }
      });
  }

  getTrainingList(topic) {
    this.main_list=true;
    this.sub_list=false;
    this.loader=true
    this.server.get("topics/"+topic+"?page=0&limit=10000")
    .then((data) => {
        if (data.status == 200 && data.result.response != 'failed') {
            this.topic_list = data.result.data;  
            this.no_data=data.result.total
            // console.log(this.no_data)
            LocalStorage.setValue('topic_list', this.topic_list) 
            // console.log(this.topic_list,"this.topic_list")
           
            // console.log(this.topic_title,"this.topic_title")
            // this.onSelect(this.topicid);
            this.loader=false
        }
        else if (data.result.status == 401 && data.result.response == 'failed') {
             this.router.navigateByUrl('');
        }
    });
  }

 
  is_selected: any = [];
  selectTopic(id,i,paramvalue){
    for (let i = 0; i <= this.category_list.length; i++) {
      this.is_selected[i] = ""
    }
    this.is_selected[i] = "selected";
    this.selectedtypeval=paramvalue;
    if(this.selectedtypeval == 0){
      this.selectedcolor='grey';
    }else if(this.selectedtypeval != 0 &&  this.selectedtypeval <100){
      this.selectedcolor='amber';
    }else if(this.selectedtypeval == 100){
      this.selectedcolor='green';
    }else{

    }
    LocalStorage.setValue('active_menu_training', i) 
    //  this.active_menu=LocalStorage.getValue('active_menu_training') ;
    //  console.log(LocalStorage.getValue('active_menu_training'),"this.active_menu",i)
    this.getTrainingList(id);
  }

  onSelect(list){
    this.router.navigateByUrl('mycontent/'+ list.assettopicid);
    // this.main_list=false;
    // this.sub_list=true;
    // this.loader = true
    // this.server.get("topics/content/" + list.assettopicid + "?page=0&limit=10000")
    //     .then((data) => {
    //         if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
    //             this.content_list = data.result.data;
    //             this.topic = LocalStorage.getValue('topic_list');
    //             this.topic_title = this.topic.find(item => item.assettopicid === list.assettopicid); 
    //             LocalStorage.setValue('content_list', this.content_list);
    //             this.loader = false
    //         } else if (data.result.status == 401 && data.result.response == 'failed') {
    //             this.router.navigateByUrl('');
    //         } else {
    //             this.content_list = [];
    //         }
    //     });

  }
  onSelectSub(list) { 
    this.send_data.list = list; 
    this.content_modal.showcontentrModal('list', this.send_data, (data) => { 
        this.getContentDeatails(this.topicid);
    })
}
getContentDeatails(page: number = 0): any {
  this.loader = true
  this.server.get("topics/content/" + page + "?page=0&limit=10000")
      .then((data) => {
          if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
              this.content_list = data.result.data;
              LocalStorage.setValue('content_list', this.content_list);
              this.loader = false
          } else if (data.result.status == 401 && data.result.response == 'failed') {
              this.router.navigateByUrl('');
          } else {
              this.content_list = [];
          }
      });
}
  backClicked(){
    this.main_list=true;
    this.sub_list=false;
    this.is_selected[LocalStorage.getValue('active_menu_training')] = "selected"
    // this.getTrainingList(LocalStorage.getValue('active_menu_training')+1)
    // console.log( this.is_selected[LocalStorage.getValue('active_menu_training')] = "selected")
  }
//   getContentDeatails(data): any {
   
// }


}
