<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>

    <gz-data-table
      ref="gzdatatable"
      form-key="meter-reading-list"
      data-list-key="UnitMeterReadingDataList"
      :show-select="false"
      :reload="reload"
      :client-criteria="clientCriteria"
      data-cy="MeterReadingsTable"
      :pre-filter-mode="preFilterMode"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "meter-reading-list";
export default {
  data() {
    return {
      unitId: undefined,
      workOrderItemUnitId: undefined,
      clientCriteria: undefined,
      preFilterMode: null,
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.UnitMeterReading,
      reload: false
    };
  },
  created() {
    this.unitId = parseInt(this.$route.params.unitid);
    this.workOrderItemUnitId = parseInt(this.$route.params.workorderitemunitid);
    //REQUIRED NON-OPTIONAL FILTER
    this.clientCriteria = this.unitId.toString();
    this.preFilterMode = {
      icon: "$ayiFan",
      id: window.$gz.util.stringToIntOrNull(this.$route.params.unitid),
      ayatype: window.$gz.type.Unit,
      viz: this.$route.params.unitname,
      clearable: false
    };
    this.rights = window.$gz.role.getRights(window.$gz.type.Unit);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  methods: {
    ayaTypes: function() {
      return window.$gz.type;
    }
  }
};

/////////////////////////////
//
//
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: "meter-reading",
          params: {
            recordid: 0,
            unitid: m.vm.unitId,
            workorderitemunitid: m.vm.workOrderItemUnitId
          }
        });
        break;
      case "report":
        {
          const res = await m.vm.$refs.reportSelector.open(
            m.vm.$refs.gzdatatable.getDataListSelection(
              window.$gz.type.UnitMeterReading
            ),
            m.id
          );
          if (res == null) {
            return;
          }
          window.$gz.form.setLastReportMenuItem(FORM_KEY, res, m.vm);
        }
        break;
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: false,
    readOnly: !vm.rights.change,
    icon: "$ayiWeight",
    title: "UnitMeterReadingList",
    helpUrl: "svc-meter-readings",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.UnitMeterReading
    }
  };

  if (vm.rights.change) {
    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
    });
  }
  window.$gz.eventBus.$emit("menu-change", menuOptions);
}
</script>
