<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="cust-customer-notes"
      data-list-key="CustomerNoteDataList"
      :show-select="rights.read"
      :reload="reload"
      :client-criteria="clientCriteria"
      @selection-change="handleSelected"
      data-cy="customerNotesTable"
      :pre-filter-mode="preFilterMode"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "cust-customer-notes";
export default {
  created() {
    this.customerId = parseInt(this.$route.params.customerid);
    //REQUIRED NON-OPTIONAL FILTER
    this.clientCriteria = this.customerId.toString();
    this.preFilterMode = {
      icon: "$ayiAddressCard",
      id: window.$gz.util.stringToIntOrNull(this.$route.params.customerid),
      ayatype: window.$gz.type.Customer,
      viz: this.$route.params.customername,
      clearable: false
    };
    this.rights = window.$gz.role.getRights(window.$gz.type.CustomerNote);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      customerId: undefined,
      clientCriteria: undefined,
      preFilterMode: null,
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.CustomerNote,
      selectedItems: [],
      reload: false
    };
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    },
    ayaTypes: function() {
      return window.$gz.type;
    }
  }
};
/////////////////////////////
//
//
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-note-edit",
          params: {
            recordid: 0,
            customerid: m.vm.customerId
          }
        });
        break;
      case "extensions":
        const res = await m.vm.$refs.extensions.open(
          m.vm.$refs.gzdatatable.getDataListSelection(
            window.$gz.type.CustomerNote
          )
        );
        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.CustomerNote
            ),
            m.id
          );
          if (res == null) {
            return;
          }
          window.$gz.form.setLastReport(FORM_KEY, res);
          generateMenu(m.vm);
        }
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: false,
    readOnly: !vm.rights.change,
    icon: "$ayiClipboard",
    title: "CustomerNoteList",
    helpUrl: "customer-notes",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.CustomerNote
    }
  };

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