<template>
  <div class="h-full w-full flex justify-between items-center">
    <!-- Activity List -->
    <div class="h-full w-full bg-neutral-10 border border-neutral-300 rounded-xl ml-4 p-4 space-y-2">
      <p class="h2 pl-2 border-b mb-3">Activity list</p>
      <div class="h-[calc(100%_-_56px)]">
        <Draggable :list="datadata" :group="{name: 'activity', pull: 'clone' }" itemKey="name" animation="300" :fallbackTolerance="5" :forceFallback="true" :ghostClass="'ghostSelected'" :dragClass="'dragSelected'" :sort="false" @end="onEnd" class="h-full flex flex-wrap justify-start content-start gap-4 px-2 overflow-y-auto scrollbar">
          <template #item="{ element, index }">
            <div :class="listSequence.includes(element) ? 'border-primary text-primary' : ''" class="flex items-center w-[166px] border rounded p-2 bg-neutral-10 cursor-pointer hover:bg-primary/20" @dblclick="moveActItem(index, element)" :title="element">
              <span class="whitespace-nowrap break-keep text-ellipsis overflow-hidden">{{ element }}</span>
            </div>
          </template>
        </Draggable>
      </div>
    </div>
    <!-- sequence -->
    <div class="w-full h-full relative bg-neutral-10 border border-neutral-300 rounded-xl ml-4 p-4 space-y-2 text-sm">
      <p class="h2 border-b border-500 mb-3">Sequence</p>
      <!-- No Data -->
      <div v-if="listSequence && listSequence.length === 0" class="p-4 w-[calc(100%_-_32px)] h-5/6 flex justify-center items-center absolute">
        <p class="text-neutral-500">Please drag and drop at least two activities here and sort.</p>
      </div>
      <!-- Have Data -->
      <div class="m-auto w-full  h-[calc(100%_-_56px)]">
        <div class="w-full h-full overflow-y-auto overflow-x-auto scrollbar px-4 text-center">
          <draggable class="h-full" :group="{name: 'activity'}" :list="listSequence" itemKey="name" animation="300" :forceFallback="true" :dragClass="'dragSelected'" :fallbackTolerance="5" @start="onStart" @end="onEnd" :component-data="getComponentData()">
            <template #item="{ element, index }">
              <div :title="element">
                <div class="flex justify-center items-center">
                  <div class="w-full p-2 border rounded bg-neutral-10  cursor-pointer hover:bg-primary/20" @dblclick="moveSeqItem(index, element)">
                    <span>{{ element }}</span>
                  </div>
                  <span class="material-symbols-outlined pl-1 cursor-pointer duration-300 hover:text-danger" @click.stop.native="moveSeqItem(index, element)">close</span>
                </div>
                <span v-show="index !== listSequence.length - 1 && index !== lastItemIndex - 1" class="pi pi-chevron-down !text-lg inline-block py-2 pr-7"></span>
              </div>
            </template>
          </draggable>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
import { sortNumEngZhtw } from '@/module/sortNumEngZhtw.js';

export default {
  props: ['data', 'listSeq', 'isSubmit', 'category'],
  data() {
    return {
      listSequence: [],
      lastItemIndex: null,
      isSelect: true
    }
  },
  computed: {
    datadata: function() {
      // TODO Activity List 的 dblclick, drag & drop 要改假刪除
      // Activity List 要排序
      let newData;
      if(this.data !== null) {
        newData = JSON.parse(JSON.stringify(this.data));
        sortNumEngZhtw(newData);
      }
      return newData;
    },
  },
  methods: {
    /**
     * double click Activity List
     * @param {number} index data item index
     * @param {object} element data item
     */
    moveActItem(index, element){
      this.listSequence.push(element);
    },
    /**
     * double click Sequence List
     * @param {number} index data item index
     * @param {object} element data item
     */
    moveSeqItem(index, element){
      this.listSequence.splice(index, 1);
    },
    /**
     * get listSequence
     */
    getComponentData(){
      this.$emitter.emit('getListSequence',{
        category: this.category,
        task: this.listSequence,
      });
    },
    /**
     * Element dragging started
     */
    onStart(evt) {
      const lastChild = evt.to.lastChild.lastChild;
      lastChild.style.display = 'none';
      // 隱藏拖曳元素原位置
      const originalElement = evt.item;
      originalElement.style.display = 'none';
      // 拖曳最後一個元素時，倒數第二的元素的箭頭要隱藏
      const listIndex = this.listSequence.length - 1;
      if(evt.oldIndex === listIndex) this.lastItemIndex  = listIndex;
    },
    /**
     * Element dragging ended
     */
    onEnd(evt) {
      // 顯示拖曳元素
      const originalElement = evt.item;
      originalElement.style.display = '';
      // 拖曳結束要顯示箭頭，但最後一個不用
      const lastChild = evt.item.lastChild;
      const listIndex = this.listSequence.length - 1
      evt.oldIndex !== listIndex ? lastChild.style.display = '' : null;
      // reset: 拖曳最後一個元素時，倒數第二的元素的箭頭要隱藏
      this.lastItemIndex = null;
    },
  },
  created() {
    let newlist = JSON.parse(JSON.stringify(this.listSeq));
    this.isSubmit ? this.listSequence = newlist : this.listSequence = [];
    this.$emitter.on('reset', (data) => {
      this.listSequence = [];
    });
  },
}
</script>
<style scoped>
.ghostSelected {
  @apply bg-primary/20
}
.dragSelected {
  @apply !opacity-100
}
</style>
