// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        bg:cc.Node,
        content:cc.Node,
        button:cc.Node,
        cardLabel:cc.Label,
        close:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        
    },

    onEnable(){
        wx.showLoading();
        wx.request({
            url:'https://api-touhu.duowan.com/user/getUserRecord.do',
            data:{
                token:Global.user.token,
                uid:Global.user.uid,
                channel:Global.channel,
                os:Global.os,
                ver:Global.ver,
            },
            success:(response)=>{
                if(response.data.code==-5){
                    wx.showToast({
                        title:'登录过期'
                    })
                    Global.showLoginPage = true;
                    cc.director.loadScene('MainMenu');
                }else{
                    if(response.data.data.revivedCount!=undefined){
                        Global.user.revivedCount = response.data.data.revivedCount;
                    }
                    this.bindData();
                }
            },
            fail:()=>{
                wx.showToast({title:'网络错误'})
            },
            complete:()=>{
                wx.hideLoading();
            }
        })

    },

    bindData(){
        this.bg.on('touchend',  ()=> {
            this.node.active = false;
        });
        this.content.on('touchend',  ()=> {});
        this.close.on('touchend', ()=> {this.node.active = false;})
        this.cardLabel.string = Global.user.revivedCount;
    }
    // update (dt) {},
});
