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

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

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


  get type() {
    return this.section?.type?.data;
  }

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

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

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

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


  ngOnInit() {

  }


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

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

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

}
