import {Component, Input} from '@angular/core';
import {FormsService, ListsService, ObjectsService} from "@app/_services";

@Component({
  selector: 'page-menu',
  templateUrl: 'page-menu.component.html',
  styleUrls: ['page-menu.component.scss']
})
export class PageMenuComponent {
  @Input() page: any;
  @Input() section: any;
  @Input() editMode: boolean;
  @Input() index: number;
  @Input() modelId: string;
  @Input() modelType: string;

  constructor(private formsService: FormsService, private objectsService: ObjectsService, private listsService: ListsService) {
  }

  ngOnInit() {
    //console.log(this.section)
  }


  get ord() {
    return this.section?.pivot?.ord;
  }

  get total() {
    return this.page?.sections?.data?.length || 1;
  }

  get isFirst() {
    return this.index === 0;
  }

  get isLast() {
    return (this.index + 1) === this.total;
  }


  edit() {
    this.formsService.editObject(this.section.id);
  }

  move(ord) {
    this.objectsService.move(this.section.id, {modelType: this.modelType, modelId: this.modelId, ord: ord}).subscribe(res => {
      this.listsService.refresh();
    });
  }

  delete() {
    //r u sure?
    if (confirm('Удалить этот блок?')) this.objectsService.destroy(this.section.id).subscribe(res => {
      this.listsService.refresh();
    });
  }
}
