<template>
  <div>
    <gz-data-table
      ref="gzdatatable"
      form-key="csr-list"
      data-list-key="CustomerServiceRequestDataList"
      :reload="reload"
      :show-filter-manager="false"
      data-cy="csrTable"
      @selection-change="handleSelected"
    >
    </gz-data-table>
  </div>
</template>

<script>
const FORM_KEY = "customer-csr-list";
export default {
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.CustomerServiceRequest,
      selectedItems: [],
      reload: false
    };
  },
  created() {
    this.rights = window.$gz.role.getRights(
      window.$gz.type.CustomerServiceRequest
    );
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    }
  }
};

/////////////////////////////
//
//
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-csr-edit",
          params: { recordid: 0 }
        });
        break;

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

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: true,
    icon: "$ayiConciergeBell",
    title: "CustomerServiceRequestList",
    helpUrl: "~customer~customer-csr",
    hideSearch: true,
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.CustomerServiceRequest
    }
  };

  if (vm.rights.change) {
    menuOptions.menuItems.push({
      title: "New",
      icon: "$ayiPlus",
      surface: true,
      key: FORM_KEY + ":new",
      vm: vm
    });
  }
  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
</script>
