import { NgxSpinnerService } from 'ngx-spinner';
import { ActivatedRoute, Params } from '@angular/router';
import { AuthServiceService } from './../../shared/auth-service.service';
import { Component, OnInit, AfterContentInit } from '@angular/core';
import { InternalPageService } from '../../shared/internal-page.service';
declare var $: any;

@Component({
  selector: 'app-hospital-content',
  templateUrl: './hospital-content.component.html',
  styleUrls: ['./hospital-content.component.css']
})

export class HospitalContentComponent implements OnInit {

  constructor(private internalService: InternalPageService, 
              private route: ActivatedRoute,
              private spinner: NgxSpinnerService,
              private authSer:AuthServiceService) { 
              }

  hospitalCenters = [];
  detailsField = [];

  expandedIndex:number;
  topArrow:boolean = true;
  bottomArrow:boolean = false;
  flag: boolean = false;
  typePage:string;
  
  catchEvent(event) {
    this.flag = true
    console.log(event)
  }

  hide(event) {
    this.flag = false;
  }
  


  ngOnInit() {
    
    this.spinner.show();
    this.authSer.homeActivate = false;
    this.expandedIndex = 0;
    this.route.params.subscribe(
      (params: Params) => {
        this.typePage = params['typePage'];
        if(params['typePage'] == 'hospital') {
          this.internalService.getHospitalCenterList('hospitals_centers_list').subscribe(
            (responce) => {
              console.log(responce);
              this.hospitalCenters = responce['hospitals_centers'];
              this.showDescriptionDetails(this.hospitalCenters[0].fields[0]);
              console.log(this.hospitalCenters);
            },
            (error) => {
              console.log(error);
            }
          );
    
          this.authSer.parentsPage = [];
    
          this.internalService.getParentData().subscribe(
            (responce) => {
              this.authSer.parentsPage = responce['parents'];
              for(let i = 0; i < this.authSer.parentsPage.length; i++) {
                if(this.authSer.parentsPage[i].id == 8) {
                  this.authSer.parentsPage[i].active = 1;
                } else {
                  this.authSer.parentsPage[i].active = 0;
                }
              }
              this.spinner.hide();
            },
            (error) => {
              console.log(error);
            }
          );
        } else if(params['typePage'] == 'managements') {
          this.internalService.getHospitalCenterList('managements_list').subscribe(
            (responce) => {
              console.log(responce);
              this.hospitalCenters = responce['managements'];
              this.showDescriptionDetails(this.hospitalCenters[0].fields[0]);
              console.log(this.hospitalCenters);
            },
            (error) => {
              console.log(error);
            }
          );
    
          this.internalService.getParentData().subscribe(
            (responce) => {
              this.authSer.parentsPage = responce['parents'];
              for(let i = 0; i < this.authSer.parentsPage.length; i++) {
                if(this.authSer.parentsPage[i].id == 9) {
                  this.authSer.parentsPage[i].active = 1;
                } else {
                  this.authSer.parentsPage[i].active = 0;
                }
              }
              this.spinner.hide();
            },
            (error) => {
              console.log(error);
            }
          );
        }
      }
    );

  }

  getInformationData(data) {
    for(let i = 0; i < this.hospitalCenters.length; i++) {
      if(data.id == this.hospitalCenters[i].id) {
        this.hospitalCenters[i].active = 1;
      } else if(data.id != this.hospitalCenters[i].id) {
        this.hospitalCenters[i].active = 0;
      }
       else{
        this.hospitalCenters[i].active = 0;
      }
    }
  }


  //show detail description 
  showDescriptionDetails(deatilData) {
    this.detailsField = deatilData;
  }

  open(data,i) {
    console.log(data);
    this.expandedIndex = i;
  }

  closeCollapse(i) {
    this.expandedIndex = -1;
  }

}
