<template>
  <form class="task-form">
    <div class="form-row form-row-basic mx-0 justify-content-end">
      <div
        class="main-page_body-items items items-top d-flex align-items-center col-12"
      >
        <div
          v-if="templateValue.title == 'multi-org_workbase'"
          class="col-2 pl-0 pr-0 task-Form-Organ"
        >
          <multiselect
            v-model.number="taskForm.organ"
            class="multiselect Organ"
            tag-placeholder="Add this as new tag"
            placeholder="سازمان"
            :options="organOptions"
            :taggable="true"
            :close-on-select="true"
            :clear-on-select="false"
            :searchable="true"
            :max-height="250"
            :preserve-search="true"
            @tag="addTagOrgan"
          ></multiselect>
          <!-- <pre
                        class="language-json"
                    ><code>{{ taskForm.category  }}</code></pre> -->
        </div>
        <div class="col-2 pl-0 pr-0 task-Form-category">
          <multiselect
            class="multiselect category"
            id="task-Form-category"
            v-model.number="taskForm.category"
            tag-placeholder="Add this as new tag"
            placeholder="دسته"
            :options="categoryOptions"
            :taggable="true"
            :close-on-select="true"
            :clear-on-select="false"
            :searchable="true"
            :max-height="250"
            :preserve-search="true"
            @tag="addTagCategury"
          ></multiselect>
          <!-- <pre
                        class="language-json"
                    ><code>{{ taskForm.category  }}</code></pre> -->
        </div>

        <vue-tribute
          class="col-3 pl-0 pr-0 tribute-container"
          :options="tributeOptions"
        >
          <input
            class="label-1 form-control task-form-control task-title"
            placeholder="عنوان"
            v-model="taskForm.title"
            cols="20"
            rows="3"
            @tribute-no-match="noMatchFound"
            @tribute-replaced="textReplaced"
            @keydown.tab="foucsNext('duration')"
          />
        </vue-tribute>
        <input
          v-model="taskForm.duration"
          ref="duration"
          type="text"
          placeholder="مدت "
          @blur="timeCorrect(taskForm.duration)"
          v-on:focus="timeFocus('duration', taskForm.duration)"
          class="col-1 label-3 form-control task-form-control"
        />
        <div class="col-4 label-4">
          <input
            v-model="taskForm.description"
            type="text"
            placeholder="گویا و مختصر بنویسید"
            class="label-4 form-control task-form-control Description"
            @keydown.tab="postInTaskWork(taskForm)"
          />
          <div class="d-flex align-items-center">
            <span
              @click="postInTaskWork(taskForm)"
              title="ثبت"
              class="btn main-page_body-items-icon pl-0"
            >
              <svg
                class="icon icon-Component-233--1 main-page_body-items-icon-add"
              >
                <use xlink:href="#icon-Component-233--1"></use>
              </svg>
            </span>
            <button-component
              @click="closeFormTaskInformation(taskForm)"
              buttonText=""
              v-tooltip="'حذف فرایند'"
            >
              <span class="text-danger">
                <svg class="icon icon-Component-295--1">
                  <use xlink:href="#icon-Component-295--1"></use>
                </svg>
              </span>
            </button-component>
          </div>
        </div>
      </div>
    </div>
    <div class="form-row form-row-mobile mx-0 justify-content-end">
      <div
        class="main-page_body-items items items-top d-flex align-items-center col-12"
      >
        <!-- <input
                    v-model="taskForm.category"
                    type="text"
                    :lang="userLang"
                    placeholder="دسته "
                    class="col-3 label-1 form-control task-form-control"
                    id="Input"
                    autofocus
                /> -->

        <!-- <vue-tribute class="col-6 pl-0 pr-0" :options="tributeOptions"> -->
        <div v-if="templateValue.title == 'multi-org_workbase'" class="col-6">
          <multiselect
            v-model.number="taskForm.organ"
            class="multiselect Organ Organ-mobile"
            tag-placeholder="Add this as new tag"
            placeholder="سازمان"
            :options="organOptions"
            :taggable="true"
            :close-on-select="true"
            :clear-on-select="false"
            :searchable="true"
            :max-height="250"
            :preserve-search="true"
            @tag="addTagOrgan"
          ></multiselect>
          <!-- <pre
                        class="language-json"
                    ><code>{{ taskForm.category  }}</code></pre> -->
        </div>
        <div class="col-6">
          <multiselect
            id="task-Form-category"
            v-model.number="taskForm.category"
            tag-placeholder="Add this as new tag"
            placeholder="دسته"
            :options="categoryOptions"
            :taggable="true"
            :close-on-select="true"
            :clear-on-select="false"
            :searchable="true"
            :max-height="250"
            :preserve-search="true"
            @tag="addTagCategury"
          ></multiselect>
        </div>
        <!-- </vue-tribute> -->
      </div>

      <div
        class="col-12 label-4 main-page_body-items items items-bottom d-flex align-items-center mb-1"
      >
        <vue-tribute class="col-5 pl-0 pr-0" :options="tributeOptions">
          <input
            class="label-1 form-control task-form-control task-title"
            placeholder="عنوان"
            v-model="taskForm.title"
            cols="20"
            rows="3"
            @tribute-no-match="noMatchFound"
            @tribute-replaced="textReplaced"
          />
        </vue-tribute>
        <input
          v-model="taskForm.duration"
          ref="duration2"
          type="text"
          placeholder="مدت "
          @blur="timeCorrect(taskForm.duration)"
          v-on:focus="timeFocus('duration2', taskForm.duration)"
          class="col-2 label-3 form-control task-form-control"
        />
        <input
          v-model="taskForm.description"
          type="text"
          placeholder="توضیحات(گویا،مختصر)"
          class="label-5 form-control task-form-control Description"
        />
      </div>
      <div class="d-flex align-items-center ml-2">
        <span
          @click="postInTaskWork(taskForm)"
          title="ثبت"
          class="btn main-page_body-items-icon pl-0"
        >
          <svg class="icon icon-Component-233--1 main-page_body-items-icon-add">
            <use xlink:href="#icon-Component-233--1"></use>
          </svg>
        </span>
        <span title="حذف" class="btn pl-0">
          <svg
            @click="closeFormTaskInformation(taskForm)"
            class="icon icon-Component-295--1 main-page_body-items-icon-close"
          >
            <use xlink:href="#icon-Component-295--1"></use>
          </svg>
        </span>
      </div>
    </div>
  </form>
</template>

<script>
import taskApi from "~/apis/taskApi";


import VueTribute from "vue-tribute";

export default {
  props: {
    itemIndex: {
      default: 0,
    },
    isMultiOrgan: {
      default: false,
    },
    taskForm: {
      default() {
        return {
          organ: "",
          category: "",
          title: "",
          duration: "",
          description: "",
        };
      },
    },
    templateValue: {
      default() {
        return {
          name: "تک سازمانی - کارمحور",
          title: "single-org_workbase",
        };
      },
    },
  },
  emits: ["close-form-tasks", "save-form-tasks", "add-new-record"],
  beforeMount() {
    this.httpService = new HttpService(
      import.meta.env.VITE_BASE_URL + taskUrl()
    );
  },
  // mounted() {
  // },
  data() {
    return {
      categoryOptions: [
        "جلسه",
        "مدل سازی",
        "نظارت مدل سازی",
        "استخراج حکم",
        "تحقیق و توسعه",
        "پردازش داده",
        "مستند سازی",
        "توسعه بک",
        "توسعه فرانت",
        "اشکالات بک",
        "اشکالات فرانت",
        "آموزش",
        "پیگیری اداری",
        "مدیریت پروژه",
        "سرور و تنظیمات",
        "هوش مصنوعی",
        "نیرو یاری",
        "تحلیل طراحی",
        "متفرقه",
      ],
      organOptions: ["همتا", " فرهنگستان", " تیم فنی"],
      showOrgan: this.isMultiOrgan,
      userLang: "fa-IR",

      tributeOptions: {
        // symbol or string that starts the lookup
        trigger: "",

        // element to target for @mentions
        iframe: null,

        // class added in the flyout menu for active item
        selectClass: "highlight",

        // class added to the menu container
        containerClass: "tribute-container mini",

        // class added to each list item
        itemClass: "my-tribute-class",

        // function called on select that returns the content to insert
        selectTemplate: function (item) {
          return item.original.title;
          // return "@" + item.original.value;
        },

        // template for displaying item in menu
        menuItemTemplate: function (item) {
          return item.original.title;
        },

        // template for when no match is found (optional),
        // If no template is provided, menu is hidden.
        noMatchTemplate:
          '<span class="no-result">مورد مشابهی ثبت نشده است.</span>',

        // specify an alternative parent container for the menu
        // container must be a positioned element for the menu to appear correctly ie. `position: relative;`
        // default container is the body
        menuContainer: document.body,

        // column to search against in the object (accepts function or string)
        lookup: "title",

        // column that contains the content to insert by default
        fillAttr: "title",

        // REQUIRED: array of objects to match or a function that returns data (see 'Loading remote data' for an example)
        // values: [
        //   {
        //     key: null,
        //     value: "sghl",
        //   },
        // ],
        // values: ["سلام", "باسلام", "علیک سلام", "بی سلام", "صدتا سلام"],
        values: (text, cb) => {
          this.remoteSearch(text, (items) => cb(items));
        },
        // When your values function is async, an optional loading template to show
        loadingItemTemplate: null,

        // specify whether a space is required before the trigger string
        requireLeadingSpace: true,

        // specify whether a space is allowed in the middle of mentions
        allowSpaces: true,

        // optionally specify a custom suffix for the replace text
        // (defaults to empty space if undefined)
        // replaceTextSuffix: "",

        // specify whether the menu should be positioned.  Set to false and use in conjuction with menuContainer to create an inline menu
        // (defaults to true)
        positionMenu: true,

        // when the spacebar is hit, select the current match
        spaceSelectsMatch: false,

        // turn tribute into an autocomplete
        autocompleteMode: true,

        // Customize the elements used to wrap matched strings within the results list
        // defaults to <span></span> if undefined
        searchOpts: {
          pre: '<span style="color:red">',
          post: "</span>",
          skip: false, // true will skip local search, useful if doing server-side search
        },

        // Limits the number of items in the menu
        menuItemLimit: 25,

        // specify the minimum number of characters that must be typed before menu appears
        menuShowMinLength: 0,
      },
    };
  },
  computed: {
    taskUrl() {
      return import.meta.env.VITE_TASK;
    },
  },
  methods: {
    foucsNext(ref) {
      this.$refs[ref].focus();
    },
    // setMultiOrgan(value){
    //   this.isMultiOrgan = value;
    // },
    timeFocus(ref, value) {
      //if (value == "00:00" || value == "" || value == "مدت")
      this.$refs[ref].select();
    },
    timeCorrect(duration) {
      duration = duration.trim();
      duration = duration.replace(".", ":");
      duration = duration.replace(" ", ":");

      if (duration.indexOf(":") == -1) {
        if (duration.length == 0) duration = "00:00";
        else if (duration.length == 1) duration = "0" + duration + ":00";
        else if (duration.length == 2) duration = duration + ":00";
        else if (duration.length == 3)
          duration =
            "0" + duration.substring(0, 1) + ":" + duration.substring(1);
        else if (duration.length == 4)
          duration =
            duration.substring(0, 2) +
            ":" +
            duration.substring(2).padStart(2, "0");
        else if (duration.length > 4)
          duration = duration.substring(0, 2) + ":" + duration.substring(3, 5);
      }
      this.taskForm.duration = duration;
    },
    noMatchFound(args) {
    },
    textReplaced(args) {
    },

    closeFormTaskInformation() {
      this.$emit("close-form-tasks");
    },
    postInTaskWork(taskForm) {
      this.$emit("save-form-tasks", taskForm);
    },
    openFormTaskInformation() {
      this.$emit("add-new-record");
    },
    addTagCategury(newTag) {
      // const tag = {
      //   name: newTag,
      // };
      this.taskForm.category = newTag;
    },
    addTagOrgan(newTag) {
      // const tag = {
      //   name: newTag,
      // };
      this.taskForm.organ = newTag;
    },
    addTagTitle(newTag) {
      // const tag = {
      //   name: newTag,
      // };

      this.taskForm.title = newTag;
    },
    remoteSearch(text, cb) {
      let url = taskApi.workingHours.complition.replace("{{query}}", text);
      if (text == "") url = taskApi.workingHours.complitionAll;
      this.httpService
        .getRequest(url)
        .then((response) => {
          cb(response.data);
        })
        
        
    },
    isNumberKey(evt) {
      var charCode = evt.which ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
      return true;
    },
  },

};
</script>

<style lang="scss" scoped>
// input::placeholder {
//   color: #d4dce3;
// }
// textarea::placeholder {
//   color: #d4dce3;
// }

.form-row {
  border: 1px rgb(223 223 223) solid;
  border-radius: 0.5em;
  margin-bottom: 0.5em;
}
.task-Form-title {
  border-right: 1px var(--color-items-7) solid;
}
// .my-tribute-class {
//   background: #d4dce3;
// }
</style>
