<template>
  <section 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)"
      />
      <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(["proposeProtein"]),

    proposeProteinUpload() {
      if (this.customPDBFile != null) {
        this.proposeProtein({
          proteinInfo: {
            method: "upload",
            name: this.customPDBFile.name(true),
            file: this.customPDBFile.file,
            chain: this.chain ?? "A",
          },
          role: this.role,
        });
      }
    },
  },
  watch: {
    customPDBFile: function () {
      this.proposeProteinUpload();
    },
    chain: function () {
      this.proposeProteinUpload();
    },
  },
};
</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>