
var UserInformationApi = require('../net/UserInformationApi');
var {UserInformationType} = require('../utils/GameEnum');

cc.Class({
    extends: cc.Component,

    properties: {
        emptyNode: cc.Node,

        scrollViewNode: cc.ScrollView,

        timelinePrefab: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    init() {

    },

    onLoad () {

    },

    onDisable() {
        for (let child of this.scrollViewNode.content.children) {
            if (cc.isValid(child)) {
                child.destroy();
            }
        }
        this.scrollViewNode.scrollToTop();
    },

    start () {

    },

    loadUserTimeline(uid) {
        this.uid = uid;
        this.getNetworkData();
    },

    //获取个人信息
    getNetworkData() {
        UserInformationApi.getTimelineList(this.uid, (responseData)=> {
            // console.log("timeline responseData: " + JSON.stringify(responseData));
            this.configData(responseData);
        }, (error) => {
            console.log('error' + error);
            this.loadDataError();
        });
    },

    refreshThemeUI(infoTheme) {
        this.infoTheme = infoTheme;
        // this.loadItemData();
    },

    configData(responseData) {

        for (let child of this.scrollViewNode.content.children) {
            if (cc.isValid(child)) {
                child.destroy();
            }
        }

        this.dataList = responseData.list;
        if (this.dataList == undefined || this.dataList.length == 0) {
            this.emptyNode.active = true;
            this.scrollViewNode.node.active = false;
        } else {
            this.emptyNode.active = false;
            this.scrollViewNode.node.active = true;
            this.loadItemData()
        }
    },

    loadDataError() {
        for (let child of this.scrollViewNode.content.children) {
            if (cc.isValid(child)) {
                child.destroy();
            }
        }

        this.emptyNode.active = true;
        this.scrollViewNode.node.active = false;
    },

    loadItemData() {
        for (var i = 0; i < this.dataList.length; i++) {
            var itemNode = cc.instantiate(this.timelinePrefab);
            itemNode.parent = this.scrollViewNode.content;
            let itemMng = itemNode.getComponent('UserInformationTimelineItem');
            let itemModel = this.dataList[i];
            itemMng.configData(itemModel);
        }
    }

    // update (dt) {},
});
