import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { tools } from '../tools';
import { long_story_list_item } from './long_story_list_item';
import { long_story_list_item_data } from '../../data/data';
import { http } from '../http';
const { ccclass, property } = _decorator;

@ccclass('long_story_list')
export class long_story_list extends Component {
    @property(Node) btn_back:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item:Prefab = null;
    @property(Node) page_number:Node = null;
    @property(Node) btn_previous_page:Node = null;
    @property(Node) lab_current_page:Node = null;
    @property(Node) btn_next_page:Node = null;

    private m_back_call_back = null;
    private m_click_callback = null;

    private m_cur_page:number = 1;
    private m_every_page_count:number = 9;
    private m_total_page_count:number = 0;

    start() {
        this.btn_back.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_back_call_back) {
                this.m_back_call_back()
            }
        })
        this.page_number.active = false
        let self = this
        this.btn_previous_page.on(Node.EventType.TOUCH_END, ()=> {
            if(self.m_cur_page == 1) {
                tools.showToast("已是第一页")
                return
            }
            self.m_cur_page-=1
            self.requestData()
        })
        this.btn_next_page.on(Node.EventType.TOUCH_END, ()=> {
            if(self.m_cur_page >= self.m_total_page_count) {
                tools.showToast("已是最后一页")
                return
            }
            self.m_cur_page+=1
            self.requestData()
        })
    }

    init(back_callback, click_call_back) {
        this.m_back_call_back = back_callback
        this.m_click_callback = click_call_back
        this.requestData()
    }

    private requestData() {
        this.content.removeAllChildren()

        let request_data = {"page":this.m_cur_page,"limit":this.m_every_page_count}
        http.post("/tool/mysnote/book_list",request_data,(err,data)=>{
            if(!err){
                let _data = JSON.parse(data);
                // console.log('长篇列表=',_data)
                let __data =  _data.content
                if(this.m_cur_page == 1) {
                    this.page_number.active = true
                    this.m_total_page_count = Math.ceil(__data.tool_number/this.m_every_page_count)
                }
                this.setupLabNumber()
                this.initView(__data.list)
            }
        })
    }

    initView(list:long_story_list_item_data[]){
        for(let index = 0; index<list.length; index++) {
            let element = list[index]
            let node = instantiate(this.item)
            node.parent = this.content
            node.getComponent(long_story_list_item).init(element,this.m_click_callback)
        }
    }

    setupLabNumber() {
        let string = this.m_cur_page + ' / ' + this.m_total_page_count
        this.lab_current_page.getComponent(Label).string = string
    }
}

