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

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

    @ViewChild(ContentPopupComponent) content_modal: ContentPopupComponent;

    sub: any;
    content_id: number;
    content_list: any;
    sales_date: any;
    user_dashboard: any;
    category_list: any = [];
    send_data: any = {};
    topicid: number;
    topic: any;
    topic_title: any;
    loader: boolean;
    percentage: any;
    is_selected:any=[]
    constructor(private route: ActivatedRoute, private server: HyperService, private router: Router, private _location: Location) {
        this.sub = this.route.params.subscribe(params => {
            this.topicid = params['id'];
        });
        // console.log( this.route.routeConfig.path ," this.route.params")
        LocalStorage.setValue('last_url',this.route.routeConfig.path);
        this.topic = LocalStorage.getValue('topic_list',);
        this.topic_title = this.topic.find(item => item.assettopicid === this.topicid); 
        this.getContentDeatails(this.topicid);
        this.categoryList() 
    }

    ngOnInit() {}


    categoryList() {
        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);
                } else if (data.result.status == 401 && data.result.response == 'failed') {
                    this.router.navigateByUrl('');
                }
            });
    }


    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 = [];
                }
            });
    }
  

    selectTopic(id) {
        this.getContentDeatails(id);
    }

    onSelect(list) { 
        this.send_data.list = list; 
        this.content_modal.showcontentrModal('list', this.send_data, (data) => { 
            this.getContentDeatails(this.topicid);
        })
    }

    backClicked() {
        console.log("heeeerwereeee")
        this._location.back();
        this.is_selected[LocalStorage.getValue('active_menu_training')] = "selected"
    }


}