<template lang="html">
  <div id="user-flags">
    <h2
      class="mdi mdi-folder-outline"
    >{{ $t("materio.My folders") }} ({{collsLength}})</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 v-if="collsLength<15" class="create-flag">
        <input
          placeholder="new folder"
          v-model="new_folder_name"
          @keyup.enter.prevent.stop="onCreateFlagColl"
        />
        <span
          class="add-btn mdi"
          :class="addFlagBtnClassObj"
          @click.prevent.stop="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
    }),
    collsLength() {
      return Object.keys(this.flagcolls).length
    },
    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 onOpenFlagColl", flagcollid);
      this.openFlagColl(flagcollid)
        .then(() => {
          // console.log("onDeleteFlagColl then", data);
        })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
