<template>
  <v-expansion-panel v-if="available()">
    <v-expansion-panel-header
      disable-icon-rotate
      expand-icon="$sockiFileDownload"
      >{{ $sock.t("Export") }}</v-expansion-panel-header
    >
    <v-expansion-panel-content>
      <v-btn icon @click="goHelp()"
        ><v-icon>$sockiQuestionCircle</v-icon></v-btn
      >
      <v-btn color="blue darken-1" text @click="doAction()">{{
        $sock.t("Export")
      }}</v-btn>
    </v-expansion-panel-content>
  </v-expansion-panel>
</template>
<script>
export default {
  props: {
    dataListSelection: { type: Object, default: null }
  },
  data: () => ({
    jobActive: false
  }),
  methods: {
    available() {
      return (
        this.dataListSelection.SockType != 0 &&
        this.dataListSelection.SockType != window.$gz.type.PartInventoryRestock
      );
    },
    goHelp() {
      window.open(window.$gz.api.helpUrl() + "sock-ex-export", "_blank");
    },

    async doAction() {
      try {
        const res = await window.$gz.api.upsert(
          `export/render`,
          this.dataListSelection
        );
        if (res.error) {
          throw new Error(window.$gz.errorHandler.errorToString(res, this));
        }

        const href = window.$gz.api.genericDownloadUrl(
          "export/download/" + res.data
        );

        if (window.open(href, "DownloadExport") == null) {
          throw new Error(
            "Unable to download, your browser rejected navigating to download url."
          );
        }
      } catch (error) {
        window.$gz.errorHandler.handleFormError(error, this);
        window.$gz.eventBus.$emit("notify-error", this.$sock.t("JobFailed"));
      }
    }
  }
};
</script>
