<template>
  <div class="q-gutter-y-sm">
    <template v-if="xsl">
      <div class="row items-center justify-between">
        <base-label-value class="items-center col-11">
          <template v-slot:label>{{ $t('interagencies.xslScript') }}</template>
          <template v-slot:value>{{ xsl.fileName }}</template>
        </base-label-value>
        <q-btn
          class="col-1"
          unelevated
          text-color="primary"
          icon="arrow_downward"
          @click="download"
          :loading="downloadLoading"
        >
          <q-tooltip :delay="500">{{ $t('interagencies.download') }}</q-tooltip>
        </q-btn>
      </div>
      <!--  <div class="q-gutter-x-sm">
        <q-btn
          @click="() => (isCodeShow = !isCodeShow)"
          :icon="isCodeShow ? 'close' : 'visibility'"
          unelevated
          :label="isCodeShow ? $t('base.close') : $t('interagencies.see')"
          color="primary"
        /> 
      </div>
      <div>
        <codemirror v-if="isCodeShow" :value="value" :options="cmOptions" />
      </div> -->
    </template>

    <div v-else>xsl-сценарий отсутствует</div>
  </div>
</template>

<script lang="ts">
/*
// @ts-nocheck
import { codemirror } from 'vue-codemirror';
import 'codemirror/mode/xml/xml.js';
import 'codemirror/lib/codemirror.css';
import 'codemirror/addon/selection/active-line.js'; */
import { Component, Vue, Prop } from 'vue-property-decorator';
import { notifyError } from '@/utils/notify';
import { AttachmentApi } from '@openapi/registry-service';
import { exportFile } from 'quasar';

const attachmentApi = new AttachmentApi();

@Component({
  components: {
    /*   codemirror, */
  },
})
export default class XslScript extends Vue {
  @Prop() xsl: any;
  @Prop() interagencyId: string;

  downloadLoading = false;

  /* isCodeShow = false;
  cmOptions = {
    tabSize: 4,
    mode: 'text/html',
    styleActiveLine: true,
    lineNumbers: true,
    line: true,
    readOnly: true,
  }; */

  async download() {
    try {
      this.downloadLoading = true;
      const fileName = this.xsl.fileName;
      const file = await attachmentApi.getDocumentFile(
        'Interagency',
        this.interagencyId,
        fileName
      );
      const blob = new Blob([file.data as Blob]);
      exportFile(fileName, blob, 'text/xml');
    } catch (e) {
      if (e instanceof Error) {
        notifyError(e.message);
      }
    } finally {
      this.downloadLoading = false;
    }
  }
}
</script>

<style scoped></style>