<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      :aya-type="aType"
      :selected-items="selectedItems"
      ref="extensions"
    >
    </gz-extensions>
    <gz-data-table
      ref="gzdatatable"
      form-key="workorder-item-loan-list"
      data-list-key="WorkOrderItemLoanDataList"
      :show-select="rights.read"
      :reload="reload"
      @selection-change="handleSelected"
      data-cy="WorkOrderItemLoansTable"
      :client-criteria="clientCriteria"
      :pre-filter-mode="preFilterMode"
      @clear-pre-filter="clearPreFilter"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "workorder-item-loan-list";
export default {
  created() {
    this.rights = window.$gz.role.getRights(window.$gz.type.WorkOrder);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    //------ 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);
  },
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.WorkOrderItemLoan,
      selectedItems: [],
      reload: false,
      clientCriteria: undefined,
      preFilterMode: null,
      objectId: null,
      aForType: null,
      name: null
    };
  },
  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 "new":
        m.vm.$router.push({
          name: "workorder-edit",
          params: { recordid: 0 }
        });
        break;
      case "extensions":
        const res = await m.vm.$refs.extensions.open(
          m.vm.$refs.gzdatatable.getDataListSelection(
            window.$gz.type.WorkOrderItemLoan
          )
        );
        if (res && res.refresh == true) {
          m.vm.reload = !m.vm.reload;
        }
        break;
      case "report":
        if (m.id != null) {
          m.vm.$router.push({
            name: "ay-report",
            params: {
              recordid: m.id,
              ayatype: window.$gz.type.WorkOrderItemLoan
            }
          });
        } else {
          const res = await m.vm.$refs.reportSelector.open(
            m.vm.$refs.gzdatatable.getDataListSelection(
              window.$gz.type.WorkOrderItemLoan
            )
          );

          if (res == null) {
            return;
          }

          window.$gz.form.setLastReport(FORM_KEY, res);

          m.vm.$router.push({
            name: "ay-report",
            params: {
              recordid: res.id,
              ayatype: window.$gz.type.WorkOrderItemLoan
            }
          });
        }
        break;
      case "WorkOrderList":
        m.vm.$router.push({
          name: "svc-workorders"
        });
        break;
      case "WorkOrderItemList":
        m.vm.$router.push({
          name: "svc-workorder-items"
        });
        break;

      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: true,
    icon: "$ayiPlug",
    title: "WorkOrderItemLoanList",
    helpUrl: "svc-workorders",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.WorkOrderItemLoan
    }
  };

  if (vm.rights.change) {
    menuOptions.menuItems.push({
      title: "New",
      icon: "$ayiPlus",
      surface: true,
      key: FORM_KEY + ":new",
      vm: vm
    });
  }

  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
  });

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

  menuOptions.menuItems.push({
    title: "WorkOrderList",
    icon: "$ayiTools",
    key: FORM_KEY + ":WorkOrderList",
    vm: vm
  });

  menuOptions.menuItems.push({
    title: "WorkOrderItemList",
    icon: "$ayiWrench",
    key: FORM_KEY + ":WorkOrderItemList",
    vm: vm
  });

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

  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
</script>
