<template>
  <div class="nesha-content">
    <div class="">
      <div class="search-items firefox-scrollbar">
        <div
          class="search-items__item"
          v-for="(item, i) in listAnswer"
          :key="i"
        >
          <div class="search-items__header">
            <a class="search-items__label text__13 text__dark-gray">{{
              item._source.rostaType
            }}</a>
            <a class="search-items__title text__15 text__blue">
              {{ item._source.title }}
            </a>
          </div>
          <div class="text__15 text__dark-gray search-items__code">
            <span style="margin-left: 10px"
              >استان : {{ item._source.ostan }}</span
            >
            <span style="margin-left: 10px"
              >شهر : {{ item._source.shahr }}</span
            >
            <span style="margin-left: 10px"
              >روستا : {{ item._source.rosta }}</span
            >
            <span style="margin-left: 10px">تاریخ : {{ datefa(item) }}</span>
          </div>
          <div class="search-items__content">
            <div
              v-html="highlightText(item)"
              class="text__15 line-clamp__2"
            ></div>
            <div v-if="item._source.elat" class="text__15 line-clamp__2">
              <span style="margin-left: 10px">علت : </span>
              {{ item._source.elat }}
            </div>
            <div v-if="item._source.payamad" class="text__15 line-clamp__2">
              <span style="margin-left: 10px">پیامد : </span>
              {{ item._source.payamad }}
            </div>
            <div v-if="item._source.raahkar" class="text__15 line-clamp__2">
              <span style="margin-left: 10px">راهکار : </span>
              {{ item._source.raahkar }}
            </div>
          </div>
        </div>
      </div>
      <jahat-pagination
      style="font-size: 13px; "
        v-if="pagination.total"
        :paginationInfo="pagination"
        @page-changed="pageChanged"
        @page-limit-changed="pageLimitChanged"
        @sort-changed="sortChanged"
      >
      </jahat-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Content",
  props: ["pagination"],
  data() {
    return {
      listAnswer: [],
      totalCount: 0,
      typeCount: "",
      countInPage: 0,
      maxPage: 0,
      currentPage: 1,
      beginPage: 1,
      endPage: 1,
      listPage: [],
      textSearch: "",
      iscode: true,
      pag: this.pagination,
      sorting: {
        sortby: "created",
        sortorder: undefined, // asc | desc | none
      },
    };
  },

  beforeCreate: function () {},
  methods: {
    highlightText(item) {
      var text = "";
      if (item.highlight) {
        if (item.highlight.description)
          text = item.highlight.description.join("... ");
      } else {
        if (item._source.description) text = item._source.description;

        if (text.length > 500) text = text.substring(0, 500);
      }
      return text;
    },
    datefa(item) {
      var d = new Date(item._source.date).toLocaleDateString("fa-IR");
      return d;
    },
    setAnswer: function (list, count = -1, _typeCount = "") {
      if (count != -1) {
        this.totalCount = count;
        this.typeCount = _typeCount == "eq" ? "مساوی با " : "بیشتر از ";
        this.maxPage =
          this.totalCount == 0
            ? 0
            : Math.floor(this.totalCount / this.countInPage) + 1;
        this.currentPage = 1;
        this.beginPage = 2;
        this.endPage = this.beginPage + 3;
        if (this.endPage > this.maxPage - 1) {
          this.endPage = this.maxPage - 1;
          if (this.beginPage > 2) this.beginPage = this.beginPage - 1;
        }
        this.updateListPage();
      }
      const total = count;
      const pages = Math.ceil(total / this.pag.limit);
      const pagination = {
        total: total,
        pages: pages == 0 ? 1 : pages,
      };

      this.pag = { ...this.pagination, ...pagination };
      this.listAnswer = list;
      this.scrollToTop();
      localStorage.setItem("answer", JSON.stringify(this.listAnswer));
    },
    updateListPage: function () {
      this.listPage = [];
      for (let i = this.endPage; i >= this.beginPage; i--) {
        this.listPage.push(i);
      }
    },
    nextPage: function (item) {
      if (item > 0) this.setPage(this.currentPage + 1);
      else this.setPage(this.currentPage - 1);
    },
    setPage: function (item) {
      if (item == -1) {
        // begin ...
        item = 3;
        this.beginPage = 2;
        this.endPage = 5;
      } else if (item == -2) {
        // end ...
        item = this.maxPage - 3;
        this.beginPage = this.maxPage - 5;
        this.endPage = this.maxPage - 1;
      } else if (item == this.beginPage) {
        this.beginPage--;
        if (this.beginPage > 5) {
          this.beginPage--;
          this.endPage--;
        } else {
          this.beginPage = 2;
          this.endPage = 5;
        }
      } else if (item == this.endPage) {
        if (this.endPage < this.maxPage - 5) {
          this.endPage++;
          this.beginPage++;
        } else {
          this.beginPage = this.maxPage - 5;
          this.endPage = this.maxPage - 1;
        }
      }
      this.updateListPage();

      this.currentPage = item;
      this.$emit("changePage", item - 1);
    },
    changeCurrent: function (i) {
      this.$emit("changeCurrent", this.listAnswer[i]);
    },
    setTextSearch(item, countInPage) {
      this.textSearch = item;
      this.countInPage = countInPage;
    },
    scrollToTop() {
      window.scrollTo(0, 0);
    },
    //mehdi
    pageLimitChanged(paging) {
      this.resetPagination();
      this.pag.limit = paging.limit;

      this.$emit("changePage", this.pag);
    },
    pageChanged(paging) {
      let page = paging.pageNumber;
      page -= 1;
      this.pag.offset = page * paging.limit;
      this.pag.limit = paging.limit;
      this.pag.page = paging.pageNumber;

      this.$emit("changePage", this.pag);
    },
    sortChanged(sorting) {
      this.pag.page = this.pag.offset = 0;
      this.sorting = sorting;

      this.$emit("changePage", this.sorting);
    },
    resetPagination() {
      this.pag = {
        pages: 0,
        total: 0,
        page: 1,
        offset: 0,
        limit: 10,
      };
    },
    // getListSpecial(_entityType, _specialType) {

    //   if (this.fetchingData) return;
    //   this.fetchingData = true;

    //   this.entity_type = _entityType;

    //   let url = searchApi.Farhanghestan.search_normal;

    //   url = url + `/${this.pagination.offset}/${this.pagination.limit}/`;

    //   this.httpService.getRequest(url).then((res) => {

    //     this.listEntity = res.hits.hits;

    //     const total = res.hits.total.value;
    //     const pages = Math.ceil(total / this.pagination.limit);
    //     const pagination = {
    //       total: total,
    //       pages: pages == 0 ? 1 : pages
    //     }

    //     this.pagination = { ...this.pagination, ...pagination };

    //     this.fetchingData = false;
    //   });
    // },
    getList(_entityType) {
      if (this.fetchingData) return;
      this.fetchingData = true;

      this.entity_type = _entityType;

      let url =
        searchApi.Farhanghestan.search_normal +
        `/${this.pag.offset}/${this.pag.limit}`;

      this.httpService.getRequest(url).then((res) => {
        this.listEntity = res.hits.hits;

        const total = res.hits.total.value;
        const pages = Math.ceil(total / this.pag.limit);
        const pagination = {
          total: total,
          pages: pages == 0 ? 1 : pages,
        };

        this.pag = { ...this.pagination, ...pagination };

        this.fetchingData = false;
      });
    },
    //mehdi
  },
  
  
  mounted() {
    window.scrollTo(0, 0);
    if (this.$route.query.q) this.searchText = this.$route.query.q;
  },
};
</script>
<style lang="scss" scoped>
.detail-page__content {
  margin-right: 0 !important;
}
.detail-page__content {
  right: 79px !important;
}
.search-items__item {
  position: relative;
  padding: 1em;
  overflow: hidden;

  &:not(:last-child) {
    margin-bottom: 30px;
  }

  &:hover,
  &.active {
    background-color: var(--list-background-color);

    .search-item__actions {
      // width: 6.5em;
      width: auto;

      transition: width 0.5s;
      background: #fff;
      border-radius: 0 0.5em 0.5em 0;

      .tavasi-more-vert {
        transition: all 0.2s;
        display: none;
      }
    }
  }
}

.detail-page__content {
  position: initial;
  margin-right: 0 !important;
}
@media screen and (min-width: 992px) {
  .search-system .detail-page .detail-page__content .detail-page__tab-content {
    position: initial;
    margin: 0 !important;
    /* margin-right: 310px; */
  }
}
</style>



<style lang="scss" scoped>
// .detail-page__tab-content {
// width: 90%;
// margin-right: 0 !important;
// position: relative;
// &.serve-majles {
// width: 100%;
// }
// }
// .search-items {
//     overflow-y: auto;
//     height: calc(100vh - 13em);
// }

.search-items__item {
  position: relative;
  padding: 1em;
  overflow: hidden;

  &:not(:last-child) {
    margin-bottom: 30px;
  }

  &:hover,
  &.active {
    background-color: var(--list-background-color);

    .search-item__actions {
      // width: 6.5em;
      width: auto;

      transition: width 0.5s;
      background: #fff;
      border-radius: 0 0.5em 0.5em 0;

      .tavasi-more-vert {
        transition: all 0.2s;
        display: none;
      }
    }
  }
}

.search-item__actions {
  position: absolute;
  left: 0;
  width: 1.6em;
  top: 1em;
  // overflow: hidden;
  transition: all 0.5s;
  display: flex;
  align-items: center;

  .btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.175rem 0.35rem;

    &:hover {
      filter: brightness(0.7);
    }
    .icon-copy2 {
      font-size: 0.8rem;
    }

    &.favorites {
      color: calc(--primary-color);
      .icon-bookmark-1,
      .icon-bookmark-2 {
        height: 1.3em;
      }
    }
  }
}
// .detail-page__content {
// top: 35px !important;
// }

// @media only screen and (min-width: 576px) and (max-width: 767.98px) {
//   .detail-page__content {
//     top: 170px !important;
//   }
// }
// @media (max-width: 575.98px) {
//   .detail-page__content {
//     top: 170px !important;
//   }
// }

// @media (max-width: 575.98px) {
// }
// @media only screen and (min-width: 576px) and (max-width: 767.98px) {
//     .detail-page__tab-content {
//         max-width: 500px;
//     }
// }
// @media only screen and (min-width: 768px) and (max-width: 900.98px) {
//     .detail-page__tab-content {
//         max-width: 600px;
//     }
// }
// @media only screen and (min-width: 901px) and (max-width: 1049.98px) {
//     .detail-page__tab-content {
//         max-width: 900px;
//     }
// }
// @media (min-width: 1050px) {
// }
</style>