<template>
  <modal v-if="show" @close="hideModal">
    <div slot="header">
      <ul>
        <li>
          <div class="flex-wrap">
            <h3 class="title">Edit Request</h3>
            <div>
              <button class="icon" @click="hideModal">
                <i class="material-icons">close</i>
              </button>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div slot="body">
      <ul>
        <li>
          <label for="selectLabel">Label</label>
          <input
            type="text"
            id="selectLabel"
            v-model="requestUpdateData.name"
            @keyup.enter="saveRequest"
            :placeholder="request.name"
          />
          <label for="selectCollection">Collection</label>
          <select
            type="text"
            id="selectCollection"
            v-model="requestUpdateData.collectionIndex"
          >
            <option :key="undefined" :value="undefined" hidden disabled selected
              >Current Collection</option
            >
            <option
              v-for="(collection, index) in $store.state.postwoman.collections"
              :key="index"
              :value="index"
              >{{ collection.name }}</option
            >
          </select>
          <label for="selectFolder">Folder</label>
          <select
            type="text"
            id="selectFolder"
            v-model="requestUpdateData.folderIndex"
          >
            <option :key="undefined" :value="undefined">/</option>
            <option
              v-for="(folder, index) in folders"
              :key="index"
              :value="index"
              >{{ folder.name }}</option
            >
          </select>
        </li>
      </ul>
    </div>
    <div slot="footer">
      <ul>
        <li>
          <button class="icon" @click="saveRequest">
            <i class="material-icons">save</i>
            <span>Save</span>
          </button>
        </li>
      </ul>
    </div>
  </modal>
</template>

<script>
export default {
  props: {
    show: Boolean,
    collectionIndex: Number,
    folderIndex: Number,
    request: Object,
    requestIndex: Number
  },
  components: {
    modal: () => import("../../components/modal")
  },
  data() {
    return {
      requestUpdateData: {
        name: undefined,
        collectionIndex: undefined,
        folderIndex: undefined
      }
    };
  },
  watch: {
    "requestUpdateData.collectionIndex":function resetFolderIndex() { //resetFolderIndex Function
      // if user choosen some folder, than selected other collection, which doesn't have any folders
      // than `requestUpdateData.folderIndex` won't be reseted
      this.$data.requestUpdateData.folderIndex = undefined;
    }
  },
  computed: {
    folders() {
      const userSelectedAnyCollection =
        this.$data.requestUpdateData.collectionIndex !== undefined;
      if (!userSelectedAnyCollection) return [];

      return this.$store.state.postwoman.collections[
        this.$data.requestUpdateData.collectionIndex
      ].folders;
    }
  },
  methods: {
    saveRequest() {
      const userSelectedAnyCollection =
        this.$data.requestUpdateData.collectionIndex !== undefined;

      const requestUpdated = {
        ...this.$props.request,
        name: this.$data.requestUpdateData.name || this.$props.request.name,
        collection: userSelectedAnyCollection
          ? this.$data.requestUpdateData.collectionIndex
          : this.$props.collectionIndex,
        folder: this.$data.requestUpdateData.folderIndex
      };

      // pass data separately to don't depend on request's collection, folder fields
      // probably, they should be deprecated because they don't describe request itself
      this.$store.commit("postwoman/editRequest", {
        requestOldCollectionIndex: this.$props.collectionIndex,
        requestOldFolderIndex: this.$props.folderIndex,
        requestOldIndex: this.$props.requestIndex,
        requestNew: requestUpdated,
        requestNewCollectionIndex: requestUpdated.collection,
        requestNewFolderIndex: requestUpdated.folder
      });

      this.hideModal();
    },
    hideModal() {
      this.$emit("hide-modal");
    }
  }
};
</script>
