<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      ref="extensions"
      :aya-type="aType"
      :selected-items="selectedItems"
    >
    </gz-extensions>
    <gz-data-table
      ref="gzdatatable"
      form-key="part-inventory-request-list"
      data-list-key="PartInventoryRequestDataList"
      :show-select="rights.read"
      :reload="reload"
      :client-criteria="clientCriteria"
      data-cy="partInventoryRequestTable"
      :pre-filter-mode="preFilterMode"
      @selection-change="handleSelected"
      @clear-pre-filter="clearPreFilter"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "part-inventory-request-list";
export default {
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.PartInventoryRequestDataList,
      selectedItems: [],
      clientCriteria: undefined,
      preFilterMode: null,
      reload: false
    };
  },
  created() {
    const vm = this;
    vm.rights = window.$gz.role.getRights(
      window.$gz.type.PartInventoryRequestDataList
    );
    window.$gz.eventBus.$on("menu-click", clickHandler);
    //Generate client criteria to send if filtered by part, whs or both
    //ClientCriteria is optional for this list, Format for this list is "PARTID,WAREHOUSEID" where the id is 0 if not filtered or the id to filter
    //OPTIONAL FILTER
    if (vm.$route.params.filter) {
      vm.preFilterMode = {
        icon: vm.$route.params.filter.icon,
        id: window.$gz.util.stringToIntOrNull(vm.$route.params.filter.objectId),
        ayatype: window.$gz.util.stringToIntOrNull(
          vm.$route.params.filter.ayatype
        ),
        viz: vm.$route.params.filter.viz,
        clearable: true
      };
      vm.clientCriteria = `${
        vm.$route.params.filter.PartName
          ? vm.$route.params.filter.PartName
          : "0"
      },${
        vm.$route.params.filter.PartWarehouseName
          ? vm.$route.params.filter.PartWarehouseName
          : ""
      }`;
    }
    generateMenu(vm);
  },
  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) {
      case "extensions":
        {
          const res = await m.vm.$refs.extensions.open(
            m.vm.$refs.gzdatatable.getDataListSelection(
              window.$gz.type.PartInventoryRequestDataList
            )
          );
          if (res && res.refresh == true) {
            m.vm.reload = !m.vm.reload;
          }
        }
        break;
      case "report":
        {
          const res = await m.vm.$refs.reportSelector.open(
            m.vm.$refs.gzdatatable.getDataListSelection(
              window.$gz.type.PartInventoryRequestDataList
            ),
            m.id
          );
          if (res == null) {
            return;
          }
          window.$gz.form.setLastReportMenuItem(FORM_KEY, res, m.vm);
        }
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: true,
    icon: "$ayiParachuteBox",
    title: "WorkOrderItemPartRequestList",
    helpUrl: "inv-part-inventory-requests",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.PartInventoryRequestDataList
    }
  };

  menuOptions.menuItems.push({
    title: "Report",
    icon: "$ayiFileAlt",
    key: FORM_KEY + ":report",
    vm: vm
  });

  const lastReport = window.$gz.form.getLastReport(FORM_KEY);
  if (lastReport != null) {
    menuOptions.menuItems.push({
      title: lastReport.name,
      notrans: true,
      icon: "$ayiFileAlt",
      key: FORM_KEY + ":report:" + lastReport.id,
      vm: vm
    });
  }

  menuOptions.menuItems.push({
    title: "Extensions",
    icon: "$ayiPuzzlePiece",
    key: FORM_KEY + ":extensions",
    vm: vm
  });
  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
</script>
