import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { widget_item_data } from '../../../data/data';
import { widget_item } from '../widget_item';
const { ccclass, property } = _decorator;

@ccclass('select_widget_list')
export class select_widget_list extends Component {
    @property(Node) content:Node = null;
    @property(Node) btn_close:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    private call_back = null;
    public show(list:widget_item_data[],call,cur_select_id:number=-1){
        this.call_back = call;
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(widget_item).initHideDragOtherList()
            item.getComponent(widget_item).initView(element,this.onItemClick.bind(this),cur_select_id)
        }
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
        })

    }
    onItemClick(data:widget_item_data){
       if(this.call_back!=null){
            this.call_back(data)
       } 
       this.close()
    }

    close(){
        this.node.destroy()
    }
}


