<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="trial-license-request-list"
      data-list-key="TrialLicenseRequestDataList"
      :show-select="rights.read"
      :reload="reload"
      data-cy="trialLicenseRequestTable"
      :client-criteria="clientCriteria"
      :pre-filter-mode="preFilterMode"
      @selection-change="handleSelected"
      @clear-pre-filter="clearPreFilter"
    >
    </gz-data-table>
  </div>
</template>

<script>
const FORM_KEY = "trial-license-request-list";
export default {
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.TrialLicenseRequest,
      selectedItems: [],
      reload: false,
      clientCriteria: undefined,
      preFilterMode: null
    };
  },
  created() {
    this.rights = window.$gz.role.getRights(
      window.$gz.type.TrialLicenseRequest
    );
    window.$gz.eventBus.$on("menu-click", clickHandler);

    //------ pre-filter ----
    //OPTIONAL "Show All customers of head office" 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,
        socktype: this.aForType,
        viz: this.$route.params.name,
        clearable: true
      };
    }
    //------ /pre-filter ----
    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) {
      case "new":
        m.vm.$router.push({
          name: "customer-edit",
          params: { recordid: 0 }
        });
        break;
      case "extensions":
        {
          const res = await m.vm.$refs.extensions.open(
            m.vm.$refs.gzdatatable.getDataListSelection(
              window.$gz.type.TrialLicenseRequest
            )
          );
          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.TrialLicenseRequest
            ),
            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: "$sockiHandHoldingWater",
    title: "TrialLicenseRequestList",
    menuItems: [],
    formData: {
      sockType: window.$gz.type.TrialLicenseRequest
    }
  };

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

  menuOptions.menuItems.push({
    title: "Report",
    icon: "$sockiFileAlt",
    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: "$sockiFileAlt",
      key: FORM_KEY + ":report:" + lastReport.id,
      vm: vm
    });
  }

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