<template>
   <NuxtLayout name="default" :menu="adminMenu">
  <div class="m-3">
    <the-content-loading v-if="fetchingData"></the-content-loading>

    <form v-else @submit.prevent="onSave()">
      <div class="form-group">
        <!-- <label for="stop-words"></label> -->
        <textarea
          v-model="stopWords"
          class="form-control"
          id="stop-words"
          rows="10"
          style="height: auto"
        ></textarea>
      </div>

      <button-component
        type="submit"
        classes="btn-primary"
        class="mt-2"
        :buttonText="saveButtonText"
      >
      </button-component>
    </form>
  </div>
</NuxtLayout>
</template>

<script>
import settingsApi from "~/apis/settingsApi";
import adminMenu from "~/json/admin/json/menu.json";
export default {
  name: "stopWord",
  setup() {
    definePageMeta({
      name: "stopWord",
      layout: false,
    });
  },
  created() {
    this.httpService = useNuxtApp()["$http"];
  },
  mounted() {
    this.getData();
  },
  watch: {
    //   $route: {
    //     handler: function () {
    //       this.$store.state.collapsed = false;
    //     },
    //     deep: true,
    //     immediate: true,
    //   },
  },
  props: {
    saveButtonText: {
      default: "ذخیره",
    },
  },
  data() {
    return {
      adminMenu: adminMenu,
      httpService: {},
      stopWords: "",
      fetchingData: false,
      data: undefined,
      buttonLoading: false,
    };
  },

  methods: {
    getData() {
      var vm = this;
      this.fetchingData = true;

      var url = settingsApi.app.getByKey;
      url = url.replace("{{key_option}}", "words_stop");
      // let url = apis.admin.get.replace("{{system}}", this.$route.meta.apiKey);

      this.httpService.getRequest(url)
        .then((response) => {
          vm.fetchingData = false;

          this.stopWords = response.data.hits.hits[0]._source.value;
          this.data = response.data.hits.hits[0]._source;
        })

        .finally(() => {
          vm.fetchingData = false;
        });
    },
    onSave() {
      if (this.buttonLoading) return;
      this.buttonLoading = true;

      // var url = apis.admin.save;
      // const payload = { ...this.data, ...{ value: this.stopWords } };

      var url = settingsApi.app.saveByKey;
      url = url.replace("{{key_option}}", "words_stop");
      const payload = { value: this.stopWords };

      this.httpService.postRequest(url, payload)
        .then((response) => {
          mySwalToast({
            title: "تبریک",
            html: response.data.message,
            icon: "success",
          });
        })

        .finally(() => {
          this.buttonLoading = false;
        });
    },
  },

};
</script>
