<template>
  <div class="page">
    <div class="container">
      <form>
        <div class="form-row">
          <div class="col-3">
            <label for=""> انتخاب بخش:</label>
            <multiselect
              :allow-empty="false"
              :searchable="true"
              :close-on-select="true"
              :show-labels="false"
              :value="value"
              :options="options"
              class="multi"
              @select="select"
              label="label"
              track-by="key"
              placeholder="دسته"
              :hide-selected="false"
              :max-height="200"
            >
            </multiselect>
          </div>
        </div>
        <div class="form-row">
          <div class="col-6">
            <label for="">عنوان سوال:</label>
            <input
              type="text"
              class="form-control"
              placeholder="عنوان سوال را کوتاه و واضح وارد نمایید"
              v-model="title"
            />
          </div>
          <div class="col-6">
            <label for="">نشانی صفحه:</label>
            <input
              type="text"
              class="form-control"
              placeholder="نشانی از همین سامانه انتخاب کنید"
              v-model="link"
            />
          </div>
        </div>
        <div class="form-row">
          <div class="col">
            <label for="">توضیح کوتاه:</label>
            <input
              type="text"
              placeholder="توضیح مختصر حداکثر یک خط باشد"
              v-model="description"
              maxlength="500"
            />
          </div>
        </div>
        <div class="form-row">
          <div class="col">
            <label for="" class="mt-2">توضیح مفصل:</label>
            <!-- <VueEditor
              dir="rtl"
              v-model="editorData"
              :editorOptions="editorOptions"
            ></VueEditor> -->
          </div>
        </div>
      </form>
      <button class="btn-save" @click="saveData">ثبت</button>
    </div>
  </div>
</template>
<script>
import settingsApi from "~/apis/settingsApi";

// todo: install vueeditor for nuxt3/vue3
// import { VueEditor } from "vue2-editor";
import { mapState, mapActions } from "pinia";
import searchApi from "~/apis/searchApi";
import { useStorage } from "@vueuse/core";

export default {
  props: ["editList"],
  beforeMount() {
    this.httpService = new HttpService(import.meta.env.VITE_REPO_BASE_URL);
  },
  mounted() {
    let localStoageHelpSchema = useStorage("settingSchema",undefined).value;

    if (localStoageHelpSchema) {
      let helpSchema = JSON.parse(localStoageHelpSchema);
      this.helpSchemaSetter(helpSchema);
    } else this.getSchemas();
    this.options = this.helpSchemaGetter[0]?.category;
    this.handlerItemEdit();
  },
  

  data() {
    return {
      value: "",
      editorData: "",
      title: "",
      link: "",
      description: "",
      fullDesc: "",
      httpService: undefined,
      options: [{ name: "جستجو" }, { name: "محتوا" }, { name: "مطالعه" }],
      editorOptions: {
        formats: {
          direction: "rtl",
          align: "right",
        },
        placeholder: "توضیحات...",
        // readOnly: true,
        // theme: "snow",
      },
    };
  },
  computed: {
    ...mapState(useSearchStore, ["helpSchemaGetter"]),
    ...mapState(["organNameGetter"]),
  },
  methods: {
    ...mapActions("search", ["helpSchemaSetter"]),
    getSchemas() {
      this.httpService
        .postRequest(searchApi.schema.list, {
          organ: this.organNameGetter,
          system: "setting",
          build_state: buildState(),
        })
        .then((response) => {
          this.helpSchemaSetter(response.data.setting);
          // this.options = response;
        })
        
    },
    select(e) {
      this.value = e;
    },
    saveData() {
      var id = this.editList._id;
      let url = settingsApi.help.editItem;
      url = url.replace("{{id}}", id);

      let cleaned = this.link;
      if (this.link.startsWith(origin) || this.link == "") {
        cleaned = this.link.replace(origin, "");
      } else {
        let errorMessage = "نشانی پیوند باید با " + origin + "شروع شود";
        mySwalToast({
          title: errorMessage,
          // html: response.data.message,

          icon: "error",
          timer: 7000,
        });
        return "";
      }

      let payload = {
          value_key: this.value.key,
          value: this.editorData,
          meta : {
            title: this.title,
            link: cleaned,
            description: this.description,

          }
      } 


      this.httpService
        .postRequest(url, payload)
        .then((response) => {
          this.$emit("close");

          // this.helpSchemaSetter(response.data.setting);
          // this.options = response;
        })
        
    },
    handlerItemEdit() {
      this.link = this.editList._source?.meta?.link;
      this.title = this.editList._source?.meta?.title;
      this.description = this.editList._source?.meta?.description;
      this.editorData = this.editList._source?.value;
      this.options.forEach((element) => {
        if (element.key == this.editList._source?.value_key) {
          this.value = element;
        }
      });
    },
  },

};
</script>
<style scoped lang="scss">
input {
  border-radius: 0.5rem;
  border: 2px solid rgb(127, 170, 170);
  font-size: 14px;
  height: 3em;
  width: 100%;
  &::placeholder {
    color: #a7a098;
  }
}
.first {
  width: 50%;
}
.multi {
  width: 200px;
  border: 2px solid rgb(127, 170, 170);
  border-radius: 0.5rem;
}
.btn-save {
  width: 100px;
  height: 2.5em;
  float: left;
  background-color: white;
  margin-top: 0.8em;
  border-radius: 0.5rem;
  border: 2px solid rgb(127, 170, 170);
}
.page {
  height: calc(100vh - 23em);
}
@media (max-width: 575.98px) {
}
@media only screen and (min-width: 576px) and (max-width: 767.98px) {
}
@media only screen and (min-width: 768px) and (max-width: 991.98px) {
}
@media only screen and (min-width: 992px) and (max-width: 1199.98px) {
}
@media (min-width: 1200px) {
}
</style>
