<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>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*  Xeslint-disable    */
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

const FORM_KEY = "meter-reading-list";
export default {
  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);
  },
  data() {
    return {
      unitId: undefined,
      workOrderItemUnitId: undefined,
      // unitname: undefined,
      clientCriteria: undefined,
      preFilterMode: null,
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.UnitMeterReading,
      reload: false
    };
  },
  methods: {
    ayaTypes: function() {
      return window.$gz.type;
    }
  }
};

/////////////////////////////
//
//
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: "meter-reading",
          params: {
            recordid: 0,
            unitid: m.vm.unitId,
            workorderitemunitid: m.vm.workOrderItemUnitId
          }
        });
        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.UnitMeterReading
            }
          });
        } else {
          //general report selector chosen

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

          //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.UnitMeterReading
            }
          });
        }
        break;

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

//////////////////////
//
//
function generateMenu(vm) {
  let 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
    });
  }

  //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
    });
  }

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