<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      :ayaType="ayType"
      :selectedItems="selectedItems"
      ref="extensions"
    >
    </gz-extensions>
    {{ customername }}
    <gz-data-table
      ref="gzdatatable"
      formKey="cust-customer-notes"
      :dataListKey="dataListKey"
      :dataListFilter="dataListFilter"
      :dataListSort="dataListSort"
      :showSelect="rights.change"
      :metaView="metaView"
      v-on:selection-change="handleSelected"
      data-cy="customerNotesTable"
    >
    </gz-data-table>
  </div>
</template>

<script>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*  Xeslint-disable    */
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

const FORM_KEY = "cust-customer-notes";
//"[{\"fld\":\"widgetname\",\"filter\":{\"items\":[{\"op\":\"%-\",\"value\":\"Awesome\"}]}},{\"fld\":\"widgetserial\"},{\"fld\":\"widgetdollaramount\"},{\"fld\":\"widgetusertype\",\"filter\":{\"items\":[{\"op\":\"=\",\"value\":1}]}},{\"fld\":\"widgetstartdate\"},{\"fld\":\"widgetactive\"},{\"fld\":\"username\",\"filter\":{\"items\":[{\"op\":\"=\",\"value\":\"aya\"}]}},{\"fld\":\"widgettags\"},{\"fld\":\"widgetcustom1\"},{\"fld\":\"widgetcustom2\"}]"
export default {
  created() {
    this.customername = this.$route.params.customername;
    this.customerId = this.$route.params.customerid;
    this.metaView = JSON.stringify([
      {
        fld: "metacustomer",
        filter: { items: [{ op: "=", value: this.customerId }] }
      }
    ]);

    this.rights = window.$gz.role.getRights(window.$gz.type.Customer);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      customerId: undefined,
      customername: undefined,
      currentListViewId: 1,
      dataListKey: "CustomerNoteDataList",
      dataListFilter: "",
      dataListSort: "",
      metaView: undefined,
      rights: window.$gz.role.defaultRightsObject(),
      ayType: window.$gz.type.CustomerNote,
      selectedItems: []
    };
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    }
  }
};
//"[{\"fld\": \"widgetname\",\"filter\": {\"any\":false,\"items\": [{\"op\": \"%-\",\"value\": \"Awesome\"}]}},{\"fld\":\"widgetserial\"},{\"fld\":\"widgetdollaramount\"},{\"fld\":\"widgetusertype\"},{\"fld\":\"widgetstartdate\"},{\"fld\":\"widgetactive\"},{\"fld\":\"username\"},{\"fld\":\"widgettags\"},{\"fld\":\"widgetcustom1\"},{\"fld\":\"widgetcustom2\"}]"
/////////////////////////////
//
//
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: "customer-note-edit",
          params: {
            recordid: 0,
            customerid: m.vm.customerId,
            customername: m.vm.customername
          }
        });
        break;
      case "extensions":
        let res = await m.vm.$refs.extensions.open(
          m.vm.$refs.gzdatatable.getDataListSelection(
            window.$gz.type.CustomerNote
          )
        );
        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.CustomerNote }
          });
        } else {
          //general report selector chosen

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

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

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

//////////////////////
//
//
function generateMenu(vm) {
  let menuOptions = {
    isMain: true,
    icon: "$ayiClipboard",
    title: "CustomerNoteList",
    helpUrl: "form-ay-data-list-view",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.CustomerNote
    }
  };

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