<template>
  <div class="loading" v-if="!material || loading">
    <span>Loading ...</span>
  </div>
  <article v-else class="card modal-card">
    <section class="col col-right">
      <header>
        <h1>{{ material.title }}</h1>
        <h4>{{ material.short_description }}</h4>
        <span class="ref">{{ material.reference }}</span>
      </header>
      <nav class="tools">
        <section class="tool close">
          <span
            class="btn mdi mdi-close"
            @click.prevent="onCloseModalCard"
          />
        </section>
        <section class="tool flags">
          <span class="btn mdi mdi-folder-outline"/>
          <div class="tool-content">
            <ul>
              <li  v-if="flagcolls" v-for="coll in flagcolls" :key="coll.id">
                <span
                  class="flag mdi"
                  :class="[
                    flagIsLoading(coll.id) ? 'mdi-loading mdi-spin' : flagIsActive(coll.id) ? 'mdi-close-circle isActive' : 'mdi-plus'
                  ]"
                  :collid="coll.id"
                  @click.prevent="onFlagActionCard"
                >
                  {{ coll.name }}
                </span>
              </li>
            </ul>
          </div>
        </section>
      </nav>
      <!-- <section class="samples">
        <h3>{{ $t("materio.Samples") }}</h3>
        <ul>
          <li
            v-for="sample in material.samples"
            :key="sample.showroom.id"
          >
            <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
          </li>
        </ul>
      </section> -->
      <section class="body" v-html="material.body"/>
      <section class="linked-materials">
        <LinkedMaterialCard v-for="m in material.linked_materials" v-bind:key="m.id" :item="m"/>
      </section>
    </section>
    <section class="col col-left images" v-switcher>
      <figure
        v-for="(img, index) in material.images"
        :key="img.url"
      >
        <img
          class="lazy"
          v-lazy="index"
          :data-src="img.style_cardfull.url"
          :title="img.title"
        />
        <img
          class="blank"
          :src="blanksrc"
          @click="lightbox_index = index"
        >
      </figure>
    </section>
    <CoolLightBox
      :items="material.images"
      :index="lightbox_index"
      srcName="url"
      :loop="true"
      @close="lightbox_index = null">
    </CoolLightBox>
  </article>
</template>

<script>
import { mapState, mapActions } from 'vuex'

import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'

import cardMixins from 'vuejs/components/cardMixins'

import { MGQ } from 'vuejs/api/graphql-axios'
import { print } from 'graphql/language/printer'
import gql from 'graphql-tag'
import materiauFields from 'vuejs/api/gql/materiaumodal.fragment.gql'

export default {
  name: "ModalCard",
  props: ['item'],
  mixins: [cardMixins],
  components: {
    LinkedMaterialCard
  },
  data() {
    return {
      material: null,
      loading: false,
      blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
      loadingFlag: false,
      lightbox_index: null
    }
  },
  computed: {
    ...mapState({
      flagcolls: state => state.User.flagcolls,
      showrooms: state => state.Showrooms.showrooms_by_tid
    })
  },
  created () {
    console.log('modale item', this.item)
    this.loadMaterial()
  },
  methods: {
    ...mapActions({
      flagUnflag: 'User/flagUnflag'
    }),
    loadMaterial(){
      console.log('loadMaterial', this.item.id)
      this.loading = true
      let ast = gql`{
        materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
          ...MateriauFields
        }
      }
      ${ materiauFields }
      `
      MGQ.post('', { query: print(ast)
      })
        .then(({ data:{data:{materiau}}}) => {
          console.log('loadMaterial material loaded', materiau )
          this.material = materiau
          this.loading = false
          // delay the lazyload to let the card the time to update dom
          // maybe not the best method
          setTimeout(function () {
            this.activateLazyLoad()
          }.bind(this), 5)
        })
        .catch(error => {
          console.warn('Issue with loadMaterial', error)
          Promise.reject(error)
        })
    },
    flagIsActive(collid) {
      // console.log(this.item.uuid);
      // console.log(this.flagcolls[collid].items_uuids);
      // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
      return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
    },
    flagIsLoading(collid) {
      // console.log(this.item.uuid);
      // console.log(this.flagcolls[collid].items_uuids);
      return collid === this.loadingFlag;
    },
    onFlagActionCard (e) {
      console.log("Card onFlagActionCard", e);
      if (!this.loadingFlag) {
        let collid = e.target.getAttribute('collid');
        let isActive = this.flagIsActive(collid);
        let action = isActive ? 'unflag' : 'flag';
        // console.log('collid', collid);
        // console.log("this.item", this.item);
        this.loadingFlag = collid;
        this.flagUnflag({ action: action, id: this.item.id, collid: collid})
        .then(data => {
          console.log("onFlagActionCard then", data);
          this.loadingFlag = false;
        })
      }
    },
    onCloseModalCard (e) {
      this.$modal.hideAll()
    }
  }
}

</script>

<style lang="scss" scoped>

</style>
