<script setup>
import { ref, reactive, watch } from "vue";
import axios from "axios";
import moment from "moment";
import Map from "@/components/Map.vue";
import Navbar from "@/components/Navbar.vue";

let pageNum = ref(1); // 頁數(預設第一頁)
let pageAmount = ref(4); // 每頁顯示筆數
let totalPages = ref(1); // 總頁數

// 取得所有課程總筆數
(async () => {
  try {
    const response = await axios.get("https://cmm.ai:8088/api/get_class_name");
    totalPages.value = getTotalPages(response.data.classes.length, 4);
  } catch (error) {
    console.error(error);
  }
})();

// 計算頁數
function getTotalPages(totalRecords, recordsPerPage) {
  return Math.ceil(totalRecords / recordsPerPage);
}

// 切換分頁
watch(pageNum, () => {
  getClass();
});

let classes = reactive({
  data: [],
});

let assignState = ref(false);
let assignLocationId = ref(null);

async function getClass() {
  let url = `https://cmm.ai:8088/api/get_class_name?page_num=${pageNum.value}&page_amount=${pageAmount.value}`;
  let assignUrl = `https://cmm.ai:8088/api/get_class_name?page_num=${pageNum.value}&page_amount=${pageAmount.value}&location_id=${assignLocationId.value}`;

  try {
    const response = await axios.get(assignState.value ? assignUrl : url);
    console.log("response", response.data);
    classes.data = response.data.classes;
  } catch (error) {
    console.error(error);
  }
}

getClass();

// 取得特定據點課程總筆數
const getClassList = async (locationId) => {
  console.log("locationId", locationId);
  assignState.value = true;
  assignLocationId.value = locationId;

  try {
    const response = await axios.get(
      `https://cmm.ai:8088/api/get_class_name?location_id=${locationId}`
    );
    getClass();
    pageNum.value = 1;
    totalPages.value = getTotalPages(response.data.classes.length, 4);
  } catch (error) {
    console.log("error", error);
  }
};
</script>

<template>
  <div class="banner">
    <img src="@/assets/img/home/banner.png" alt="" class="cover" />
    <img src="@/assets/img/home/logo.png" alt="" class="logo" />
  </div>
  <Navbar />

  <v-container class="pa-0 pt-16">
    <v-row>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-12.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-11.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-10.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-09.png" alt="" />
      </v-col>
      <v-col cols="4" class="d-flex justify-center align-center">
        <h3 class="title">八大工藝學群</h3>
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-05.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-06.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-07.png" alt="" />
      </v-col>
      <v-col cols="4">
        <img src="@/assets/img/home/首頁元素-08.png" alt="" />
      </v-col>
    </v-row>

    <div class="map-block">
      <h3 class="mb-10 title">全台工藝地圖</h3>
      <div class="v-row">
        <v-col cols="7">
          <div class="map">
            <Map @locationId="getClassList" />
          </div>
        </v-col>
        <v-col cols="5">
          <v-list lines="three" class="list pa-0">
            <v-list-item v-for="item in classes.data" :key="item.id">
              <div class="d-flex align-center">
                <img :src="item.cover_img" alt="" />
                <section>
                  <h2>{{ item.name }}</h2>
                  <p>主辦單位：{{ item.organizer }}</p>
                  <p>據點名稱：{{ item.school }}</p>
                </section>
              </div>
            </v-list-item>
          </v-list>
          <v-pagination
            v-model="pageNum"
            class="my-4"
            :length="totalPages"
          ></v-pagination>
        </v-col>
      </div>
      <!-- <div class="map">
        <Map @locationId="getClassList" />
      </div> -->
    </div>
  </v-container>
</template>

<style lang="scss" scoped>
.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  .cover {
    width: 100vw;
    height: 100vh;
    object-fit: cover;
  }
  .logo {
    width: 800px;
    position: absolute;
    z-index: 10;
  }
}

.title {
  font-size: 26px;
  font-weight: 400;
  letter-spacing: 2px;
  text-align: center;
}

.map-block {
  margin: 100px auto;
  .map {
    height: 100vh;
  }

  .list {
    img {
      width: 200px;
      height: 175px;
      margin-right: 20px;
      border-radius: 5px;
      box-shadow: 2px 2px 4px #aaaaaa;
    }

    h2 {
      font-size: 18px;
      margin-bottom: 20px;
    }

    h2,
    p {
      line-height: 28px;
      // 超過兩行則省略
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      line-break: after-white-space;
    }

    .v-list-item {
      height: 215px;
      padding: 20px 0;
      border-bottom: 1px solid #cccccc;

      &:first-child {
        padding: 0 0 20px;
      }

      &:last-child {
        border-bottom: none;
      }

      .v-list-item__content {
        overflow: initial !important;
      }
    }
  }
}
</style>
