import {Component, Input, OnInit} from '@angular/core';
import {PagesService} from "@app/_services/pages.service";

@Component({
  selector: 'pages-menu',
  templateUrl: 'pages-menu.component.html',
  styleUrls: ['pages-menu.component.scss']
})
export class PagesMenuComponent implements OnInit {
  @Input() root: any;
  @Input() level = 0;

  constructor(private pagesService: PagesService) {
  }

  get children() {
    return this.root?.children?.data;
  }


  ngOnInit() {
    if (this.root && this.root?.hasChildren && !this.children) this.fetch();
  }

  fetch() {
    let include = ['children'];
    this.pagesService.show(this.root.id, {include: include.join(',')}).subscribe(res => {
      this.root = res.data;
    });
  }

}
