import { Component, Host, h } from '@stencil/core';
import { FacilitatorService } from '../../../../services/facilitator.service';
import facilitatorStore from '../../../../stores/facilitator.store';
import { modalController } from '@ionic/core';
export class FacilitatorNotes {
  async componentDidLoad() {
    await this.fetchData();
  }
  async fetchData() {
    await FacilitatorService.getInstance().fetchNotes({ showAllNotes: false });
  }
  async deleteNote(id) {
    await FacilitatorService.getInstance().deleteFacilitatorNote(id);
  }
  async deleteNoteConfirmation(id) {
    if (window.confirm("Do you really want to delete this note?")) {
      await this.deleteNote(id);
    }
  }
  async presentFacilitatorNotesModal(values) {
    const modal = await modalController.create({
      component: 'sparkle-facilitator-notes-form',
      cssClass: 'sparkle-modal sparkle-facilitator-notes-form',
      componentProps: {
        initialValues: values
      }
    });
    await modal.present();
  }
  renderNotes() {
    var _a, _b, _c;
    return (h("table", { class: "table" },
      h("thead", null,
        h("tr", null,
          h("th", { scope: "col" }, "Notes"),
          h("th", { scope: "col" }, "Action"))),
      h("tbody", null, (_a = facilitatorStore.state.pageNotes) === null || _a === void 0 ? void 0 :
        _a.map(item => h("tr", null,
          h("td", { class: "c1" }, item.NoteText),
          h("td", { class: "c2" },
            h("div", { class: "d-flex" },
              h("div", { class: "p-2" },
                h("button", { class: "btn btn-primary", type: "button", onClick: () => this.presentFacilitatorNotesModal(item) }, "Edit")),
              h("div", { class: "p-2" },
                h("button", { class: "btn btn-dark", type: "button", onClick: () => this.deleteNoteConfirmation(item.Id) }, "Delete")))))),
        !facilitatorStore.state.pageNotes || ((_b = facilitatorStore.state.pageNotes) === null || _b === void 0 ? void 0 : _b.length) == 0 &&
          h("tr", null,
            h("td", { colSpan: 2 }, facilitatorStore.state.notesLoading ? "Loading..." : (((_c = facilitatorStore.state.pageNotes) === null || _c === void 0 ? void 0 : _c.length) == 0 ? "No Data" : ""))))));
  }
  render() {
    console.log("render");
    return (h(Host, null,
      h("div", { class: "p-2 pb-4 d-md-flex justify-content-md-end" },
        h("button", { class: "btn btn-primary pull-right", type: "button", onClick: () => this.presentFacilitatorNotesModal(null) }, "+ Add Note")),
      this.renderNotes()));
  }
  static get is() { return "sparkle-facilitator-notes"; }
  static get originalStyleUrls() { return {
    "$": ["facilitator-notes.css"]
  }; }
  static get styleUrls() { return {
    "$": ["facilitator-notes.css"]
  }; }
}
