<template>
  <div>
    <div class="header">
      <el-button type="primary" link class="back">返回</el-button>
      微方志
    </div>
    <BookReader :pages="pages" :bookmarks="bookmarks" />
  </div>
</template>
<script setup>
import { getDocumentDetail } from "@/api";
import BookReader from "@/components/BookReader/index.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>
.header {
  background-color: #444;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  height: 46px;
  padding: 0 20px;
  .back {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>
