import { Component, OnInit, TemplateRef } from '@angular/core';
import { BsModalRef, BsModalService, ModalOptions } from 'ngx-bootstrap';
import {NavMenuService} from '../nav-menu/nav-menu.service';
import { ReleaseNotes } from '../releasenotes/releasenotes';
import { ReleasenotesService } from '../releasenotes/releasenotes.service';
declare var $: any;


@Component({
  selector: 'app-homelayout',
  templateUrl: './homelayout.component.html',
  styleUrls: ['./homelayout.component.css']
})
export class HomelayoutComponent implements OnInit {
  isExpanded = false;
  curyr: number = new Date().getFullYear();
  menuLength = 0;
  public userid = localStorage.getItem('userid');
  list: any = [];
  releasenoteContent: string; 
  releasenotes: ReleaseNotes[]
  releaseversions = '';
  modalRef: BsModalRef;
  config: ModalOptions = {
    /*backdrop: 'static',
    keyboard: false,
    animated: true,*/
    class: 'modal-dialog-centered',
    ignoreBackdropClick: false
  }



  constructor(private navmenuservice: NavMenuService,
    private releaseNoteService: ReleasenotesService,
    private modalService: BsModalService) { }

  ngOnInit() {

    this.navmenuservice.getRoleMappedMenu(+this.userid).subscribe(res => {
      debugger;
      if (res !== undefined && res != null && res.length > 0) {
        res.forEach(i => {
          if (i !== undefined) {
            this.list.push(JSON.parse(i));
          }
        });
      }

      else{
        debugger;
        this.toggleclicked();
      }

      this.menuLength = this.list.length;

    });
    this.getReleaseNotes();
  }

  getReleaseNotes() {
    this.releaseNoteService.Get().subscribe(res => {
     
      this.releasenotes = JSON.parse(res[0].j);
      //console.log( this.releasenotes);
      //console.log(this.releasenotes[0].content);
     this.releasenoteContent = this.releasenotes[0].content;
      // this.topbar.releaseversion = this.releasenotes[0].version;
      this.releaseversions = this.releasenotes[0].version;
    })
  }
  changeLog(template: TemplateRef<any>){
    this.modalRef = this.modalService.show(template, this.config);
    const aa = document.getElementsByClassName('modal-content');    
    aa[0].classList.remove("modal-content");
  }
 
  toggleclicked() {
    debugger;
    var $BODY = $('body'),
      $SIDEBAR_MENU = $('#sidebar-menu');

    // $(window).resize();
    // console.log('clicked - menu toggle');

    if ($BODY.hasClass('nav-md')) {
      $SIDEBAR_MENU.find('li.active ul').hide();
      $SIDEBAR_MENU.find('li.active').addClass('active-sm').removeClass('active');
    } else {
      $SIDEBAR_MENU.find('li.active-sm ul').show();
      $SIDEBAR_MENU.find('li.active-sm').addClass('active').removeClass('active-sm');
    }

    $BODY.toggleClass('nav-md nav-sm');

    this.setContentHeight();

    $('.dataTable').each(function () { $(this).dataTable().fnDraw(); });
    return;
  }

  setContentHeight() {
    var $RIGHT_COL = $('.right_col'), $BODY = $('body'), $SIDEBAR_FOOTER = $('.sidebar-footer'),
      $LEFT_COL = $('.left_col'), $FOOTER = $('footer'), $NAV_MENU = $('.nav_menu');
    // reset height
    $RIGHT_COL.css('min-height', $(window).height());

    var bodyHeight = $BODY.outerHeight(),
      footerHeight = $BODY.hasClass('footer_fixed') ? -10 : $FOOTER.height(),
      leftColHeight = $LEFT_COL.eq(1).height() + $SIDEBAR_FOOTER.height(),
      contentHeight = bodyHeight < leftColHeight ? leftColHeight : bodyHeight;

    // normalize content
    contentHeight -= $NAV_MENU.height() + footerHeight;

    $RIGHT_COL.css('min-height', contentHeight);
  }
}