<template>
  <gz-data-table
    formKey="widget-list"
    :dataListKey="dataListKey"
    :dataListFilter="dataListFilter"
    :dataListSort="dataListSort"
    :showSelect="false"
    :singleSelect="false"
    v-on:update:selected="handleSelected"
  >
  </gz-data-table>
</template>

<script>
import GzDataTable from "../components/gz-data-table.vue";
const FORM_KEY = "widget-list";
export default {
  components: {
    GzDataTable
  },
  created() {
    this.rights = window.$gz.role.getRights(window.$gz.type.Widget);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      currentListViewId: 1,
      dataListKey: "TestWidgetDataList",
      dataListFilter: "",
      dataListSort: "",
      rights: window.$gz.role.defaultRightsObject()
    };
  },
  methods: {
    handleSelected(selectedItems) {
      console.log(selectedItems);
    }
  }
};

/////////////////////////////
//
//
function clickHandler(menuItem) {
  if (!menuItem) {
    return;
  }
  let m = window.$gz.menu.parseMenuItem(menuItem);
  if (m.owner == FORM_KEY && !m.disabled) {
    switch (m.key) {
      case "new":
        m.vm.$router.push({
          name: "widget-edit",
          params: { recordid: 0 }
        });
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  let menuOptions = {
    isMain: true,
    icon: "fa-vial",
    title: vm.$ay.t("WidgetList"),
    helpUrl: "form-ay-data-list-view",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.Widget
    }
  };

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

  //STUB REPORTS
  //Report not Print, print is a further option
  menuOptions.menuItems.push({
    title: vm.$ay.t("Report"),
    icon: "fa-file-alt",
    key: FORM_KEY + ":report",
    vm: vm
  });

  menuOptions.menuItems.push({
    title: "stub: Last report used",
    icon: "fa-file-alt",
    key: FORM_KEY + ":report:STUBlastusedreportid",
    vm: vm
  });

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