<template>
  <div>
    <h1>New notification count: {{ newNotificationCount() }}</h1>
    <v-list three-line>
      <v-list-item> </v-list-item>
    </v-list>
  </div>
</template>

<script>
const FORM_KEY = "notification-subscriptions";
export default {
  async created() {
    let vm = this;
    try {
      await initForm(vm);

      window.$gz.eventBus.$on("menu-click", clickHandler);
      generateMenu(vm, false);
      await vm.getDataFromApi();
    } catch (err) {
      window.$gz.errorHandler.handleFormError(err, vm);
    } finally {
      vm.formState.ready = true;
    }
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject()
    };
  },
  methods: {
    async getDataFromApi() {
      let vm = this;
     
     
      let url = API_BASE_URL + recordId;
      try {
        window.$gz.form.deleteAllErrorBoxErrors(vm);

        let res = await window.$gz.api.get(url);

        if (res.error) {
          //Not found?
          if (res.error.code == "2010") {
            //notify not found error then navigate backwards
            window.$gz.eventBus.$emit("notify-error", vm.$ay.t("ErrorAPI2010"));
            // navigate backwards
            window.$gz._.delay(function() {
              vm.$router.go(-1);
            }, 2000);
          }
          vm.formState.serverError = res.error;
          window.$gz.form.setErrorBoxErrors(vm);
        } else {
          vm.obj = res.data;
          //modify the menu as necessary
          generateMenu(vm);
          //Update the form status
          window.$gz.form.setFormState({
            vm: vm,
            dirty: false,
            valid: true,
            loading: false
          });
        }
      } catch (error) {
        window.$gz.errorHandler.handleFormError(error, vm);
      } finally {
        window.$gz.form.setFormState({
          vm: vm,
          loading: false
        });
      }
    }
  }
};

/////////////////////////////
//
//
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: "home-notify-subscription",
      //     params: { recordid: 0 }
      //   });
      //   break;
      // case "extensions":
      //   let res = await m.vm.$refs.extensions.open();
      //   break;

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

//////////////////////
//
//
function generateMenu(vm) {
  let menuOptions = {
    isMain: true,
    icon: "fa-bell",
    title: "Notifications",
    helpUrl: "form-home-notifications",
    menuItems: []
  };

  // if (vm.rights.change) {
  //   menuOptions.menuItems.push({
  //     title: "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: "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
  // });

  // menuOptions.menuItems.push({
  //   title: "Extensions",
  //   icon: "fa-puzzle-piece",
  //   key: FORM_KEY + ":extensions",
  //   vm: vm
  // });
  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
</script>
