import { _decorator, Component, Node } from 'cc';
import JNLayerBase from '../../../../extensions/ngame/assets/ngame/ui/base/JNLayerBase';
import JNScrollView from '../../../../extensions/ngame/assets/ngame/util/components/scrollview/JNScrollView';
import { app, TD } from '../../App';
import GiftData, { GiftDataEnum } from '../../data/GiftData';
import { Sprite } from 'cc';
import { color } from 'cc';
import { EventTouch } from 'cc';
const { ccclass, property } = _decorator;

//商店页面
@ccclass('ShopView')
export class ShopView extends JNLayerBase {

    //商店礼包列表
    @property(JNScrollView)
    views:JNScrollView

    //商店标签
    @property(Node)
    tigs:Node;

    //页面下标
    index:number = 0;

    async onJNLoad(data?: any) {

        super.onJNLoad(data);

        await this.onUpdateView();

        //监听
        app.event.on(GiftDataEnum.BUY,this.onUpdateView,this)

    }

    onJNClose(): void {
        super.onJNClose();
        app.event.off(GiftDataEnum.BUY,this.onUpdateView,this)
    }

    async onUpdateView() {

        this.tigs.children.forEach((item,index) => {
            if(this.index == index){
                item.getComponentInChildren(Sprite).color = color(0,0,0);
            }else{
                item.getComponentInChildren(Sprite).color = color(255,255,255);
            }
        })

        this.views.refreshData(TD.TbGShop.getDataList().filter(item => item.type == this.index));

    }

    //切换下标
    onClickTigs(touch:EventTouch){
        this.index = touch.target.getSiblingIndex();
        this.onUpdateView();
    }

}


