const fetch = require('node-fetch');
const tools = require('../../../tools')
async function search_id(book_id) {
    try {
        // 构建查询参数
        const filter = JSON.stringify({
            type_two: null,
            type_three: null,
            id: book_id
        });
        
        const op = JSON.stringify({
            type_two: "=",
            type_three: "=",
            id: "="
        });

        // 构建URL with params
        const url = new URL('https://admin.wqxsw.com/admin/bookshort/book/channelindex');
        url.searchParams.append('sort', 'id');
        url.searchParams.append('order', 'desc');
        url.searchParams.append('offset', '0');
        url.searchParams.append('limit', '10');
        url.searchParams.append('filter', filter);
        url.searchParams.append('op', op);

        let cookit = await tools.getDzCookit()
        const response = await fetch(url.toString(), {
            method: 'GET',
            headers: {
                'accept': 'application/json, text/javascript, */*; q=0.01',
                'accept-language': 'zh-CN,zh;q=0.9',
                'content-type': 'application/json',
                'x-requested-with': 'XMLHttpRequest',
                'cookie': `${cookit}`
            }
        });

        const data = await response.json();
    
        let info = {

        }
        if(data.total<=0){
            throw data  
        }else{
            info.words =  data['rows'][0].totalWordSize
            info.book_name =  data['rows'][0].bookName
            info.book_id =  data['rows'][0].bookId
            info.totalChapterNum = data['rows'][0].totalChapterNum
            info.genre = 3;
            info.publish_time = data['rows'][0].ctimePack
        }
        return info;
    } catch (error) {
        console.log("error:",error,book_id)
        return null;
    }
}

module.exports = { search_id }