<template lang="html">
  <div id="user-flags">
    <h2
      class="mdi mdi-folder-outline"
    >My folders</h2>
    <ul>
      <li v-if="flagcolls" v-for="coll in flagcolls" :key="coll.id">
        <h5
          :flagcollid="coll.id"
          @click.prevent="onOpenFlagColl"
        >{{ coll.name }} ({{ coll.items.length }})</h5>
        <div class="actions">
          <span
            class="delete-btn mdi"
            :class="flagDeletingClassObj"
            :flagcollid="coll.id"
            @click.prevent="onDeleteFlagColl"
          />
        </div>
      </li>
      <li class="create-flag">
        <input
          placeholder="new folder"
          v-model="new_folder_name"
        />
        <span
          class="add-btn mdi"
          :class="addFlagBtnClassObj"
          @click.prevent="onCreateFlagColl"
        />
      </li>
    </ul>
  </div>
</template>

<script>

import { mapState, mapActions } from 'vuex'

export default {
  name: "userFlags",
  data: () => ({
    new_folder_name: "",
    is_creating_folder: false,
    is_deleting_folder: false
  }),
  computed: {
    ...mapState({
      flagcolls: state => state.User.flagcolls
    }),
    addFlagBtnClassObj() {
      return {
        'mdi-plus-circle-outline': !this.is_creating_folder,
        'mdi-loading': this.is_creating_folder,
        active: this.new_folder_name.length > 4 && !this.is_creating_folder,
        loading: this.is_creating_folder
      }
    },
    flagDeletingClassObj() {
      return {
        'mdi-trash-can-outline': !this.is_deleting_folder,
        'mdi-loading': this.is_deleting_folder,
        'loading': this.is_deleting_folder
      }
    }
  },
  methods: {
    ...mapActions({
      createFlagColl: 'User/createFlagColl',
      deleteFlagColl: 'User/deleteFlagColl',
      openFlagColl: 'User/openFlagColl'
    }),
    onCreateFlagColl () {
      console.log("UserFlags onCreateFlagColl", this.new_folder_name)
      this.is_creating_folder = true;
      this.createFlagColl(this.new_folder_name)
        .then(data => {
          console.log("onCreateFlagColl then", data);
          this.new_folder_name = "";
          this.is_creating_folder = false;
        })
    },
    onDeleteFlagColl (e) {
      const flagcollid = e.target.getAttribute('flagcollid');
      console.log("UserFlags onDeleteFlagColl", flagcollid);
      this.is_deleting_folder = flagcollid;
      this.deleteFlagColl(flagcollid)
        .then(() => {
          // console.log("onDeleteFlagColl then", data);
          this.is_deleting_folder = false;
        })
    },
    onOpenFlagColl (e) {
      const flagcollid = e.target.getAttribute('flagcollid');
      console.log("UserFlags onDeleteFlagColl", flagcollid);
      this.openFlagColl(flagcollid)
        .then(() => {
          // console.log("onDeleteFlagColl then", data);
        })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
