'use strict';

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

const index = require('./index-e0385008.js');
const formatMeasure = require('./formatMeasure-1cf0667a.js');
const formatUSD = require('./formatUSD-65108a08.js');

const FormSiteService = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinSubmit = index.createEvent(this, "fireenjinSubmit", 7);
    this.siteId = undefined;
    this.name = undefined;
    this.serviceId = undefined;
    this.jobId = undefined;
    this.unit = undefined;
    this.siteServiceId = undefined;
    this.isGeneral = false;
    this.site = undefined;
    this.amount = 0;
    this.quantity = 1;
    this.price = 0;
    this.notes = undefined;
    this.showNotes = false;
  }
  onSuccess(event) {
    var _a, _b, _c, _d, _e;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "deleteScopeService") {
      this.amount = 0;
      this.quantity = 1;
      this.siteServiceId = null;
    }
    else if (((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.endpoint) === "addScopeService") {
      this.siteServiceId = (_e = (_d = (_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.addScopeService) === null || _e === void 0 ? void 0 : _e.id;
    }
  }
  onReset(event) {
    this.fireenjinSubmit.emit({
      endpoint: "deleteScopeService",
      event,
      data: {
        id: this.siteServiceId,
        siteId: this.siteId,
        jobId: this.jobId,
      },
    });
  }
  render() {
    var _a, _b, _c, _d;
    return (index.h("ion-accordion", { value: `${this.isGeneral ? this.jobId : this.siteId}-${this.serviceId}` }, index.h("ion-item", { slot: "header", style: { "--background": "var(--background-color)" } }, index.h("ion-label", null, index.h("h2", null, this.name ||
      (this.isGeneral ? "General Service" : "No Site Name")), this.siteServiceId ? (index.h("ion-text", { color: "primary" }, index.h("b", null, this.amount || 0, " ", (this === null || this === void 0 ? void 0 : this.unit) || ""), " ", "x ", index.h("b", null, this.quantity || 1, " Qty"), " x", " ", index.h("b", null, formatUSD.formatUSD(this.price || 0)))) : (index.h("p", null, "Click to Add"))), index.h("ion-text", { color: "success", slot: "end" }, this.siteServiceId && (index.h("small", null, formatUSD.formatUSD(parseFloat(this.amount || 0) *
      parseFloat(this.quantity || 1) *
      parseFloat(this.price || 0)))))), index.h("fireenjin-form", { slot: "content", disableLoader: true, name: `siteService-${this.siteId}-${this.serviceId}`, resetButton: "Remove", resetButtonColor: "danger", disableReset: true, endpoint: this.siteServiceId ? "editScopeService" : "addScopeService", beforeSubmit: async (data) => {
        const formData = {
          jobId: this.jobId,
          siteId: this.siteId,
          ...(this.siteServiceId
            ? { id: this.siteServiceId }
            : { serviceId: this.serviceId }),
          price: parseFloat((data === null || data === void 0 ? void 0 : data.price) || this.price || 0),
          quantity: parseFloat((data === null || data === void 0 ? void 0 : data.quantity) || this.quantity || 1),
          amount: parseFloat((data === null || data === void 0 ? void 0 : data.amount) || this.amount || 1),
          notes: data.notes || this.notes || null,
        };
        if (data === null || data === void 0 ? void 0 : data.price) {
          try {
            formData.price = parseFloat((data === null || data === void 0 ? void 0 : data.price) || 0);
          }
          catch (error) {
            console.log("Error setting price: ", error);
          }
        }
        return formData;
      } }, index.h("ion-grid", { class: "ion-no-padding" }, index.h("ion-row", { class: "ion-no-padding" }, index.h("ion-col", { size: "6", sizeXs: "4" }, index.h("fireenjin-input", { type: "number", name: "amount", value: this.amount || 0, label: "Amount", labelPosition: "stacked", min: "0", step: "0.01" })), index.h("ion-col", { size: "6", sizeXs: "4" }, index.h("fireenjin-input", { type: "number", name: "quantity", value: this.quantity || 1, label: "Quantity", labelPosition: "stacked", min: "1" })), index.h("ion-col", { size: "12", sizeXs: "4" }, index.h("fireenjin-input", { type: "number", name: "price", value: this.price || 0, label: "Price", labelPosition: "stacked", step: "0.01", min: "0" }))), index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-chip-bar", null, index.h("ion-button", { shape: "round", fill: "clear", size: "small", color: "primary", onClick: () => (this.showNotes = !this.showNotes) }, index.h("ion-icon", { name: "chatbox" })), [
      {
        label: "Affected Sqft",
        value: Math.round(((_a = this.site) === null || _a === void 0 ? void 0 : _a.affectedAreaTotalSqft) * 100) /
          100,
      },
      {
        label: "Affected CuSqft",
        value: Math.round(((_b = this.site) === null || _b === void 0 ? void 0 : _b.affectedAreaTotalCuft) * 100) /
          100,
      },
      {
        label: "Total Sqft",
        value: Math.round(((_c = this.site) === null || _c === void 0 ? void 0 : _c.areaTotalSqft) * 100) / 100,
      },
      {
        label: "Total CuSqft",
        value: Math.round(((_d = this.site) === null || _d === void 0 ? void 0 : _d.areaTotalCuft) * 100) / 100,
      },
    ]
      .filter((chip) => (chip === null || chip === void 0 ? void 0 : chip.value) > 0)
      .map((chip) => (index.h("ion-chip", { onClick: () => (this.amount = (chip === null || chip === void 0 ? void 0 : chip.value) || 0) }, (chip === null || chip === void 0 ? void 0 : chip.label) || "", " - ", formatMeasure.formatMeasure((chip === null || chip === void 0 ? void 0 : chip.value) || 0))))))), index.h("ion-row", { style: { display: this.showNotes ? "block" : "none" } }, index.h("ion-col", null, index.h("fireenjin-input", { name: "notes", placeholder: "Service Notes", maxlength: 200, value: this.notes })))))));
  }
};

exports.floodteam_form_site_service = FormSiteService;
