<template>
  <nav class="bg-neutral-700">
    <div class="mx-auto px-4" :class="[showNavbarBreadcrumb? 'min-h-12': 'h-12']">
      <div class="flex justify-between items-center flex-wrap" v-show="showNavbarBreadcrumb">
        <div class="flex flex-1 items-center">
          <!-- 回 Files 頁 -->
          <router-link to class="mr-4" v-if="navViewName !== 'FILES'">
            <span @click="$router.back(-1)" class="material-symbols-outlined text-neutral-10 leading-loose">
              arrow_back
            </span>
          </router-link>
          <h2 class="mr-14 py-3 text-2xl font-black text-neutral-10">{{ navViewName }}</h2>
          <ul class="flex justify-center items-center space-x-4 text-xl font-semibold text-neutral-300 cursor-pointer">
            <li @click="switchNavItem($event)" v-for="(item, index) in navViewData[navViewName]" :key="index">{{ item }}</li>
          </ul>
        </div>
        <!-- Files Page: Search and Upload -->
        <div class="flex justify-end items-center" v-if="navViewName === 'FILES'">
          <form role="search">
            <label for="searchFiles" class="mr-4 relative">
              <input type="search" id="searchFiles" placeholder="Search" class="px-5 py-2 w-72 rounded-full text-sm align-middle duration-300 border border-neutral-500 hover:border-neutral-300 focus:outline-none focus:ring focus:border-neutral-300">
              <span class="absolute top-2 bottom-1.5 right-0.5 flex justify-center items-center gap-2">
                <IconSetting class="w-6 h-6 cursor-pointer"></IconSetting>
                <span class="w-px h-6 block after:border after:border-neutral-300 after:content-['']"></span>
                <button class="pr-2">
                  <IconSearch class="w-6 h-6"></IconSearch>
                </button>
              </span>
            </label>
          </form>
          <label class="btn btn-sm btn-neutral cursor-pointer">
            <input id="uploadFiles" class="hidden" type="file">
            upload
          </label>
        </div>
        <!-- Other Page: Save and Download -->
        <!-- Save 有 data 跳重新命名，沒有 data 跳要不要儲存，沒有動都不跳 -->
        <div v-else class="space-x-4">
          <button class="btn btn-sm" :class="[ disabledSave ? 'btn-disable' : 'btn-neutral']" :disabled="disabledSave" @click="saveModal">Save</button>
          <button class="btn btn-sm btn-neutral">Download</button>
        </div>
      </div>
    </div>
  </nav>
</template>

<script>
import { storeToRefs } from 'pinia';
import filesStore from '@/stores/files.js';
import AllMapDataStore from '@/stores/allMapData.js';
import IconSearch from '@/components/icons/IconSearch.vue';
import IconSetting from '@/components/icons/IconSetting.vue';
import { saveFilter, savedSuccessfully } from '@/module/alertModal.js';

export default {
  data() {
    return {
      showNavbarBreadcrumb: false,
      navViewData:
      {
        FILES: ['ALL', 'DISCOVER', 'COMPARE', 'DESIGN'],
        DISCOVER: ['MAP', 'CONFORMANCE', 'PERFORMANCE', 'DATA']
      },
      navViewName: 'FILES',
    };
  },
  computed: {
    // 沒有 filter Id, 沒有暫存 tempFilterId Id 就不能存檔
    disabledSave: function () {
      return this.tempFilterId ? false : true;
    }
  },
  watch: {
    '$route':'getNavViewName',
    filterName: function(newVal, oldVal) {
      this.filterName = newVal;
    },
  },
  setup() {
    const store = filesStore();
    const allMapDataStore = AllMapDataStore();
    const { logId, tempFilterId, createFilterId, filterName, postRuleData, isUpdataFilter } = storeToRefs(allMapDataStore);

    return { store, allMapDataStore, logId, tempFilterId, createFilterId, filterName, postRuleData, isUpdataFilter}
  },
  components: {
    IconSearch,
    IconSetting,
    saveFilter,
    savedSuccessfully
  },
  mounted() {
    let filterId = this.$route.params.filterId;
    this.createfilterId = filterId;
    this.showNavbarBreadcrumb = this.$route.matched[0].name !== ('AuthContainer')? true : false;
    this.getNavViewName();
  },
  methods: {
    /**
     * switch navbar item
     */
    switchNavItem(event) {
      this.store.filesTag = event.target.innerText;
    },
    getNavViewName() {
      this.navViewName = this.$route.name.toUpperCase();
      if(this.navViewName === 'DISCOVERLOG' || this.navViewName === 'DISCOVERFILTER') this.navViewName = 'DISCOVER';

    },
    /**
     * Save button' modal
     */
    async saveModal() {
      // 先判斷有沒有 filter Id，有就儲存 return，沒有就往下走
      // 沒有 filter Id, 有暫存 tempFilterId Id 可以另存新檔
      if(this.createFilterId) {
        await this.allMapDataStore.updataFilter();
        if(this.isUpdataFilter) await savedSuccessfully(this.filterName);
        this.tempFilterId = null;
      }else if(this.logId){
        saveFilter(this.allMapDataStore.addFilterId);
      };
    }
  },
}
</script>

<style scoped>
#searchFiles::-webkit-search-cancel-button{
  appearance: none;
}
</style>
