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

      <!-- <my-quill
        :parentButtonLoading="buttonLoading"
        v-else
        :tinyText="tinyText"
        @input="onSave"
        @on-save="onSave"
      ></my-quill> -->

      <quill-editor
        v-else
        v-model="tinyText"
        content-type="html"
        :options="editorOptions"
      ></quill-editor>
      <button @click="onSave" type="button" class="btn btn-primary mt-3">
        ذخیره
      </button>
    </div>
  </NuxtLayout>
</template>

<script>
import settingsApi from "~/apis/settingsApi";
import adminMenu from "~/json/admin/json/menu.json";

export default {
  name: "dataSettingContactUs",
  setup() {
    definePageMeta({
      name: "dataSettingContactUs",
      layout: false,
    });
  },
  created() {
    this.httpService = useNuxtApp()["$http"];
  },
  mounted() {
    this.getData();
  },
  watch: {
    // $route: {
    //   handler: function () {
    //     this.$store.state.collapsed = false;
    //   },
    //   deep: true,
    //   immediate: true,
    // },
  },
  data() {
    return {
      adminMenu: adminMenu,
      httpService: {},
      buttonLoading: false,
      tinyText: "",
      fetchingData: false,
      data: undefined,
      editorOptions: {
        theme: "snow", // تم ویرایشگر: 'snow' یا 'bubble'
        placeholder: "متن خود را بنویسید...",
        modules: {
          toolbar: [
            [{ header: [1, 2, 3, false] }], // سطح هدینگ‌ها
            ["bold", "italic", "underline", "strike"], // دکمه‌های استایل متن
            [{ color: [] }, { background: [] }], // رنگ متن و پس‌زمینه
            [{ list: "ordered" }, { list: "bullet" }], // لیست‌های شماره‌دار و نقطه‌ای
            ["blockquote", "code-block"], // نقل‌قول و کد
            [{ align: [] }], // تراز بندی متن
            ["link", "image", "video"], // افزودن لینک، تصویر، و ویدیو
            ["clean"], // حذف فرمت‌ها
          ],
        },
      },
    };
  },

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

      var url = settingsApi.app.getByKey;
      url = url.replace("{{key_option}}", "conect_text");

      // let url = apis.admin.get.replace('{{system}}', this.$route.meta.apiKey)

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

          this.tinyText = response.data.hits.hits[0]._source.value;
          this.data = response.data.hits.hits[0]._source;
        })
        .catch((error) => {})
        .finally(() => {
          vm.fetchingData = false;
        });
    },
    onSave(newContent) {
      if (this.buttonLoading) return;
      this.buttonLoading = true;

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

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

      this.httpService
        .postRequest(url, payload)
        .then((response) => {
          mySwalToast({
            title: "تبریک",
            html: response.data.message,
            icon: "success",
          });
        })
        .catch((error) => {})
        .finally(() => {
          this.buttonLoading = false;
        });
    },
  },
};
</script>