<script>
import Layout from "@/layout/custom.vue";
import ApiServiece from "@/services/ApiService";
import moment from "jalali-moment";
import VueSelect from "vue3-select-component";
import { onMounted, ref, watch, computed } from "vue";
import { toast } from "vue3-toastify";
import "vue3-toastify/dist/index.css";
import Swal from "sweetalert2";
import showDescription from "@/components/modals/commonModals/showDescription.vue";

export default {
  name: "BORDER",
  components: {
    Layout,
    VueSelect,
    showDescription,
  },
  setup() {
    const products = ref([]);
    const selectedProduct = ref();
    const blogs = ref([]);
    const selectedBlog = ref();
    const selectedCommentType = ref("");
    const comment = ref();
    const searchPage = ref();
    const currentPage = ref(1);
    const totalPages = ref(1);
    const paginate = ref(20);
    const page = ref(1);
    const selectedStatus = ref("");
    const filterLoading = ref(false);
    const searchQuery = ref("");
    const comments = ref();

    const convertToJalali = (date) => {
      return moment(date, "YYYY-MM-DD HH:mm:ss")
        .locale("fa")
        .format("YYYY/MM/DD");
    };
    watch(searchQuery, () => {
      getComments();
      page.value = 1;
    });
    const getComments = () => {
      filterLoading.value = true;
      ApiServiece.get(
        `admin/comments?type=${selectedCommentType.value || ""}&status=${
          selectedStatus.value || ""
        }&commentable_id=${
          selectedBlog.value ?? selectedProduct.value ?? ""
        }&paginate=${paginate.value || 10}&page=${page.value || 1}`
      )
        .then((resp) => {
          filterLoading.value = false;
          comments.value = resp.data.data.data;
          console.log(comments.value);
          currentPage.value = resp.data.data.current_page;
          totalPages.value = resp.data.data.last_page;
        })
        .catch(() => {
          filterLoading.value = false;
        });
    };

    const handleBlogSearch = async (searchTerm) => {
      if (searchTerm.length < 3) return;

      try {
        const response = await ApiServiece.get(
          `admin/blogs?title=${searchTerm}`
        );
        blogs.value = response.data.data;
      } catch (error) {
        blogs.value = [];
      }
    };

    const formattedBlog = computed(() =>
      Array.isArray(blogs.value)
        ? blogs.value.map((blog) => ({
            value: blog.id,
            label: blog.title,
          }))
        : []
    );

    const handleProductsSearch = async (searchTerm) => {
      if (searchTerm.length < 3) return;

      try {
        const response = await ApiServiece.get(
          `admin/products?title=${searchTerm}`
        );
        products.value = response.data.data;
      } catch (error) {
        products.value = [];
      }
    };

    const formattedProducts = computed(() =>
      Array.isArray(products.value)
        ? products.value.map((product) => ({
            value: product.id,
            label: product.title,
          }))
        : []
    );

    const modalData = (text) => {
      comment.value = text;
    };

    const visiblePages = computed(() => {
      const pages = [];
      if (totalPages.value <= 5) {
        for (let i = 1; i <= totalPages.value; i++) {
          pages.push(i);
        }
      } else {
        let start = currentPage.value - 2;
        let end = currentPage.value + 2;

        if (start < 1) start = 1;
        if (end > totalPages.value) end = totalPages.value;

        for (let i = start; i <= end; i++) {
          pages.push(i);
        }
      }
      return pages;
    });

    const deleteDiscount = (id, title) => {
      Swal.fire({
        text: `می خواهید تخفیف ${title} را حذف کنید؟`,
        icon: "warning",
        showCancelButton: true,
        confirmButtonColor: "#3085d6",
        cancelButtonColor: "#d33",
        confirmButtonText: "بله!",
        cancelButtonText: "خیر",
      }).then((result) => {
        if (result.isConfirmed) {
          ApiServiece.delete(`admin/discounts/${id}`)
            .then(() => {
              toast.success("!تخفیف با موفقیت حذف شد", {
                position: "top-right",
                autoClose: 3000,
              });
              comments.value = comments.value.filter(
                (comment) => comment.id !== id
              );
            })
            .catch((err) => {
              console.log(err);
              toast.error("!مشکلی در حذف کردن تخفیف پیش آمد", {
                position: "top-right",
                autoClose: 3000,
              });
            });
        }
      });
    };

    function handlePageInput() {
      if (searchPage.value < 1) {
        searchPage.value = 1;
      } else if (searchPage.value > totalPages.value) {
        searchPage.value = totalPages.value;
      }

      if (searchPage.value >= 1 && searchPage.value <= totalPages.value) {
        page.value = searchPage.value;
      }
    }

    const changeCommentStatus = (id, op) => {
      let text, successMessage, errorMessage;
      if (op === "confirmed") {
        text = ` می خواهید این نظر را قبول کنید؟`;
        successMessage = "!نظر با موفقیت قبول شد";
        errorMessage = "!مشکلی در تغییر وضعیت نظر ایجاد شد";
      } else if (op === "rejected") {
        text = `می خواهید این نظر را رد کنید؟`;
        successMessage = "!نظر با موفقیت رد شد";
        errorMessage = "!مشکلی در تغییر وضعیت نظر ایجاد شد";
      }

      Swal.fire({
        text: text,
        icon: "warning",
        showCancelButton: true,
        confirmButtonColor: "#3085d6",
        cancelButtonColor: "#d33",
        confirmButtonText: "بله!",
        cancelButtonText: "خیر",
      }).then((result) => {
        if (result.isConfirmed) {
          const formData = new FormData();
          formData.append("status", op);

          ApiServiece.put(`admin/comments/${id}`, formData)
            .then(() => {
              toast.success(successMessage, {
                position: "top-right",
                autoClose: 3000,
              });
              getComments();
            })
            .catch(() => {
              toast.error(errorMessage, {
                position: "top-right",
                autoClose: 3000,
              });
            });
        }
      });
    };

    watch(searchQuery, () => {
      getComments();
    });

    watch(page, () => {
      getComments();
    });

    watch(selectedStatus, () => {
      getComments();
    });

    watch(selectedCommentType, () => {
      selectedProduct.value = "";
      selectedBlog.value = "";
      getComments();
    });

    watch([selectedBlog, selectedProduct], () => {
      getComments();
    });

    const nextPage = () => {
      if (currentPage.value < totalPages.value) {
        page.value++;
        getComments();
      }
    };

    const prevPage = () => {
      if (currentPage.value > 1) {
        page.value--;
        getComments();
      }
    };

    onMounted(() => {
      getComments();
    });
    return {
      comments,
      convertToJalali,
      changeCommentStatus,
      deleteDiscount,
      searchQuery,
      filterLoading,
      currentPage,
      totalPages,
      nextPage,
      prevPage,
      page,
      handlePageInput,
      searchPage,
      visiblePages,
      modalData,
      comment,
      selectedCommentType,
      selectedStatus,
      handleBlogSearch,
      formattedBlog,
      selectedBlog,
      formattedProducts,
      handleProductsSearch,
      selectedProduct,
    };
  },
};
</script>
<template>
  <Layout>
    <BRow>
      <div class="col-md-12">
        <div class="card shadow-sm border-0 rounded">
          <div
            class="card-header d-flex justify-content-between align-items-center p-3"
            dir="rtl"
          >
            <div class="d-flex align-items-center">
              <select
                class="form-select form-select-sm"
                v-model="selectedCommentType"
                style="width: 120px; border-radius: 15px"
              >
                <option value="" disabled selected>نوع نظر</option>
                <option value="">همه</option>
                <option value="product">محصول</option>
                <option value="blog">بلاگ</option>
              </select>

              <select
                class="form-select form-select-sm"
                v-model="selectedStatus"
                style="width: 120px; border-radius: 15px; margin-right: 7px"
              >
                <option value="" disabled selected>وضعیت</option>
                <option value="">همه</option>
                <option value="confirmed">تایید شده</option>
                <option value="rejected">رد شده</option>
                <option value="pending">معلق</option>
              </select>

              <VueSelect
                v-if="selectedCommentType === 'blog'"
                style="
                  --vs-border-radius: 16px;
                  margin-right: 7px;
                  --vs-min-height: 18px;
                "
                v-model="selectedBlog"
                :options="formattedBlog"
                placeholder="بلاگی را انتخاب کنید"
                @search="handleBlogSearch"
              />

              <VueSelect
                v-if="selectedCommentType === 'product'"
                style="
                  --vs-border-radius: 16px;
                  margin-right: 7px;
                  --vs-min-height: 18px;
                "
                v-model="selectedProduct"
                :options="formattedProducts"
                placeholder="محصولی را انتخاب کنید"
                @search="handleProductsSearch"
              />
            </div>
          </div>
          <div v-if="!filterLoading" class="card-body table-border-style p-0">
            <div class="table-responsive">
              <table class="table table-hover table-bordered m-0" dir="rtl">
                <thead class="table-light">
                  <tr>
                    <th>نویسنده</th>

                    <th>عنوان</th>
                    <th>نام محصول</th>
                    <th>امتیاز</th>
                    <th>نظر</th>
                    <th>وضعیت</th>
                    <th>تاریخ ایجاد</th>
                    <th>عملیات</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="comment in comments" :key="comment.id">
                    <td>{{ comment?.user?.name }}</td>

                    <td v-if="comment?.commentable_type === 'product'">
                      محصول
                    </td>
                    <td v-if="comment?.commentable_type === 'blog'">بلاگ</td>
                    <td>{{ comment.commentable.title }}</td>
                    <td>
                      <span
                        v-for="n in 5"
                        :key="n"
                        :class="
                          n <= comment.rate ? 'fa fa-star' : 'fa fa-star-o'
                        "
                      ></span>
                    </td>

                    <td
                      data-bs-toggle="modal"
                      data-bs-target="#showDescription"
                      @click="modalData(comment.text)"
                      class="comment-td"
                    >
                      <span class="comment-text">
                        {{
                          comment.text.length > 20
                            ? comment.text.substring(0, 20) + "..."
                            : comment.text
                        }}
                      </span>
                    </td>
                    <td>
                      <span
                        :class="{
                          badge: true,
                          'bg-success': comment.status === 'confirmed',
                          'bg-danger': comment.status === 'rejected',
                          'bg-warning': comment.status === 'pending',
                        }"
                      >
                        {{
                          comment.status === "confirmed"
                            ? "تایید شده"
                            : comment.status === "rejected"
                            ? "رد شده"
                            : comment.status === "pending"
                            ? "معلق"
                            : comment.status
                        }}
                      </span>
                    </td>
                    <td>{{ convertToJalali(comment?.created_at) }}</td>

                    <td>
                      <div class="dropdown">
                        <button
                          class="btn btn-sm btn-outline-primary dropdown-toggle"
                          type="button"
                          id="dropdownMenuButton"
                          data-bs-toggle="dropdown"
                          aria-expanded="false"
                        >
                          تغییر وضعیت
                        </button>
                        <ul
                          class="dropdown-menu"
                          aria-labelledby="dropdownMenuButton"
                        >
                          <li
                            v-if="
                              comment.status === 'rejected' ||
                              comment.status === 'pending'
                            "
                          >
                            <a
                              class="dropdown-item"
                              href="#"
                              @click="
                                changeCommentStatus(comment.id, 'confirmed')
                              "
                            >
                              <i class="ph-duotone ph-check-circle"></i> قبول
                              نظر
                            </a>
                          </li>

                          <li
                            v-if="
                              comment.status === 'confirmed' ||
                              comment.status === 'pending'
                            "
                          >
                            <a
                              class="dropdown-item"
                              href="#"
                              @click="
                                changeCommentStatus(comment.id, 'rejected')
                              "
                            >
                              <i class="ph-duotone ph-x-circle"></i> رد نظر
                            </a>
                          </li>
                        </ul>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          <div
            v-else
            class="filter-loader card table-card user-profile-list"
          ></div>
        </div>
      </div>

      <showDescription :desc="comment" />
    </BRow>
    <BRow>
      <BCol sm="12">
        <div class="d-flex justify-content-center">
          <nav aria-label="Page navigation">
            <ul class="pagination">
              <li class="page-item" :class="{ disabled: currentPage === 1 }">
                <span class="page-link" @click="prevPage">قبلی</span>
              </li>
              <li v-if="currentPage > 2" class="page-item" @click="page = 1">
                <a class="page-link" href="javascript:void(0)">1</a>
              </li>
              <li v-if="currentPage > 3" class="page-item" disabled>
                <span class="page-link">...</span>
              </li>
              <li
                v-for="n in visiblePages"
                :key="n"
                class="page-item"
                :class="{ active: currentPage === n }"
              >
                <a
                  class="page-link"
                  href="javascript:void(0)"
                  @click="page = n"
                >
                  {{ n }}
                </a>
              </li>

              <li
                v-if="currentPage < totalPages - 2"
                class="page-item"
                disabled
              >
                <span class="page-link">...</span>
              </li>
              <li
                v-if="currentPage < totalPages - 1"
                class="page-item"
                @click="page = totalPages"
              >
                <a class="page-link" href="javascript:void(0)">{{
                  totalPages
                }}</a>
              </li>

              <li
                class="page-item"
                :class="{ disabled: currentPage === totalPages }"
              >
                <span class="page-link" @click="nextPage">بعدی</span>
              </li>
            </ul>
          </nav>
        </div>
      </BCol>
      <BCol sm="4">
        <div class="ms-0 search-number">
          <input
            v-model="searchPage"
            type="text"
            class="form-control"
            placeholder="برو به صفحه"
            :max="totalPages"
            min="1"
            @input="handlePageInput"
          />
        </div>
      </BCol>
    </BRow>
  </Layout>
</template>

<style scoped>
.card {
  transition: transform 0.3s ease;
}
.card:hover {
  transform: translateY(-3px);
}
.table th,
.table td {
  vertical-align: middle;
  text-align: center;
}
.filter-loader {
  border: 4px solid rgba(0, 123, 255, 0.3);
  border-top: 4px solid #007bff;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin: 20px auto;
}

.subject-box {
  padding: 8px 14px;
  background: linear-gradient(135deg, #fff3e0, #ffe0b2);
  color: #ef6c00;
  font-weight: 600;
  border-radius: 10px;
  box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.subject-box i {
  color: #ef6c00;
  font-size: 1rem;
}

.subject-box:hover {
  transform: translateY(-2px);
  box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.15);
}
.search-number {
  display: flex;
  align-items: center;
}

.search-number input {
  width: 150px;
  padding: 0.5rem;
  font-size: 1rem;
  border-radius: 0.375rem;
  margin-bottom: 7px;
  border: 1px solid #ced4da;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.search-number input:focus {
  border-color: #007bff;
  box-shadow: 0 0 0 0.2rem rgba(38, 143, 255, 0.25);
}

.search-number input::placeholder {
  color: #6c757d;
}

.search-number input:disabled {
  background-color: #f8f9fa;
  cursor: not-allowed;
}
.pagination {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.page-item {
  flex: 0 0 auto;
}

.page-link {
  cursor: pointer;
  user-select: none;
}
.comment-text {
  display: inline-block;
  max-width: 100%;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.comment-td {
  cursor: pointer;
}
.btn {
  transition: all 0.3s ease;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
}
</style>
