<template lang="html">
  <div id="user-flags">
    <h2
      class="mdi mdi-folder-outline"
    >My folders</h2>
    <ul>
      <li  v-if="flags" v-for="flag in flags" :key="flag.id">
        <h5>{{ flag.name }}</h5>
        <div class="actions">
          <span
            class="mdi mdi-trash-can-outline"
            :flagid="flag.id"
            @click.prevent="onDeleteFlag"
          />
        </div>
      </li>
      <li ref="create-flag">
        <input
          placeholder="new folder"
          v-model="new_folder_name"
        />
        <span

          class="mdi mdi-plus-box"
          v-if="new_folder_name && !is_creating_folder"
          @click.prevent="onCreateFlag"
        />
      </li>
    </ul>
  </div>
</template>

<script>

import { mapState, mapActions } from 'vuex'

export default {
  name: "userFlags",
  data: () => ({
    new_folder_name: "",
    is_creating_folder: false
  }),
  computed: {
    ...mapState({
      flags: state => state.User.flags
    })
  },
  methods: {
    ...mapActions({
      createFlag: 'User/createFlag',
      deleteFlag: 'User/deleteFlag'
    }),
    onCreateFlag () {
      console.log("UserFlags onCreateFlag", this.new_folder_name)
      this.createFlag(this.new_folder_name)
        .then(data => {
          console.log("onCreateFlag then", data);
          this.new_folder_name = "";
        })
    },
    onDeleteFlag (e) {
      let flagid = e.target.getAttribute('flagid');
      console.log("UserFlags onDeleteFlag", flagid);
      this.deleteFlag(flagid)
        .then(data => {
          console.log("onDeleteFlag then", data);
          this.new_folder_name = "";
        })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
