<script>
import Layout from "@/layout/custom.vue";
import ApiServiece from "@/services/ApiService";
import { onMounted, ref, watch, computed } from "vue";

import moment from "jalali-moment";
export default {
  name: "PRODUCT-LIST",
  components: {
    Layout,
  },
  setup() {
    const searchPage = ref();
    const currentPage = ref(1);
    const totalPages = ref(1);
    const paginate = ref(20);
    const page = ref(1);
    const filterLoading = ref(false);
    const allOrders = ref([]);
    const getAllOrders = () => {
      filterLoading.value = true;
      ApiServiece.get(
        `admin/orders?paginate=${paginate.value || 10}&page=${page.value || 1}`
      )
        .then((resp) => {
          console.log(resp);
          allOrders.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 convertToJalali = (date) => {
      return moment(date, "YYYY-MM-DD HH:mm:ss")
        .locale("fa")
        .format("YYYY/MM/DD");
    };

    const getStatusLabel = (status) => {
      const statusLabels = {
        waiting: "در انتظار",
        paid: "پرداخت‌شده",
        un_paid: "پرداخت‌نشده",
        approved: "تأیید‌شده",
        processing: "در حال پردازش",
        shipping: "در حال ارسال",
        delivered: "تحویل‌شده",
        canceled: "لغو‌شده",
      };
      return statusLabels[status] || "نامشخص";
    };





    

    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(page, () => {
      getAllOrders();
    });

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

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

    onMounted(() => {
      getAllOrders();

    });
    return {
      allOrders,
      visiblePages,
      nextPage,
      prevPage,
      handlePageInput,
      currentPage,
      totalPages,
      page,
      convertToJalali,
      searchPage,
      getStatusLabel
    };
  },
};
</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 -->
            

            
            </div>

            <div class="table-responsive">
              <table class="table table-hover tbl-product" id="pc-dt-simple">
                <thead>
                  <tr>
                    <th >شناسه</th>
                    <th>تاریخ ایحاد</th>
                    <th>کد رهگیری</th>
                    <th>وضعیت</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="order in allOrders" :key="order?.id">
                    <td >{{ order?.id }}</td>
                    <td>{{ convertToJalali(order?.created_at) }}</td>
                    <td>{{order?.tracking_code}}</td>
                    <td >{{ order?.status }}</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>
