<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="contact-users"
      data-list-key="OutsideUserDataList"
      :show-select="rights.read"
      :reload="reload"
      data-cy="custUsersTable"
      @selection-change="handleSelected"
    >
    </gz-data-table>
  </div>
</template>

<script>
const FORM_KEY = "contact-users";
export default {
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.User,
      selectedItems: [],
      reload: false
    };
  },
  created() {
    this.rights = window.$gz.role.getRights(window.$gz.type.User);
    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 "extensions":
        {
          const res = await m.vm.$refs.extensions.open(
            m.vm.$refs.gzdatatable.getDataListSelection(window.$gz.type.User)
          );
          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.User),
            m.id
          );
          if (res == null) {
            return;
          }
          window.$gz.form.setLastReportMenuItem(FORM_KEY, res, m.vm);
        }
        break;
      case "directnotify":
        //nav to direct notify with list of users appended to route
        {
          const selected = m.vm.$refs.gzdatatable.getDataListSelection(
            window.$gz.type.User
          ).selectedRowIds;
          if (selected.length == 0) {
            m.vm.$router.push({
              name: "home-notify-direct"
            });
          } else {
            m.vm.$router.push({
              name: "home-notify-direct",
              params: { userIdList: selected.toString() }
            });
          }
        }
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: true,
    icon: "$ayiUsers",
    title: "Contacts",
    helpUrl: "cust-contacts",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.User
    }
  };

  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({
    title: "DirectNotification",
    icon: "$ayiCommentAlt",
    key: FORM_KEY + ":directnotify",
    vm: vm
  });

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