<template>
  <div>
    <gz-report-selector ref="reportSelector"></gz-report-selector>
    <gz-extensions
      :aya-type="aType"
      :selected-items="selectedItems"
      ref="extensions"
    >
    </gz-extensions>
    <gz-data-table
      ref="gzdatatable"
      form-key="quote-item-list"
      data-list-key="QuoteItemDataList"
      :show-select="rights.read"
      :reload="reload"
      @selection-change="handleSelected"
      data-cy="quoteItemsTable"
    >
    </gz-data-table>
  </div>
</template>
<script>
const FORM_KEY = "quote-list";
export default {
  created() {
    this.rights = window.$gz.role.getRights(window.$gz.type.Quote);
    window.$gz.eventBus.$on("menu-click", clickHandler);
    generateMenu(this);
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      rights: window.$gz.role.defaultRightsObject(),
      aType: window.$gz.type.QuoteItem,
      selectedItems: [],
      reload: false
    };
  },
  methods: {
    handleSelected(selected) {
      this.selectedItems = selected;
    }
  }
};

/////////////////////////////
//
//
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: "quote-edit",
          params: { recordid: 0 }
        });
        break;
      case "extensions":
        const res = await m.vm.$refs.extensions.open(
          m.vm.$refs.gzdatatable.getDataListSelection(window.$gz.type.QuoteItem)
        );
        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.QuoteItem
            ),
            m.id
          );
          if (res == null) {
            return;
          }
          window.$gz.form.setLastReport(FORM_KEY, res);
          generateMenu(m.vm);
        }
        break;
      case "QuoteList":
        m.vm.$router.push({
          name: "svc-quotes"
        });
        break;

      case "WorkOrderItemUnitList":
        m.vm.$router.push({
          name: "svc-quote-item-units"
        });
        break;

      case "WorkOrderItemScheduledList":
        m.vm.$router.push({
          name: "svc-quote-item-scheduled-users"
        });
        break;

      case "WorkOrderItemTasks":
        m.vm.$router.push({
          name: "svc-quote-item-tasks"
        });
        break;

      case "WorkOrderItemPartList":
        m.vm.$router.push({
          name: "svc-quote-item-parts"
        });
        break;

      case "WorkOrderItemLaborList":
        m.vm.$router.push({
          name: "svc-quote-item-labors"
        });
        break;
      case "WorkOrderItemTravelList":
        m.vm.$router.push({
          name: "svc-quote-item-travels"
        });
        break;
      case "WorkOrderItemExpenseList":
        m.vm.$router.push({
          name: "svc-quote-item-expenses"
        });
        break;
      case "WorkOrderItemLoanList":
        m.vm.$router.push({
          name: "svc-quote-item-loans"
        });
        break;
      case "WorkOrderItemOutsideServiceList":
        m.vm.$router.push({
          name: "svc-quote-item-outside-services"
        });
        break;

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

//////////////////////
//
//
function generateMenu(vm) {
  const menuOptions = {
    isMain: true,
    icon: "$ayiWrench",
    title: "WorkOrderItemList",
    helpUrl: "svc-quotes",
    menuItems: [],
    formData: {
      ayaType: window.$gz.type.QuoteItem
    }
  };

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

  menuOptions.menuItems.push({
    title: "Extensions",
    icon: "$ayiPuzzlePiece",
    key: FORM_KEY + ":extensions",
    vm: vm
  });

  menuOptions.menuItems.push({ divider: true, inset: false });

  menuOptions.menuItems.push({
    title: "QuoteList",
    icon: "$ayiPencilAlt",
    key: FORM_KEY + ":QuoteList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemUnitList",
    icon: "$ayiFan",
    key: FORM_KEY + ":WorkOrderItemUnitList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemScheduledUserList",
    icon: "$ayiUserClock",
    key: FORM_KEY + ":WorkOrderItemScheduledList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemTasks",
    icon: "$ayiTasks",
    key: FORM_KEY + ":WorkOrderItemTasks",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemPartList",
    icon: "$ayiBoxes",
    key: FORM_KEY + ":WorkOrderItemPartList",
    vm: vm
  });

  menuOptions.menuItems.push({
    title: "WorkOrderItemLaborList",
    icon: "$ayiHammer",
    key: FORM_KEY + ":WorkOrderItemLaborList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemTravelList",
    icon: "$ayiTruckMonster",
    key: FORM_KEY + ":WorkOrderItemTravelList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemExpenseList",
    icon: "$ayiMoneyBillWave",
    key: FORM_KEY + ":WorkOrderItemExpenseList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemLoanList",
    icon: "$ayiPlug",
    key: FORM_KEY + ":WorkOrderItemLoanList",
    vm: vm
  });
  menuOptions.menuItems.push({
    title: "WorkOrderItemOutsideServiceList",
    icon: "$ayiLuggageCart",
    key: FORM_KEY + ":WorkOrderItemOutsideServiceList",
    vm: vm
  });

  menuOptions.menuItems.push({ divider: true, inset: false });

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