<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      :aya-type="$ay.ayt().Review"
      :selected-items="selectedItems"
      ref="extensions"
    >
    </gz-extensions>
    <gz-data-table
      ref="gzdatatable"
      form-key="review-list"
      data-list-key="ReviewDataList"
      :client-criteria="clientCriteria"
      :show-select="rights.read"
      :reload="reload"
      @selection-change="handleSelected"
      data-cy="reviewsTable"
      :pre-filter-mode="preFilterMode"
      @clear-pre-filter="clearPreFilter"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "review-list";
export default {
  async created() {
    const vm = this;
    vm.rights = window.$gz.role.getRights(window.$gz.type.Review);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    vm.objectId = window.$gz.util.stringToIntOrNull(vm.$route.params.objectId);
    vm.aType = window.$gz.util.stringToIntOrNull(vm.$route.params.aType);
    //OPTIONAL FILTER
    if (vm.objectId && vm.objectId != 0 && vm.aType) {
      //OBJECTID,AYATYPE
      vm.clientCriteria = `${vm.objectId},${vm.aType}`;
      vm.preFilterMode = {
        icon: window.$gz.util.iconForType(vm.aType),
        id: vm.objectId,
        ayatype: vm.aType,
        viz: vm.$route.params.name,
        clearable: true
      };
    }
    generateMenu(vm);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      selectedItems: [],
      reload: false,
      clientCriteria: undefined,
      preFilterMode: null,
      objectId: null,
      aType: null,
      name: null
    };
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    },
    clearPreFilter() {
      this.clientCriteria = null;
      this.preFilterMode = null;
      this.reload = !this.reload;
    }
  }
};

/////////////////////////////
//
//
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: "review-edit",
          params: {
            recordid: 0,
            aType: m.vm.aType,
            objectId: m.vm.objectId,
            name: m.vm.name
          }
        });
        break;
      case "extensions":
        const res = await m.vm.$refs.extensions.open(
          m.vm.$refs.gzdatatable.getDataListSelection(window.$gz.type.Review)
        );
        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.Review),
            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: true,
    icon: "$ayiCalendarCheck",
    title: "ReviewList",
    helpUrl: "home-reviews",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.Review
    }
  };

  if (vm.rights.change) {
    if (vm.objectId && vm.aType) {
      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>
