<template>
	<view class="bookdetails-content" v-if="book_data.book_id">
		<bookInfo :book_data='book_data'></bookInfo>
		<bookBrief :book_data='book_data'></bookBrief>
		<chapterCatalog :book_data='book_data' @clickReadChapter='clickReadChapter'></chapterCatalog>
		<recommend :book_list='recommend_book_list' @clickChangeBook='clickChangeBook'></recommend>
		<view class="bottom-placeholder" :style="{height: bottom_all_height+'px'}"></view>
		<bottom 
		:height='bottom_height' 
		:all_height='bottom_all_height' 
		:is_on_bookshelf='is_on_bookshelf'
		@clickReadBook='bottomClickReadBook'
		@clickBookshelf='bottomClickBookshelf'
		></bottom>
	</view>	
</template>

<script setup lang="ts">
	import bookInfo from '../bookdetails/bookdetails-bookInfo.vue'
	import bookBrief from '../bookdetails/bookdetails-bookBrief.vue'
	import chapterCatalog from '../bookdetails/bookdetails-chapterCatalog.vue'
	import recommend from '../bookdetails/bookdetails-recommend.vue'
	import bottom from '../bookdetails/bookdetails-bottom.vue'
	
    import { ref } from 'vue';
    import { config } from '../../config/config';
    import { http } from '../../framework/http';
    import { book_item_data } from '../../data/data';
    import { util } from '../../framework/util';
    import { tools } from '../../framework/tools';
    import { log } from '../../framework/log';
	
	let book_data = ref(new book_item_data())
	let is_on_bookshelf = ref(false)
	let is_show_bottom_tools = ref(false)
	let bottom_height = ref(80)
	let bottom_all_height = ref(bottom_height.value+uni.getSystemInfoSync().safeAreaInsets.bottom)
	
	// 废弃
	// import { getCurrentInstance, onMounted } from 'vue';
	// onMounted(() => {
		// const instance = getCurrentInstance().proxy
	    // const eventChannel = instance.getOpenerEventChannel()
	    // eventChannel.on('data', function(data:any) {
		// 	log.Debug('书详情id==>',data.book_id)
	    //   requestBookdetails(data.book_id)
	    // })
		// initEvent()
	// })
	
	let data = tools.value_gotoBookdetails_data
	requestBookdetails(data.book_id)
	initEvent()
	
	function initEvent() {
		uni.$on(config.EVENT_TYPE.UPDATE_BOOKSHELF, ()=>{
			tools.checkBookOnBookshelf(book_data.value.book_id, (is_on:boolean)=>{
				is_on_bookshelf.value  = is_on
			})
		})		
	}

	function requestBookdetails(book_id:number) {
		util.showLoading()
		let url = config.url_confg.Static.book_details(book_id)
		http.StaticRequest(url,(err=null,data=null)=>{
			util.hideLoading()
			if(!err&&data) {
				if(data.code==config.url_confg.StatesCode.SUCCESS){
					book_data.value = data.content
					// console.log(book_data.value.chapter_time_time)					
					tools.checkBookOnBookshelf(book_data.value.book_id, (is_on:boolean)=>{
						is_on_bookshelf.value  = is_on
						is_show_bottom_tools.value = true
					})
					
					requestBookCategoryRecommend()
				}
			}
		})
	}
	
	let recommend_book_list = ref([])
	function requestBookCategoryRecommend() {
		let url = config.url_confg.Dynamic.book_category_recommend
		let opt = {'category_id': book_data.value.category_id}
		http.DynamicRequest(url,opt,(err=null,data=null)=>{
			if(!err&&data) {
				if(data.code==config.url_confg.StatesCode.SUCCESS){
					recommend_book_list.value = data.content
				}
			}
		})
	}
	
	function clickReadChapter(chapter_id:number) {
		tools.gotoRead(book_data.value, chapter_id)
	}
	
	function clickChangeBook() {
		requestBookCategoryRecommend()
	}
		
	function bottomClickReadBook() {
		tools.gotoRead(book_data.value)
	}
	
	function bottomClickBookshelf() {
		if(is_on_bookshelf.value) {
			tools.deleteBookshelf([book_data.value.book_id])
		} else {
			tools.addBookshelf(book_data.value)
		}
	}
</script>

<style lang="scss">
	.bookdetails-content{
		
	}
</style>
