'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-bae2a754.js');
const overlays = require('./overlays-0a748609.js');
const facilitator_service = require('./facilitator.service-faac5c0c.js');
require('./ionic-global-878073d1.js');
require('./hardware-back-button-f7b5d99e.js');
require('./helpers-7e28976c.js');
require('./auth.store-ff3fda09.js');
require('./environment-config.service-0e02b049.js');

const LoadingSpinner = () => (index.h("div", { class: "spinner-border", role: "status" }));

const facilitatorNotesFormCss = ":host{display:block}";

const FacilitatorNotesForm = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.formControls = {
      noteText: {
        isValid: true,
        value: "",
        errorMessage: "",
        validate: value => {
          console.log(value);
          if (value && value.length > 0) {
            return { valid: true, errorMessage: "" };
          }
          else {
            return { valid: false, errorMessage: "Notes is required" };
          }
        },
      }
    };
    this.submitted = false;
  }
  componentDidLoad() {
    if (this.initialValues) {
      this.changeFormValue("noteText", this.initialValues.NoteText);
    }
  }
  async onSubmit(e) {
    e.preventDefault();
    this.submitted = true;
    let isFormValid = true;
    //Run all validation functions
    for (let controlName in this.formControls) {
      let control = this.formControls[controlName];
      control.isValid = control.validate(control.value).valid;
      control.errorMessage = control.validate(control.value).errorMessage;
      if (!control.isValid) {
        isFormValid = false;
      }
    }
    if (isFormValid) {
      if (this.initialValues && this.initialValues.Id > 0) {
        let result = await facilitator_service.FacilitatorService.getInstance().updateFacilitatorNote({
          NoteText: this.formControls.noteText.value,
          Id: this.initialValues.Id
        });
        if (result.Succeeded) {
          const toast = await overlays.toastController.create({
            message: "Notes updated successfully",
            duration: 3000
          });
          toast.present();
          this.dismiss();
        }
        else {
          const toast = await overlays.toastController.create({
            message: result.Message,
            duration: 3000
          });
          toast.present();
        }
      }
      else {
        let result = await facilitator_service.FacilitatorService.getInstance().addFacilitatorNote({
          NoteText: this.formControls.noteText.value
        });
        if (result.Succeeded) {
          const toast = await overlays.toastController.create({
            message: "Notes added successfully",
            duration: 3000
          });
          toast.present();
          this.dismiss();
        }
        else {
          const toast = await overlays.toastController.create({
            message: result.Message,
            duration: 3000
          });
          toast.present();
        }
      }
    }
  }
  dismiss(data) {
    // dismiss the closest modal and optionally pass back data
    this.el.closest('ion-modal').dismiss({
      'dismissed': true
    });
  }
  changeFormValue(controlName, value) {
    this.formControls = Object.assign(Object.assign({}, this.formControls), { [controlName]: Object.assign(Object.assign({}, this.formControls[controlName]), { value: value, isValid: this.formControls[controlName].validate(value).valid, errorMessage: this.formControls[controlName].validate(value).errorMessage }) });
  }
  render() {
    const noteTextValidation = !this.formControls.noteText.isValid ? "is-invalid" : "is-valid";
    return (index.h(index.Host, null, index.h("ion-header", { translucent: true }, index.h("ion-toolbar", null, index.h("ion-title", null, "Facilitator Note"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { onClick: () => this.dismiss() }, "Close")))), index.h("ion-content", { class: "ion-padding" }, index.h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true }, index.h("div", { class: "mb-3" }, index.h("label", { htmlFor: "validationTextarea", class: "form-label" }, "Notes"), index.h("textarea", { class: `form-control ${this.submitted && noteTextValidation}`, placeholder: "Notes", value: this.formControls.noteText.value, onChange: (e) => this.changeFormValue("noteText", e.target.value) }), this.formControls.noteText.errorMessage && index.h("div", { class: "invalid-feedback" }, this.formControls.noteText.errorMessage)), index.h("div", { class: "mb-3" }, facilitator_service.facilitatorStore.state.notesLoading && index.h(LoadingSpinner, null), index.h("button", { class: "btn btn-primary", type: "submit", disabled: facilitator_service.facilitatorStore.state.notesLoading }, "Submit"))))));
  }
  get el() { return index.getElement(this); }
};
FacilitatorNotesForm.style = facilitatorNotesFormCss;

exports.sparkle_facilitator_notes_form = FacilitatorNotesForm;
