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

@ccclass('level_list')
export class level_list extends Component {

    @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_click_callback = null;

    private m_cur_page:number = 1;
    private m_every_page_count:number = 18;
    private m_total_page_count:number = 0;
    private m_prefab_node_list:Node[] = [];

    protected start(): void {
        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)
        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()
        },this)
    }

    public init(call_back){
        this.m_click_callback = call_back;
        this.requestData()
    }

    initView(list:level_list_item_data[]){
        if(this.m_prefab_node_list.length == 0) {
            this.content.removeAllChildren()
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                let node = instantiate(this.item)
                node.parent = this.content;
                this.m_prefab_node_list.push(node)
                node.getComponent(level_list_item).initView(element,this.m_click_callback)
            }
        } else {
            for(let index = 0; index < this.m_prefab_node_list.length; index++) {
                let node = this.m_prefab_node_list[index]
                if(index < list.length) {
                    node.active = true
                    const element = list[index]
                    node.getComponent(level_list_item).initView(element,this.m_click_callback)
                } else {
                    node.active = false
                }
            }
        }
    }

    requestData() {
        let request_data = {"page":this.m_cur_page,"limit":this.m_every_page_count}
        http.post("/tool/mysnote/levels",request_data,(err,data)=>{
            if(!err){
                // console.log(data)
                let _data = JSON.parse(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()
                config.last_id = parseInt(_data.content.zujian_id)
                console.log("level_list",config.last_id )
                this.initView(__data.list)
            }
        })
    }

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


