<template>
  <div>
    <gz-data-table
      ref="gzdatatable"
      form-key="workorder-list"
      data-list-key="CustomerWorkOrderDataList"
      :show-select="rights.read"
      :reload="reload"
      data-cy="workordersTable"
      :client-criteria="clientCriteria"
      :pre-filter-mode="preFilterMode"
      @selection-change="handleSelected"
      @clear-pre-filter="clearPreFilter"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "customer-workorder-list";
export default {
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.WorkOrder,
      selectedItems: [],
      reload: false,
      openDialog: false,
      openWoNumber: null,
      clientCriteria: undefined,
      preFilterMode: null,
      objectId: null,
      aForType: null,
      name: null
    };
  },
  async created() {
    this.rights = window.$gz.role.getRights(window.$gz.type.WorkOrder);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    await fetchTranslatedText();

    //------ Show all ----
    //OPTIONAL "Show All" FILTER
    this.objectId = window.$gz.util.stringToIntOrNull(
      this.$route.params.objectId
    );
    this.aForType = window.$gz.util.stringToIntOrNull(this.$route.params.aType);

    if (this.objectId && this.objectId != 0 && this.aForType) {
      //OBJECTID,AYATYPE
      this.clientCriteria = `${this.objectId},${this.aForType}`;

      this.preFilterMode = {
        icon: window.$gz.util.iconForType(this.aForType),
        id: this.objectId,
        ayatype: this.aForType,
        viz: this.$route.params.name,
        clearable: true
      };
    }
    //------ /show all ----

    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    },
    clearPreFilter() {
      this.clientCriteria = null;
      this.preFilterMode = null;
      this.reload = !this.reload;
    }
  }
};

/////////////////////////////
//
//
async function clickHandler(menuItem) {
  if (!menuItem) {
    return;
  }
  const m = window.$gz.menu.parseMenuItem(menuItem);
  if (m.owner == FORM_KEY && !m.disabled) {
    switch (m.key) {
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu() {
  const menuOptions = {
    isMain: true,
    icon: "$ayiTools",
    title: "WorkOrderList",
    helpUrl: "customer-workorders",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.WorkOrder
    }
  };

  menuOptions.menuItems.push({ divider: true, inset: false });

  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
//////////////////////////////////////////////////////////
//
// Ensures UI translated text is available
//
async function fetchTranslatedText() {
  await window.$gz.translation.cacheTranslations(["WorkOrder"]);
}
</script>
