
import List, { ListType } from "./List";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemRender extends cc.Component {
    /**数据 */
    public data:any = null;
    /**索引 0表示第一项*/
    public itemIndex:number = 0;

    /**数据改变时调用 */
    public dataChanged(){
        let name = cc.fx.GameTool.subName(this.data.name,6);
        this.node.getChildByName("rankLab").getComponent(cc.Label).string = this.data.rank + "";
        this.node.getChildByName("nameLab").getComponent(cc.Label).string = name + "";
        this.node.getChildByName("totalLab").getComponent(cc.Label).string = this.data.total + "";
        this.node.getChildByName("rank").getChildByName("one").active = false;
        this.node.getChildByName("rank").getChildByName("two").active = false;
        this.node.getChildByName("rank").getChildByName("three").active = false;
        if(this.data.rank == 1){
            this.node.getChildByName("rank").getChildByName("one").active = true;
            this.node.getChildByName("rankLab").active = false;
        }
        else if(this.data.rank == 2){
            this.node.getChildByName("rank").getChildByName("two").active = true;
            this.node.getChildByName("rankLab").active = false;
        }
        else if(this.data.rank == 3){
            this.node.getChildByName("rank").getChildByName("three").active = true;
            this.node.getChildByName("rankLab").active = false;
        }else{
            this.node.getChildByName("rankLab").active = true;
        }
        this.setPic();
    }   
    
    public setPic(){
        this.node.getChildByName("pic").getChildByName("icon").active = false;
        this.node.getChildByName("pic").getChildByName("pic").active= false;
        var self = this;
        let url = this.data.pic;
        fetch(url)
        .then(response => {
            return response.headers.get('Content-Length');
        })

        .then(errNo => {
            // console.log(this.data.rank,'X-Info:', errNo); // 输出X-ErrNo的值
            if(errNo == "5093"){
                // console.log(this.data.rank,"没头像");
                this.node.getChildByName("pic").getChildByName("icon").active = true;
            }
        })
        .catch(error => {
            // console.error('Error fetching X-Info:', error);
        });
        cc.assetManager.loadRemote(url,{ext:'.png'},(err, texture:cc.Texture2D) => {
            if(texture){
                this.node.getChildByName("pic").getChildByName("pic").active= true;
                var sprite = this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite);
                sprite.spriteFrame = new cc.SpriteFrame(texture);
                // console.log(this.data.rank,"设置头像成功",err);
            }
            else{
                // console.log("设置头像失败",url);
                console.log(err,texture)
            }
        })
    }


}
