<template>
  <div>
    <van-nav-bar :title="title" left-text="返回" left-arrow @click-left="onClickLeft" />
    <BookReader :pages="pages" :bookmarks="bookmarks" />
  </div>
</template>
<script setup>
import { getDocumentDetail } from "@/api";
import BookReader from "@/components/BookReader.vue";
const route = useRoute();
const router = useRouter();
/* props */
/* emit */
/* data */
const title = ref('')
const pages = ref([]);
const bookmarks = ref([]);
/* computed */
/* methods */
async function loadData(id) {
  const res = await getDocumentDetail(id);
  pages.value = res.map((item) => {
    return {
      ...item,
      page_url: import.meta.env.VITE_API_BASE_URL + "/static/" + item.page_url,
      images: item.images.map((img) => {
        return {
          ...img,
          url: import.meta.env.VITE_API_BASE_URL + "/static/" + img.url,
        };
      }),
    };
  });
}

function onClickLeft(){
  router.go(-1)
}
onMounted(() => {
  const { id } = route.params;
  bookmarks.value = JSON.parse(localStorage.getItem(`bookmarks-${id}`));
  title.value = route.query.title
  loadData(id);
});
</script>
<style lang="scss" scoped></style>
