<script>
import Layout from "@/layout/custom.vue";
import ApiServiece from "@/services/ApiService";
import { onMounted, ref, watch, computed } from "vue";
import DatePicker from "vue3-persian-datetime-picker";
import VueSelect from "vue3-select-component";
import moment from "jalali-moment";
export default {
  name: "PRODUCT-LIST",
  components: {
    Layout,
    VueSelect,
    DatePicker,
  },
  setup() {
    const isLoading = ref(false);
    const date = ref([]);
    const brands = ref([]);
    const searchPage = ref();
    const currentPage = ref(1);
    const totalPages = ref(1);
    const paginate = ref(20);
    const page = ref(1);
    const filterLoading = ref(false);
    const selectedBrand = ref();
    const allProducts = ref([]);
    const getAllProducts = () => {
      filterLoading.value = true;
      ApiServiece.get(
        `admin/orders/order-items/approved?brand_id=${
          selectedBrand.value || ""
        }&start_date=${date.value[0] || ""}&end_date=${
          date.value[1] || ""
        }&paginate=${paginate.value || 10}&page=${page.value || 1}`
      )
        .then((resp) => {
          console.log(resp);
          allProducts.value = resp.data.data.data;
          currentPage.value = resp.data.data.current_page;
          totalPages.value = resp.data.data.last_page;
        })
        .catch((err) => {
          console.log(err);
        });
    };

    const getAllBrands = () => {
      ApiServiece.get("admin/brands")
        .then((resp) => {
          console.log(resp);
          brands.value = resp.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    };

    const convertToJalali = (date) => {
      return moment(date, "YYYY-MM-DD HH:mm:ss")
        .locale("fa")
        .format("YYYY/MM/DD");
    };

    const formattedBrands = computed(() => {
      return brands.value.map((brand) => ({
        value: brand?.id,
        label: brand?.title,
      }));
    });

    const getFile = () => {
      isLoading.value = true;
      ApiServiece.post(
        "admin/orders/order-items/approved/export",
        {},
        { responseType: "blob" }
      )
        .then((resp) => {
          const excelBlob = resp.data;

          const url = window.URL.createObjectURL(excelBlob);

          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download", "order-items-export.xlsx");
          document.body.appendChild(link);

          link.click();

          window.URL.revokeObjectURL(url);
          document.body.removeChild(link);
          isLoading.value = false;
        })
        .catch((err) => {
          console.log(err);
          isLoading.value = false;
        });
    };

    watch(selectedBrand, () => {
      getAllProducts();
      page.value = 1;
    });

    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;
    });

    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;
      }
    }

    watch(selectedBrand, () => {
      getAllProducts();
    });

    watch(date, () => {
      getAllProducts();
    });

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

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

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

    function formatWithCommas(number) {
      return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }

    onMounted(() => {
      getAllProducts();
      getAllBrands();
    });
    return {
      allProducts,
      visiblePages,
      nextPage,
      prevPage,
      handlePageInput,
      currentPage,
      totalPages,
      page,
      getFile,
      convertToJalali,
      searchPage,
      brands,
      formattedBrands,
      selectedBrand,
      date,
      isLoading,
      formatWithCommas,
    };
  },
};
</script>

<template>
  <Layout>
    <BRow>
      <BCol class="col-sm-12">
        <BCard no-body class="table-card">
          <BCardBody>
            <div class="text-end p-sm-4 pb-sm-2">
              <!-- Button to Trigger Export -->
              <BRow>
                <BCol sm="3" class="mt-3">
                  

                  <VueSelect
                    style="--vs-border-radius: 8px"
                    v-model="selectedBrand"
                    :options="formattedBrands"
                    placeholder="انتخاب برند"
                  />
                </BCol>
                <BCol style="margin-right: 180px" class="mt-3" sm="3">
                  <div class="form-group">
                    <DatePicker
                      format="YYYY/MM/DD HH:mm:ss"
                      type="date"
                      :range="true"
                      v-model="date"
                      @input="handleInput"
                    ></DatePicker>
                  </div>
                </BCol>
                <BCol sm="4" class="mt-3">
                  <button
                    @click="getFile"
                    type="button"
                    class="btn btn-primary"
                    :disabled="isLoading"
                  >
                    <span
                      v-if="isLoading"
                      class="spinner-border spinner-border-sm"
                      role="status"
                      aria-hidden="true"
                    ></span>
                    <span v-else>گرفتن خروجی</span>
                  </button>
                </BCol>
              </BRow>

              <!-- Product Selection Section -->
            </div>

            <div class="table-responsive">
              <table class="table table-hover tbl-product" id="pc-dt-simple">
                <thead>
                  <tr>
                    <th class="text-end">#</th>
                    <th>جزییات محصول</th>
                    <th>تاریخ ایحاد</th>
                    <th class="text-end">قیمت عمده</th>
                    <th class="text-end">قیمت تک</th>
                    <th class="text-end">تعداد سفارش</th>
                    <th class="text-end">تعداد ارسال شده</th>
                    <th class="text-center">عنوان برند</th>
                    <th class="text-center">تصویر برند</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="product in allProducts" :key="product?.id">
                    <td class="text-end">5</td>
                    <td>
                      <BRow>
                        <BCol class="col-auto pe-5">
                          <img
                            :src="product?.product?.image"
                            alt="user-image"
                            class="wid-40 rounded product-img"
                          />
                        </BCol>
                        <BCol>
                          <h6 class="mb-1">{{ product?.product?.title }}</h6>
                          <p class="text-muted f-12 mb-0">
                            {{ product.product.description.slice(0, 25)
                            }}{{
                              product.product.description.length > 25
                                ? "..."
                                : ""
                            }}
                          </p>
                        </BCol>
                      </BRow>
                    </td>
                    <td>{{ convertToJalali(product.created_at) }}</td>
                    <td
                      v-if="product?.product?.wholesale_price"
                      class="text-end"
                    >
                      {{
                        formatWithCommas(product?.product?.wholesale_price)
                      }}تومان
                    </td>
                    <td
                      v-if="!product?.product?.wholesale_price"
                      class="text-end"
                    >
                      <i
                        class="ph-duotone ph-x-circle text-danger f-24"
                        data-bs-toggle="tooltip"
                        data-bs-title="danger"
                      ></i>
                    </td>
                    <td v-if="product?.product?.retail_price" class="text-end">
                      {{ formatWithCommas(product?.product?.retail_price) }}
                    </td>
                    <td v-if="!product?.product?.retail_price" class="text-end">
                      <i
                        class="ph-duotone ph-x-circle text-danger f-24"
                        data-bs-toggle="tooltip"
                        data-bs-title="danger"
                      ></i>
                    </td>
                    <td class="text-end">{{ product.count }}</td>
                    <td class="text-end">{{ product.send_count }}</td>
                    <td class="text-center">
                      {{ product.product?.brand?.title }}
                    </td>
                    <td class="text-center">
                      <img
                        :src="product.product?.brand?.image"
                        alt="user-image"
                        class="wid-40 brand-img"
                      />
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </BCardBody>
        </BCard>
      </BCol>
    </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>
.product-img {
  max-width: 48px;
  min-width: 48px;
  max-height: 45px;
  min-height: 45px;
  object-fit: cover;
}
.brand-img {
  max-width: 43px;
  min-width: 43px;
  max-height: 40px;
  min-height: 40px;
  object-fit: cover;
}
.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;
}
</style>
