import { Component, OnInit ,ViewChild} from '@angular/core';
import * as moment from 'moment';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { LocalStorage } from '../../core/services/local_storage.service';
import { BulletinDetailsComponent } from '../bulletin-details/bulletin-details.component';

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

  @ViewChild(BulletinDetailsComponent) content_modal: BulletinDetailsComponent;

  loader: boolean;
  bullet_in: any;
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  user_role:any;
  show_dashboard_dse: boolean=false;
  constructor(private server: HyperService, private router: Router) {
    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;   
    }
  }

  ngOnInit() { 
      this.getTrainingList();
  } 
    
  getTrainingList() {
      this.loader = true
    //   this.server.get("common/learning")
      this.server.get("bulletin?page=0&limit=10")
          .then((data) => {
              if (data.status == 200 && data.result.response != 'failed' && data.result.data != null  ) {
                  this.loader = false;
                  this.bullet_in = data.result.data;
                  LocalStorage.setValue('bullet_in', this.bullet_in)
                 
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              }
          });
  }

  onSelect(list) {
    //   this.server.get("common/learning")
      this.server.get("bulletin/bulletin_viewed/"+list.bulletin_id)
          .then((data) => {
            console.log(data)
              if (data.status == 200 && data.result.response != 'failed' && data.result.data != null  ) {
                  this.bullet_in = data.result.data;
                  LocalStorage.setValue('bullet_in', this.bullet_in)
                 
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              }
          });
      this.content_modal.showcontentrModal('Result', list, (data) => { 
      })
  }

}