<template>
  <section @keyup.enter="uploadSelectedProtein" id="wannabe-pdbs-section">
    <div id="wannabepdbs" class="pdb-menu">
      <VueFileAgent
        class="pdb"
        :theme="'list'"
        :multiple="false"
        :deletable="true"
        helpText="Click or drag to choose custom B-factor files."
        v-model="customPDBFile"
      >
        <template v-slot:file-preview="slotProps">
          <div :key="slotProps.index" class>
            <strong>{{ slotProps.fileRecord.name() }}</strong>
            <span class="text-muted">({{ slotProps.fileRecord.size() }})</span>
          </div>
        </template>
        <template v-slot:file-preview-new>
          <div key="new">Click or drag & drop here</div>
        </template>
      </VueFileAgent>
      <input
        type="text"
        v-model="chain"
        class="chain"
        name="chain"
        placeholder="Chain (A)"
      />
      <LabelButton class="upload-button" @click="uploadSelectedProtein">
        Upload PDB
      </LabelButton>
      <CancelButton class="back-button" @click="$emit('cancel')" />
    </div>
  </section>
</template>

<script>
import VueFileAgentPlugin from "vue-file-agent";
import VueFileAgentStyles from "vue-file-agent/dist/vue-file-agent.css";
import LabelButton from "../PanelButtons/TextLabelButton";
import CancelButton from "../PanelButtons/CancelButton";
import { mapActions } from "vuex";

export default {
  props: ["role"],
  data() {
    return {
      chain: null,
      customPDBFile: null,
    };
  },
  components: {
    VueFileAgent: VueFileAgentPlugin.VueFileAgent,
    LabelButton,
    CancelButton,
  },
  methods: {
    ...mapActions(["uploadProtein"]),

    uploadSelectedProtein() {
      if (this.customPDBFile != null) {
        this.uploadProtein({
          name: this.customPDBFile.name(true),
          file: this.customPDBFile.file,
          chain: this.chain ?? "A",
          role: this.role,
        }).then(() => this.$emit("loaded", this.customPDBFile.name(true)));
      }
    },
  },
};
</script>

<style scoped>
input[type="text"] {
  padding: 10px;
  border: 1px solid grey;
  outline: 0;
}

.chain {
  flex: 1;
  width: 60px;
}

.pdb {
  flex: 100;
  display: flex;
  align-items: stretch;
  border: 1px grey solid;
  background: white;
  border-radius: 10px 0px 0px 10px;
}
.upload-button {
  border-radius: 0px 0px 0px 0px;

  height: auto;
}

.back-button {
  border-radius: 0px 10px 10px 0px;
  height: auto;
}
.pdb-menu {
  display: flex;
}
</style>