const RankApi = require('../net/RankApi');
cc.Class({
    extends: cc.Component,

    properties: {

        content: cc.Node,
        myNode: cc.Node,

        allRankSpriteFrame: [cc.SpriteFrame],
        friendRankSpriteFrame: [cc.SpriteFrame],
        secondTabSpriteFrame: [cc.SpriteFrame],

        topDivider: cc.Node,

        allRankTab: cc.Sprite,
        friendRankTab: cc.Sprite,
        companyTab: cc.Sprite,
        artistTab: cc.Sprite,
        companyText: cc.RichText,
        artistText: cc.RichText,

        companySrcollView: cc.ScrollView,
        artistSrcollView: cc.ScrollView,

    },

    onLoad() {
        this.node.height = cc.view.getVisibleSize().height;
        this.contentY = this.content.y;
        this.content.y = this.content.height - cc.view.getVisibleSize().height;
        this.currentTab = 0;
        this.secondTab = 0;

        this.show();
    },

    onEnable() {
        this.scheduleOnce(() => {
            this.allRankSelected();
        }, 0.3);
    },

    allRankSelected() {

        // this.allRankTab.node.x = 480;
        // this.friendRankTab.node.x = 496;
        this.allRankTab.node.zIndex = 99;
        this.friendRankTab.node.zIndex = 0;
        this.allRankTab.spriteFrame = this.allRankSpriteFrame[1];
        this.friendRankTab.spriteFrame = this.friendRankSpriteFrame[0];
        this.currentTab = 0;
        this.data = null
        this.companyTabSelected();
        this.getData();
    },

    friendRankSelected() {
        // this.allRankTab.node.x = 496;
        // this.friendRankTab.node.x = 480;
        this.allRankTab.node.zIndex = 0;
        this.friendRankTab.node.zIndex = 99;
        this.allRankTab.spriteFrame = this.allRankSpriteFrame[0];
        this.friendRankTab.spriteFrame = this.friendRankSpriteFrame[1];
        this.currentTab = 1;
        this.data = null
        this.companyTabSelected();
        this.getData();
    },

    companyTabSelected() {
        this.secondTab = 0;
        this.topDivider.x = -140;
        this.companyTab.spriteFrame = this.secondTabSpriteFrame[1];
        this.artistTab.spriteFrame = this.secondTabSpriteFrame[0];
        this.setSelectedText(this.companyText, '经济公司');
        this.setDefaultText(this.artistText, '艺人');
        this.companySrcollView.node.active = true;
        this.artistSrcollView.node.active = false;
    },

    artistTabSelected() {
        this.secondTab = 1;
        this.topDivider.x = 140;
        this.companyTab.spriteFrame = this.secondTabSpriteFrame[0];
        this.artistTab.spriteFrame = this.secondTabSpriteFrame[1];
        this.setDefaultText(this.companyText, '经济公司');
        this.setSelectedText(this.artistText, '艺人');
        this.companySrcollView.node.active = false;
        this.artistSrcollView.node.active = true;
    },

    setDefaultText(richText, message) {
        richText.node.color = new cc.Color(161, 105, 33);
        richText.string = `<outline color=#A16921 width=1><b>${message}<b></outline>`
    },

    setSelectedText(richText, message) {
        richText.node.color = new cc.Color(255, 244, 222);
        richText.string = `<outline color=#c47005 width=2><b>${message}<b></outline>`
    },

    show() {
        this.content.y = -cc.view.getVisibleSize().height;
        let s = cc.sequence(cc.moveTo(0.2, 0, this.contentY + 20).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
        this.content.runAction(s);
    },

    close() {
        if (this.node && this.node.parent) {
            let self = this;
            let finish = cc.callFunc(() => {
                self.node.destroy();
            }, this)
            this.content.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
        }
    },

    getData() {
        let promise = null;
        this.companySrcollView.getComponent('RankList').setLoading();
        if (this.currentTab === 0) {
            promise = RankApi.getAllRankPromise();
        } else {
            promise = RankApi.getFriendRankPromise();
        }

        promise.then(res => {
            this.data = res.data;
            this.companySrcollView.getComponent('RankList').bindData(this.data.companyRank);
            this.artistSrcollView.getComponent('RankList').bindData(this.data.artistRank);
        }).catch((code, msg) => {
            console.log(code)
        });
    },

    // bindData(pageData) {
    //     pageData.ranks.forEach(item => {
    //         item.type = this.secondTab;
    //     });
    //     this.srcollView.stopAutoScroll();
    //     this.srcollView.scrollToTop(0);
    //     this.myScript.setType(this.secondTab);
    //     this.myScript.bindMySelf(pageData.me);
    //     this.rankListScript.bindData(pageData.ranks);
    // },
});
