import { Component, Element, Event, h, Listen, Prop, Build, State, } from "@stencil/core";
import { menuController, modalController, popoverController, toastController, } from "@ionic/core";
import state from "../../../store";
import getJobFromCache from "../../../helpers/getJobFromCache";
export class AppJob {
  constructor() {
    this.feedFetchData = {
      orderBy: "createdAt",
      orderDirection: "desc",
    };
    this.userRoleMap = {};
    this.showingEfileHistory = [];
    this.isUploading = false;
    this.efileLoading = false;
    this.isSending = false;
    this.activityFeed = [];
    this.isSiteModalOpen = false;
    this.isEndOfFeed = false;
    this.payModels = [];
    this.feedLimit = 10;
    this.currentSlide = 0;
    this.showControls = null;
    this.isUploadingPhoto = false;
    this.readingsView = "readings";
    this.preparingEfiles = [];
  }
  async onSlideChange(event) {
    this.currentSlide =
      (await event.target.getActiveIndex()) + 1;
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
    if (event.detail.endpoint === "upload") {
      this.isUploadingPhoto = false;
    }
    else if (event.detail.endpoint === "addEfile") {
      this.isUploading = false;
    }
    else if (event.detail.endpoint === "editJob") {
      this.modalEl.dismiss();
    }
    else if (event.detail.endpoint === "addUserToJob") {
      this.modalEl.dismiss();
    }
    else if (event.detail.endpoint === "addEfile") {
      // FLIP THE SWITCH OFF HERE
      this.isUploading = false;
    }
    else if (["sendTemplateEmail", "editJob", "addUserToJob"].includes((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint)) {
      if ((_b = this.modalEl) === null || _b === void 0 ? void 0 : _b.dismiss)
        this.modalEl.dismiss();
    }
    else if (event.detail.endpoint === "addNote") {
      const paginationEl = this.activityFeedEl.querySelector("fireenjin-pagination");
      if (paginationEl) {
        setTimeout(() => {
          paginationEl.getResults();
        }, 2000);
      }
    }
    else if (event.detail.endpoint === "findJob" &&
      ((_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.job) === null || _e === void 0 ? void 0 : _e.id) === this.jobId) {
      this.userRoleMap = {};
      if ((_g = (_f = state === null || state === void 0 ? void 0 : state.job) === null || _f === void 0 ? void 0 : _f.customer) === null || _g === void 0 ? void 0 : _g.id)
        this.userRoleMap[(_h = state.job) === null || _h === void 0 ? void 0 : _h.customer.id] = "customer";
      if ((_k = (_j = state === null || state === void 0 ? void 0 : state.job) === null || _j === void 0 ? void 0 : _j.manager) === null || _k === void 0 ? void 0 : _k.id)
        this.userRoleMap[(_l = state.job) === null || _l === void 0 ? void 0 : _l.manager.id] = "manager";
      if ((_o = (_m = state === null || state === void 0 ? void 0 : state.job) === null || _m === void 0 ? void 0 : _m.referral) === null || _o === void 0 ? void 0 : _o.id)
        this.userRoleMap[(_p = state.job) === null || _p === void 0 ? void 0 : _p.referral.id] = "referral";
      this.userRoleMap = { ...this.userRoleMap };
      const efiles = [];
      for (const efile of ((_q = state === null || state === void 0 ? void 0 : state.job) === null || _q === void 0 ? void 0 : _q.efiles) || []) {
        const existingFileIndex = efiles.findIndex((file) => { var _a, _b; return ((_a = file === null || file === void 0 ? void 0 : file.template) === null || _a === void 0 ? void 0 : _a.id) === ((_b = efile === null || efile === void 0 ? void 0 : efile.template) === null || _b === void 0 ? void 0 : _b.id); });
        if (existingFileIndex >= 0) {
          efiles[existingFileIndex].history.push(efile);
        }
        else {
          efiles.push({ ...efile, history: [] });
        }
      }
      this.efiles = efiles;
      setTimeout(() => {
        var _a, _b, _c;
        this.selectedReadingDate = ((_b = (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.readings) === null || _b === void 0 ? void 0 : _b.length)
          ? (_c = state.job) === null || _c === void 0 ? void 0 : _c.readings[0].name
          : null;
        this.readingsGraphData();
      }, 1000);
    }
    else if (((_r = event === null || event === void 0 ? void 0 : event.detail) === null || _r === void 0 ? void 0 : _r.endpoint) === "generatePdf") {
      const toast = await toastController.create({
        message: "File has been added to Efile.",
        duration: 3000,
      });
      toast.present();
      this.efileLoading = false;
    }
    else if (((_s = event === null || event === void 0 ? void 0 : event.detail) === null || _s === void 0 ? void 0 : _s.endpoint) === "listPayModels") {
      this.payModels = ((_v = (_u = (_t = event === null || event === void 0 ? void 0 : event.detail) === null || _t === void 0 ? void 0 : _t.data) === null || _u === void 0 ? void 0 : _u.listPayModels) === null || _v === void 0 ? void 0 : _v.payModels) || [];
    }
  }
  async onSlideTap(_event) {
    this.openSiteEditModal();
  }
  onCloseModa() {
    this.isSiteModalOpen = false;
  }
  async openReadingModal(event) {
    var _a, _b, _c, _d, _e, _f;
    if (!["addReading", "addDot"].includes((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name))
      return;
    const modalEl = await modalController.create({
      component: "modal-take-readings",
      componentProps: {
        jobId: (_b = state === null || state === void 0 ? void 0 : state.job) === null || _b === void 0 ? void 0 : _b.id,
        zip: (_d = (_c = state === null || state === void 0 ? void 0 : state.job) === null || _c === void 0 ? void 0 : _c.address) === null || _d === void 0 ? void 0 : _d.zip,
        dotForm: ((_e = event === null || event === void 0 ? void 0 : event.detail) === null || _e === void 0 ? void 0 : _e.name) === "addDot",
        closeOnFinish: ((_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.name) === "addDot",
      },
    });
    await modalEl.present();
  }
  onOpenSiteEditModal(event) {
    this.openSiteEditModal(event.detail.site, {
      selectedTab: event.detail.selectedTab,
    });
  }
  onSelect(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) === "readingId" && !((_b = this.modalEl) === null || _b === void 0 ? void 0 : _b.isConnected)) {
      if ((_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.reading) {
        console.log((_e = (_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.reading) === null || _e === void 0 ? void 0 : _e.name);
        this.selectedReadingDate = (_g = (_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.reading) === null || _g === void 0 ? void 0 : _g.name;
      }
      else {
        this.openReadingModal();
      }
      this.popoverEl.dismiss();
    }
    else if (((_h = event === null || event === void 0 ? void 0 : event.detail) === null || _h === void 0 ? void 0 : _h.name) === "user" && ((_k = (_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.user) === null || _k === void 0 ? void 0 : _k.id)) {
      this.sendEmail(event, (_m = (_l = event === null || event === void 0 ? void 0 : event.detail) === null || _l === void 0 ? void 0 : _l.user) === null || _m === void 0 ? void 0 : _m.id);
      this.popoverEl.dismiss();
    }
  }
  onChange(event) {
    var _a, _b, _c, _d;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "readingsView") {
      this.readingsView = event.target.value;
      this.readingsGraphData();
    }
    if ((_d = (_c = (_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.name) === null || _c === void 0 ? void 0 : _c.includes) === null || _d === void 0 ? void 0 : _d.call(_c, "efile-")) {
      this.preparingEfiles = this.preparingEfiles.includes(event.target.value)
        ? this.preparingEfiles.filter((url) => event.target.value !== url)
        : [...new Set([...this.preparingEfiles, event.target.value])];
      console.log(event.target.value);
    }
  }
  async removeEfileFromJob(efile) {
    if (confirm(`Are you sure you want to remove this file from the job?`)) {
      this.fireenjinSubmit.emit({
        endpoint: "deleteEfile",
        id: efile.id,
      });
    }
    if (this.efileListEl)
      this.efileListEl.closeSlidingItems();
  }
  async openMatterportModal() {
    var _a, _b;
    this.modalEl = await modalController.create({
      component: "modal-matterport",
      cssClass: "modal-matterport",
      backdropDismiss: false,
      componentProps: {
        jobId: (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.id,
        modelId: (_b = state === null || state === void 0 ? void 0 : state.job) === null || _b === void 0 ? void 0 : _b.matterportModelId,
      },
    });
    this.modalEl.present();
  }
  async openSiteEditModal(site, options) {
    var _a, _b, _c;
    state.site = site;
    if (this.isSiteModalOpen)
      return;
    this.modalEl = await modalController.create({
      component: "modal-site-edit",
      cssClass: "modal-site-edit",
      backdropDismiss: false,
      componentProps: {
        site,
        jobId: (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.id,
        siteId: ((_b = state === null || state === void 0 ? void 0 : state.site) === null || _b === void 0 ? void 0 : _b.id) || null,
        sites: (options === null || options === void 0 ? void 0 : options.sites) || ((_c = state === null || state === void 0 ? void 0 : state.job) === null || _c === void 0 ? void 0 : _c.sites) || [],
        currentSlide: (options === null || options === void 0 ? void 0 : options.currentSlide) ? options.currentSlide : null,
        viewGallery: typeof (options === null || options === void 0 ? void 0 : options.viewGallery) === "boolean"
          ? !!(options === null || options === void 0 ? void 0 : options.viewGallery)
          : true,
        showProgress: !!(options === null || options === void 0 ? void 0 : options.showProgress),
        selectedTab: (options === null || options === void 0 ? void 0 : options.selectedTab) ? options.selectedTab : null,
        photo: (options === null || options === void 0 ? void 0 : options.photo) ? options.photo : null,
        photoUploading: !!(options === null || options === void 0 ? void 0 : options.photoUploading),
      },
    });
    this.modalEl.present();
    this.isSiteModalOpen = true;
  }
  async openReadingSelect(event) {
    var _a, _b;
    this.popoverEl = await popoverController.create({
      component: "popover-reading-select",
      componentProps: {
        readings: ((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.readings)
          ? Object.values((_b = state.job) === null || _b === void 0 ? void 0 : _b.readings)
          : [],
      },
      event,
    });
    return this.popoverEl.present();
  }
  async removeUserFromJob(user) {
    var _a;
    if (confirm(`Are you sure you want to remove ${(user === null || user === void 0 ? void 0 : user.firstName) ? user.firstName : "this user"} from the job?`))
      this.fireenjinSubmit.emit({
        endpoint: "removeUserFromJob",
        data: {
          jobId: (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.id,
          userId: user.id,
        },
      });
    if (this.userListEl)
      this.userListEl.closeSlidingItems();
  }
  async openaddReadingPopover(event) {
    this.popoverEl = await popoverController.create({
      event,
      cssClass: "popover-add-reading",
      component: "popover-add-reading",
      componentProps: {
        job: state.job,
      },
    });
    this.popoverEl.present();
  }
  async openUserSelect(event) {
    var _a;
    this.popoverEl = await popoverController.create({
      event,
      cssClass: "popover-user-select",
      component: "popover-user-select",
      componentProps: {
        jobId: this.jobId,
        users: ((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.users) || [],
      },
    });
    this.popoverEl.present();
  }
  async openUserSearch() {
    this.modalEl = await modalController.create({
      cssClass: "modal-search-users",
      component: "modal-search-users",
      componentProps: {
        job: state.job,
      },
    });
    this.modalEl.present();
  }
  readingsGraphData() {
    var _a, _b, _c;
    const graphData = [];
    if (this.readingsView === "temperature") {
      graphData.push({
        data: (((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.readings) ? (_b = state.job) === null || _b === void 0 ? void 0 : _b.readings.reverse() : []).map((reading) => { var _a; return ((_a = reading === null || reading === void 0 ? void 0 : reading.weather) === null || _a === void 0 ? void 0 : _a.temperature) ? reading.weather.temperature : 0; }),
      });
    }
    const dotReadings = {};
    for (const reading of ((_c = state === null || state === void 0 ? void 0 : state.job) === null || _c === void 0 ? void 0 : _c.readings) || []) {
      for (const site of (reading === null || reading === void 0 ? void 0 : reading.sites) || []) {
        for (const dot of (site === null || site === void 0 ? void 0 : site.dots) || []) {
          if (!dotReadings[dot === null || dot === void 0 ? void 0 : dot.id])
            dotReadings[dot === null || dot === void 0 ? void 0 : dot.id] = [];
          dotReadings[dot === null || dot === void 0 ? void 0 : dot.id].push(dot === null || dot === void 0 ? void 0 : dot.moistureContent);
        }
      }
    }
    for (const [dotId, readings] of Object.entries(dotReadings)) {
      if (!dotId || !(readings === null || readings === void 0 ? void 0 : readings.length))
        continue;
      graphData.push({
        data: readings.map((reading) => (reading === null || reading === void 0 ? void 0 : reading[this.readingsView]) || 0),
      });
    }
    return graphData;
  }
  async refreshResults() {
    await this.feedPaginationEl.clearResults();
    await this.feedPaginationEl.getResults();
  }
  async openFileUpload(event) {
    console.log(event);
  }
  async openMenu() {
    menuController.open();
  }
  async openJobEdit(step) {
    this.modalEl = await modalController.create({
      component: "modal-job-edit",
      cssClass: "modal-edit-job",
      backdropDismiss: false,
      componentProps: {
        step,
        job: state.job,
        payModels: this.payModels,
      },
    });
    this.modalEl.present();
  }
  async openSendEfileModal(event) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.modalEl = await modalController.create({
      component: "modal-send-email",
      cssClass: "modal-send-email",
      backdropDismiss: false,
      componentProps: {
        job: state.job,
        templateFilter: (template) => { var _a, _b; return !(template === null || template === void 0 ? void 0 : template.partial) && ((_b = (_a = template === null || template === void 0 ? void 0 : template.tags) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, "job")); },
        attachments: this.preparingEfiles,
        users: ((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.users) || [],
      },
    });
    this.modalEl.present();
  }
  async openEsignModal(event, templateId) {
    event.preventDefault();
    event.stopPropagation();
    this.modalEl = await modalController.create({
      component: "modal-esign",
      cssClass: "modal-esign",
      backdropDismiss: false,
      componentProps: {
        jobId: this.jobId,
        job: state.job,
        templateId,
      },
    });
    this.modalEl.present();
  }
  async fetchJob() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      cacheKey: "jobs",
      params: {
        id: this.jobId,
        withPayments: false,
        withUsers: true,
        withSites: true,
        withPayModel: true,
        withPhotos: true,
        withEfiles: true,
        withPayables: false,
        withReadings: true,
      },
    });
  }
  async fetchPayModels() {
    this.fireenjinFetch.emit({
      endpoint: "listPayModels",
      params: {
        data: {
          limit: 10,
        },
      },
    });
  }
  async generatePdf(templateId, filename = null) {
    this.efileLoading = true;
    this.fireenjinSubmit.emit({
      endpoint: "generatePdf",
      data: {
        filename,
        jobId: this.jobId,
        templateId,
      },
    });
  }
  formatUSD(amount) {
    const formatter = new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 2,
    });
    return formatter.format(amount ? amount : 0);
  }
  send(event, template) {
    event.preventDefault();
    event.stopPropagation();
    this.template = template;
    this.isSending = true;
    this.openUserSelect(event);
  }
  async sendEmail(event, userId) {
    var _a, _b, _c, _d, _e;
    event.preventDefault();
    event.stopPropagation();
    if (!((_a = state === null || state === void 0 ? void 0 : state.session) === null || _a === void 0 ? void 0 : _a.uid))
      return;
    const data = {
      templateId: "ltEEAQ9Q1J7Jkn6wIIEH",
      userIds: [userId],
      data: {
        app: {
          url: Build.isBrowser ? window.location.origin : ".",
        },
        template: {
          id: ((_b = this.template) === null || _b === void 0 ? void 0 : _b.id) || null,
          description: ((_c = this.template) === null || _c === void 0 ? void 0 : _c.description) || null,
          subject: ((_d = this.template) === null || _d === void 0 ? void 0 : _d.description) || null,
          tags: ((_e = this.template) === null || _e === void 0 ? void 0 : _e.tags) || [],
        },
        job: JSON.parse(JSON.stringify(state.job)),
      },
    };
    console.log(data);
    this.fireenjinSubmit.emit({
      endpoint: "sendTemplateEmail",
      data,
    });
  }
  getData() {
    this.fireenjinTrigger.emit({
      name: "subscribe",
      payload: {
        collection: "jobs",
        id: this.jobId,
        callback: () => {
          this.fetchJob();
        },
      },
    });
    this.fetchJob();
    this.fetchPayModels();
  }
  async componentWillLoad() {
    state.job = await getJobFromCache(this.jobId);
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.getData();
  }
  disconnectedCallback() {
    this.fireenjinTrigger.emit({
      name: "unsubscribe",
      payload: {
        collection: "jobs",
        id: this.jobId,
      },
    });
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
    const selectedWeather = (_d = (_c = (_b = (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.readings) === null || _b === void 0 ? void 0 : _b.find) === null || _c === void 0 ? void 0 : _c.call(_b, (reading) => reading.name === this.selectedReadingDate)) === null || _d === void 0 ? void 0 : _d.weather;
    return [
      h("ion-content", null,
        h("ion-grid", { style: {
            display: "block",
            maxWidth: "1200px",
            margin: "0 auto",
          } },
          h("ion-row", null,
            h("ion-col", null,
              h("floodteam-title-bar", null, ((_e = state === null || state === void 0 ? void 0 : state.job) === null || _e === void 0 ? void 0 : _e.title) || "???"))),
          h("ion-row", null,
            h("ion-col", { class: "ion-align-self-center" },
              h("flooteam-chip-bar", null,
                h("floodteam-select-status", { jobId: this.jobId, value: (_f = state === null || state === void 0 ? void 0 : state.job) === null || _f === void 0 ? void 0 : _f.status }),
                h("floodteam-select-location", { jobId: this.jobId, value: (_h = (_g = state === null || state === void 0 ? void 0 : state.job) === null || _g === void 0 ? void 0 : _g.location) === null || _h === void 0 ? void 0 : _h.id }),
                h("ion-router-link", { href: `/jobs/${this.jobId}/scope` },
                  h("ion-chip", null,
                    h("ion-icon", { class: "ft-icon-style", name: "list" }),
                    h("ion-label", null, "View Scope"))),
                ((_j = state === null || state === void 0 ? void 0 : state.job) === null || _j === void 0 ? void 0 : _j.matterportModelId) && (h("ion-router-link", { href: "#", onClick: () => this.openMatterportModal() },
                  h("ion-chip", null,
                    h("ion-icon", { class: "ft-icon-style", name: "cube" }),
                    h("ion-label", null, "Matterport Model")))),
                h("ion-router-link", { href: `geo:${(_l = (_k = state === null || state === void 0 ? void 0 : state.job) === null || _k === void 0 ? void 0 : _k.address) === null || _l === void 0 ? void 0 : _l.full}` },
                  h("ion-chip", null,
                    h("ion-icon", { class: "ft-icon-style", name: "map" }),
                    h("ion-label", null, "Map"))),
                h("ion-router-link", { href: `/pay/${this.jobId}` },
                  h("ion-chip", null,
                    h("ion-icon", { class: "ft-icon-style", name: "logo-usd" }),
                    h("ion-label", null, "Epay"))),
                h("ion-chip", { onClick: () => this.openJobEdit("cause") },
                  h("ion-icon", { class: "ft-icon-style", name: "water" }),
                  h("ion-label", null, `Cat ${((_m = state === null || state === void 0 ? void 0 : state.job) === null || _m === void 0 ? void 0 : _m.category) || 0} - ${((_o = state === null || state === void 0 ? void 0 : state.job) === null || _o === void 0 ? void 0 : _o.cause) || "Unknown"}`))))),
          h("ion-row", { class: "ion-no-padding ion-no-margin" },
            h("ion-col", { size: "12", sizeMd: "6", class: "ion-no-padding ion-no-margin" },
              h("floodteam-title-bar", { icon: "cube", heading: 4, disableUnderline: true },
                h("ion-text", null, "Sites"),
                h("ion-button", { fill: "outline", href: "#", slot: "end", size: "small", onClick: () => this.openSiteEditModal() }, "Add")),
              h("ion-card", { class: "site-card" },
                h("ion-list", { lines: "full", style: {
                    paddingBottom: ((_q = (_p = state === null || state === void 0 ? void 0 : state.job) === null || _p === void 0 ? void 0 : _p.sites) === null || _q === void 0 ? void 0 : _q.length) ? "0px" : "8px",
                    paddingTop: ((_s = (_r = state === null || state === void 0 ? void 0 : state.job) === null || _r === void 0 ? void 0 : _r.sites) === null || _s === void 0 ? void 0 : _s.length) ? "0px" : "8px",
                  } }, ((_u = (_t = state === null || state === void 0 ? void 0 : state.job) === null || _t === void 0 ? void 0 : _t.sites) === null || _u === void 0 ? void 0 : _u.length) ? ((_v = state === null || state === void 0 ? void 0 : state.job) === null || _v === void 0 ? void 0 : _v.sites.map((site) => (h("floodteam-item-site", { onClick: (event) => {
                    event.preventDefault();
                    event.stopPropagation();
                    this.openSiteEditModal(site);
                  }, site: site })))) : (h("floodteam-fallback", { message: "No Sites Added...", icon: "cube" }))))),
            h("ion-col", { size: "12", sizeMd: "6" },
              h("floodteam-title-bar", { heading: 4, icon: "thermometer", disableUnderline: true },
                h("ion-text", null, "Readings"),
                h("ion-button", { fill: "outline", href: "#", slot: "end", size: "small", onClick: (event) => this.openaddReadingPopover(event) }, "Add")),
              h("ion-card", null,
                h("ion-item", null,
                  h("ion-select", { placeholder: "Select a View", name: "readingsView", interfaceOptions: {
                      header: "Select a View",
                    }, value: this.readingsView },
                    h("ion-select-option", { value: "readings" }, "MC Readings"),
                    h("ion-select-option", { value: "grainsPerPound" }, "Grains Per Pound"),
                    h("ion-select-option", { value: "temperature" }, "Temperature"),
                    h("ion-select-option", { value: "evaporationPotential" }, "Evaporation Potential"))),
                ((_x = (_w = state === null || state === void 0 ? void 0 : state.job) === null || _w === void 0 ? void 0 : _w.readings) === null || _x === void 0 ? void 0 : _x.length) ? (h("fireenjin-graph", { type: "line", labels: (((_y = state === null || state === void 0 ? void 0 : state.job) === null || _y === void 0 ? void 0 : _y.readings)
                    ? (_z = state.job) === null || _z === void 0 ? void 0 : _z.readings.reverse()
                    : []).map((reading) => reading.name), datasets: this.readingsGraphData() })) : (h("floodteam-fallback", { icon: "bar-chart", message: "No Readings Added..." })),
                h("ion-list", null,
                  h("ion-item-divider", null,
                    "Moisture Content",
                    h("ion-button", { fill: "clear", slot: "end", onClick: (event) => this.openReadingSelect(event) },
                      this.selectedReadingDate || "None",
                      h("ion-icon", { slot: "end", name: "today" }))),
                  h("div", null,
                    selectedWeather && (h("ion-item", null,
                      ((_1 = (_0 = selectedWeather === null || selectedWeather === void 0 ? void 0 : selectedWeather.conditions) === null || _0 === void 0 ? void 0 : _0[0]) === null || _1 === void 0 ? void 0 : _1.icon) && (h("ion-img", { style: {
                          height: "50px",
                          width: "50px",
                          marginRight: "0",
                        }, slot: "start", src: `/assets/images/${((_3 = (_2 = selectedWeather === null || selectedWeather === void 0 ? void 0 : selectedWeather.conditions) === null || _2 === void 0 ? void 0 : _2[0]) === null || _3 === void 0 ? void 0 : _3.icon)
                          ? "weather/" +
                            selectedWeather.conditions[0].icon
                          : "icon"}.png` })),
                      h("ion-label", null,
                        h("ion-grid", null,
                          h("ion-row", null,
                            h("ion-col", { class: "ion-text-left" },
                              h("ion-icon", { color: "success", name: "arrow-up", style: { opacity: "0.6" } }), selectedWeather === null || selectedWeather === void 0 ? void 0 :
                              selectedWeather.temperatureMax,
                              "\u00B0"),
                            h("ion-col", { class: "ion-text-left" }, selectedWeather === null || selectedWeather === void 0 ? void 0 :
                              selectedWeather.temperatureMin,
                              "\u00B0",
                              h("ion-icon", { color: "danger", name: "arrow-down", style: { opacity: "0.6" } })))),
                        h("p", { style: { textIndent: "15px" } }, (_5 = (_4 = selectedWeather === null || selectedWeather === void 0 ? void 0 : selectedWeather.conditions) === null || _4 === void 0 ? void 0 : _4[0]) === null || _5 === void 0 ? void 0 : _5.description)))),
                    (((_6 = state === null || state === void 0 ? void 0 : state.job) === null || _6 === void 0 ? void 0 : _6.sites) ? (_7 = state.job) === null || _7 === void 0 ? void 0 : _7.sites : [])
                      .filter((site) => { var _a; return (_a = site === null || site === void 0 ? void 0 : site.dots) === null || _a === void 0 ? void 0 : _a.length; })
                      .map((site) => [
                      h("ion-item-divider", { style: { display: "block" } }, site === null || site === void 0 ? void 0 :
                        site.name,
                        h("ion-chip", { disabled: true, slot: "end" },
                          (site === null || site === void 0 ? void 0 : site.chamber) || 0,
                          h("ion-icon", { name: "grid" }))),
                      h("ion-grid", null,
                        h("ion-row", null, ((site === null || site === void 0 ? void 0 : site.dots) ? site.dots : []).map((dot, index) => {
                          var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
                          return (h("ion-col", null,
                            h("div", { class: "floodteam-dot", style: { color: "var(--ion-text-color)" } },
                              (dot === null || dot === void 0 ? void 0 : dot.name) || index + 1,
                              ((_l = (_k = (_j = (_h = (_g = (_f = (_e = (_d = (_c = (_b = (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.readings) === null || _b === void 0 ? void 0 : _b.find) === null || _c === void 0 ? void 0 : _c.call(_b, (reading) => (reading === null || reading === void 0 ? void 0 : reading.name) ===
                                this.selectedReadingDate)) === null || _d === void 0 ? void 0 : _d.sites) === null || _e === void 0 ? void 0 : _e.find) === null || _f === void 0 ? void 0 : _f.call(_e, (readingSite) => (readingSite === null || readingSite === void 0 ? void 0 : readingSite.id) === (site === null || site === void 0 ? void 0 : site.id))) === null || _g === void 0 ? void 0 : _g.dots) === null || _h === void 0 ? void 0 : _h.find) === null || _j === void 0 ? void 0 : _j.call(_h, (readingDot) => (readingDot === null || readingDot === void 0 ? void 0 : readingDot.id) === (dot === null || dot === void 0 ? void 0 : dot.id))) === null || _k === void 0 ? void 0 : _k.moistureContent) === null || _l === void 0 ? void 0 : _l[this.readingsView]) && (h("ion-badge", { style: {
                                  position: "absolute",
                                  bottom: "0px",
                                  right: "0px",
                                } }, (_x = (_w = (_v = (_u = (_t = (_s = (_r = (_q = (_p = (_o = (_m = state === null || state === void 0 ? void 0 : state.job) === null || _m === void 0 ? void 0 : _m.readings) === null || _o === void 0 ? void 0 : _o.find) === null || _p === void 0 ? void 0 : _p.call(_o, (reading) => (reading === null || reading === void 0 ? void 0 : reading.name) ===
                                this.selectedReadingDate)) === null || _q === void 0 ? void 0 : _q.sites) === null || _r === void 0 ? void 0 : _r.find) === null || _s === void 0 ? void 0 : _s.call(_r, (readingSite) => (readingSite === null || readingSite === void 0 ? void 0 : readingSite.id) === (site === null || site === void 0 ? void 0 : site.id))) === null || _t === void 0 ? void 0 : _t.dots) === null || _u === void 0 ? void 0 : _u.find) === null || _v === void 0 ? void 0 : _v.call(_u, (readingDot) => (readingDot === null || readingDot === void 0 ? void 0 : readingDot.id) === (dot === null || dot === void 0 ? void 0 : dot.id))) === null || _w === void 0 ? void 0 : _w.moistureContent) === null || _x === void 0 ? void 0 :
                                _x[this.readingsView],
                                this.readingsView === "temperature" ? (h("span", null, "\u00B0")) : (""))))));
                        }))),
                    ]))))),
            h("ion-col", { size: "12", sizeMd: "6" },
              h("floodteam-title-bar", { heading: 4, disableUnderline: true, icon: "people" },
                h("ion-text", null, "Users"),
                h("ion-button", { fill: "outline", href: "#", slot: "end", size: "small", onClick: () => this.openUserSearch() }, "Add")),
              h("ion-card", null,
                h("ion-list", { ref: (el) => (this.userListEl = el) }, ((_9 = (_8 = state === null || state === void 0 ? void 0 : state.job) === null || _8 === void 0 ? void 0 : _8.users) === null || _9 === void 0 ? void 0 : _9.length) ? ((_10 = state.job) === null || _10 === void 0 ? void 0 : _10.users.map((user) => {
                  var _a;
                  return (h("floodteam-item-user", { role: this.userRoleMap[user === null || user === void 0 ? void 0 : user.id] || null, user: user, jobId: (_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.id }));
                })) : (h("floodteam-fallback", { icon: "people", message: "No Users Added..." }))))),
            h("ion-col", { size: "12", sizeMd: "6" },
              h("floodteam-title-bar", { heading: 4, icon: "file-tray-full", disableUnderline: true },
                h("ion-text", null, "Efiles"),
                h("ion-button", { fill: "outline", href: "#", slot: "end", size: "small", onClick: () => {
                    (this.isUploading = true),
                      this.fireenjinTrigger.emit({
                        name: "upload",
                        multiple: true,
                        payload: {
                          jobId: this.jobId,
                        },
                      });
                  } }, "Add")),
              h("ion-card", null,
                h("ion-accordion-group", null,
                  h("ion-accordion", null,
                    h("ion-item", { slot: "header" },
                      h("ion-icon", { name: "file-tray-full", slot: "start" }),
                      h("ion-label", null, "Inbox"),
                      ((_11 = this.preparingEfiles) === null || _11 === void 0 ? void 0 : _11.length) ? (h("ion-button", { onClick: (event) => this.openSendEfileModal(event), slot: "end", fill: "solid", color: "primary", size: "small" },
                        h("ion-label", null, "Send "),
                        h("ion-text", { slot: "end" },
                          "\u00A0",
                          ((_12 = this.preparingEfiles) === null || _12 === void 0 ? void 0 : _12.length) || 0))) : (h("ion-text", { color: ((_14 = (_13 = state === null || state === void 0 ? void 0 : state.job) === null || _13 === void 0 ? void 0 : _13.efiles) === null || _14 === void 0 ? void 0 : _14.length) > 0
                          ? "primary"
                          : "medium", slot: "end" }, ((_16 = (_15 = state === null || state === void 0 ? void 0 : state.job) === null || _15 === void 0 ? void 0 : _15.efiles) === null || _16 === void 0 ? void 0 : _16.length) || 0))),
                    h("ion-list", { class: {
                        "is-loading": this.efileLoading,
                      }, slot: "content", ref: (el) => (this.efileListEl = el) },
                      h("fireenjin-chip-bar", null,
                        h("ion-chip", { onClick: () => this.generatePdf("CcLLkmgyUfeQvQVEWnLp", "Photos Pdf") },
                          h("ion-icon", { class: "ft-icon-style", name: "document-text-outline" }),
                          h("ion-label", null, "Photo PDF")),
                        h("ion-chip", { onClick: () => this.generatePdf("QMzg5GspcjqouWNxArbh", "Readings PDF") },
                          h("ion-icon", { class: "ft-icon-style", name: "document-text-outline" }),
                          h("ion-label", null, "Readings PDF")),
                        h("ion-chip", { onClick: () => this.generatePdf("tA7FCnTudK0Sd887IeuU", "Invoice PDF") },
                          h("ion-icon", { class: "ft-icon-style", name: "document-text-outline" }),
                          h("ion-label", null, "Invoice PDF"))),
                      ((_17 = this.efiles) === null || _17 === void 0 ? void 0 : _17.length) ? (this.efiles
                        .sort((a, b) => ((a === null || a === void 0 ? void 0 : a.name) > (b === null || b === void 0 ? void 0 : b.name) ? 1 : -1))
                        .map((efile) => {
                        var _a, _b;
                        return [
                          h("ion-item-sliding", null,
                            h("ion-item", { detail: true, target: "_blank", href: efile === null || efile === void 0 ? void 0 : efile.url },
                              h("ion-checkbox", { slot: "start", name: `efile-${efile === null || efile === void 0 ? void 0 : efile.id}`, value: efile === null || efile === void 0 ? void 0 : efile.url }),
                              h("ion-label", { class: "ion-text-wrap" },
                                h("p", null, efile === null || efile === void 0 ? void 0 : efile.name)),
                              ((_a = efile === null || efile === void 0 ? void 0 : efile.history) === null || _a === void 0 ? void 0 : _a.length) ? (h("ion-button", { size: "large", color: "primary", fill: "clear", slot: "end", onClick: (event) => {
                                  var _a;
                                  event.preventDefault();
                                  event.stopPropagation();
                                  if ((_a = this.showingEfileHistory) === null || _a === void 0 ? void 0 : _a.includes(efile.id)) {
                                    this.showingEfileHistory =
                                      this.showingEfileHistory.filter((efileId) => efileId !== (efile === null || efile === void 0 ? void 0 : efile.id));
                                  }
                                  else {
                                    this.showingEfileHistory = [
                                      ...this.showingEfileHistory,
                                      efile.id,
                                    ];
                                  }
                                } },
                                h("ion-icon", { name: "time", slot: "icon-only" }))) : ("")),
                            h("ion-item-options", null,
                              h("ion-item-option", { color: "danger", onClick: () => this.removeEfileFromJob(efile) },
                                h("ion-icon", { slot: "end", name: "close-circle" }),
                                "Remove"))),
                          !!((_b = efile === null || efile === void 0 ? void 0 : efile.history) === null || _b === void 0 ? void 0 : _b.length) && (h("ion-list", { style: {
                              display: this.showingEfileHistory.includes(efile.id)
                                ? "block"
                                : "none",
                            } }, efile.history.map((oldEfile) => (h("ion-item-sliding", null,
                            h("ion-item", { detail: true, target: "_blank", href: oldEfile.url },
                              h("ion-checkbox", { slot: "start", name: `efile-${oldEfile.id}`, value: oldEfile.url }),
                              h("ion-label", { class: "ion-text-wrap" },
                                h("p", null, oldEfile.name))),
                            h("ion-item-options", null,
                              h("ion-item-option", { color: "danger", onClick: () => this.removeEfileFromJob(oldEfile) },
                                h("ion-icon", { slot: "end", name: "close-circle" }),
                                "Remove"))))))),
                        ];
                      })) : (h("floodteam-fallback", { icon: "file-tray-full", message: "No Files Uploaded..." })))),
                  h("ion-accordion", null,
                    h("ion-item", { slot: "header" },
                      h("ion-icon", { slot: "start", name: "create" }),
                      h("ion-label", null, "Contracts")),
                    h("ion-list", { slot: "content" }, (state.templates || [])
                      .filter((template) => {
                      var _a, _b;
                      return (template === null || template === void 0 ? void 0 : template.type) === "contract" &&
                        ((_b = (_a = template === null || template === void 0 ? void 0 : template.tags) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, "job"));
                    })
                      .map((contract) => {
                      var _a, _b;
                      return (h("ion-item", { detail: true, onClick: (event) => this.openEsignModal(event, contract === null || contract === void 0 ? void 0 : contract.id), href: `${(_a = state.config) === null || _a === void 0 ? void 0 : _a.url}/jobs/${this.jobId}/sign/${contract === null || contract === void 0 ? void 0 : contract.id}` },
                        (((_b = state === null || state === void 0 ? void 0 : state.job) === null || _b === void 0 ? void 0 : _b.efiles) || [])
                          .map((efile) => {
                          var _a;
                          return (_a = efile === null || efile === void 0 ? void 0 : efile.template) === null || _a === void 0 ? void 0 : _a.id;
                        })
                          .includes(contract === null || contract === void 0 ? void 0 : contract.id) && (h("ion-icon", { style: { marginRight: "10px" }, name: "checkmark-circle", color: "primary", slot: "start" })),
                        h("ion-label", { class: "ion-text-wrap" },
                          h("p", null, (contract === null || contract === void 0 ? void 0 : contract.subject)
                            ? contract.subject
                            : "No Name...")),
                        h("ion-button", { slot: "end", color: "primary", fill: "clear", size: "small", onClick: (event) => this.send(event, contract) },
                          h("ion-icon", { slot: "start", name: "mail" }),
                          "Send")));
                    }))))))))),
      h("floodteam-navigation-footer", null,
        h("floodteam-title-bar", { back: "/jobs", disableUnderline: true },
          h("ion-breadcrumbs", null,
            h("ion-breadcrumb", { href: "/jobs" },
              h("ion-icon", { class: "ft-icon-style", slot: "start", name: "water" }),
              "Jobs"),
            h("ion-breadcrumb", { href: "#", onClick: () => {
                var _a;
                return this.fireenjinTrigger.emit({
                  name: "pin",
                  payload: {
                    icon: "water",
                    href: `/jobs/${this.jobId}`,
                    name: ((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.title) || "New Job",
                  },
                });
              } },
              h("ion-icon", { color: ((_18 = ((state === null || state === void 0 ? void 0 : state.pins) || [])
                  .map((pin) => pin === null || pin === void 0 ? void 0 : pin.href)) === null || _18 === void 0 ? void 0 : _18.includes(`/jobs/${this.jobId}`))
                  ? "primary"
                  : null, slot: "start", name: "pin" }),
              ((_19 = state === null || state === void 0 ? void 0 : state.job) === null || _19 === void 0 ? void 0 : _19.title) || "???")),
          h("ion-buttons", { slot: "end" },
            h("ion-button", { color: "primary", fill: "solid", onClick: () => this.openJobEdit() }, "Edit")))),
    ];
  }
  static get is() { return "app-job"; }
  static get originalStyleUrls() { return {
    "$": ["app-job.css"]
  }; }
  static get styleUrls() { return {
    "$": ["app-job.css"]
  }; }
  static get properties() { return {
    "jobId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "job-id",
      "reflect": false
    }
  }; }
  static get states() { return {
    "showingEfileHistory": {},
    "efiles": {},
    "isUploading": {},
    "efileLoading": {},
    "isSending": {},
    "template": {},
    "activityFeed": {},
    "isSiteModalOpen": {},
    "isEndOfFeed": {},
    "payModels": {},
    "feedLimit": {},
    "siteId": {},
    "currentSlide": {},
    "showControls": {},
    "sidebarContentEl": {},
    "isUploadingPhoto": {},
    "activityFeedEl": {},
    "selectedReadingDate": {},
    "readingsView": {},
    "selectedImage": {},
    "preparingEfiles": {}
  }; }
  static get events() { return [{
      "method": "fireenjinFetch",
      "name": "fireenjinFetch",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinFetchEvent",
        "resolved": "FireEnjinFetchEvent",
        "references": {
          "FireEnjinFetchEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "method": "fireenjinTrigger",
      "name": "fireenjinTrigger",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinTriggerInput",
        "resolved": "FireEnjinTriggerInput",
        "references": {
          "FireEnjinTriggerInput": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "method": "fireenjinSubmit",
      "name": "fireenjinSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinSubmitInput",
        "resolved": "FireEnjinSubmitInput",
        "references": {
          "FireEnjinSubmitInput": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }]; }
  static get elementRef() { return "jobPageEl"; }
  static get listeners() { return [{
      "name": "ionSlideDidChange",
      "method": "onSlideChange",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "ionSlideTap",
      "method": "onSlideTap",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamCloseModal",
      "method": "onCloseModa",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "fireenjinTrigger",
      "method": "openReadingModal",
      "target": "document",
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamOpenSiteEditModal",
      "method": "onOpenSiteEditModal",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamSelect",
      "method": "onSelect",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "ionChange",
      "method": "onChange",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "ionRouteDidChange",
      "method": "getData",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=app-job.js.map