<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      :ayaType="ayType"
      :selectedItems="selectedItems"
      ref="extensions"
    >
    </gz-extensions>
    <gz-data-table
      ref="gzdatatable"
      formKey="cust-users"
      :dataListKey="dataListKey"
      :dataListFilter="dataListFilter"
      :dataListSort="dataListSort"
      :showSelect="rights.read"
      :reload="reload"
      :metaView="metaView"
      v-on:selection-change="handleSelected"
      data-cy="custUsersTable"
    >
    </gz-data-table>
  </div>
</template>

<script>
const FORM_KEY = "cust-users";
export default {
  created() {
    //outside users only
    this.metaView = JSON.stringify([
      {
        fld: "usertype",
        filter: {
          items: [
            { op: "=", value: 3 },
            { op: "=", value: 4 }
          ],
          any: true
        }
      }
    ]);

    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);
  },
  data() {
    return {
      currentListViewId: 1,
      dataListKey: "OutsideUserDataList",
      dataListFilter: "",
      dataListSort: "",
      metaView: undefined,
      rights: window.$gz.role.defaultRightsObject(),
      ayType: window.$gz.type.User,
      selectedItems: [],
      reload: false
    };
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    }
  }
};

/////////////////////////////
//
//
async 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: "cust-user",
          params: { recordid: 0 }
        });
        break;
      case "extensions":
        let 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":
        if (m.id != null) {
          //last report selected is in m.id
          m.vm.$router.push({
            name: "ay-report",
            params: { recordid: m.id, ayatype: window.$gz.type.User }
          });
        } else {
          //general report selector chosen

          let res = await m.vm.$refs.reportSelector.open(
            m.vm.$refs.gzdatatable.getDataListSelection(window.$gz.type.User)
          );

          //if null for no selection
          //just bail out
          if (res == null) {
            return;
          }
          //persist last report selected
          window.$gz.form.setLastReport(FORM_KEY, res);

          //Now open the report viewer...
          m.vm.$router.push({
            name: "ay-report",
            params: { recordid: res.id, ayatype: window.$gz.type.User }
          });
        }
        break;
      case "directnotify":
        //nav to direct notify with list of users appended to route
        let 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() }
            //params: { userIdList: m.vm.selected.map(z => z.id).toString() }
          });
        }
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

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

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

  //REPORTS
  //Report not Print, print is a further option
  menuOptions.menuItems.push({
    title: "Report",
    icon: "$ayiFileAlt",
    key: FORM_KEY + ":report",
    vm: vm
  });

  //get last report selected
  let lastReport = window.$gz.form.getLastReport(FORM_KEY);
  if (lastReport != null) {
    menuOptions.menuItems.push({
      title: lastReport.name,
      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>
