const fetch = require('node-fetch');

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);

        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': 'PHPSESSID=rptu41at2grj858grbugvag47c; keeplogin=61920%7C604800%7C1735033225%7C7847606df9197c965142dee641ef190c'
            }
        });

        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.genre = 3;
        }
        return info;
    } catch (error) {
        return null;
    }
}

module.exports = { search_id }