import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { dai_dao_ju_item, guo_ju_qing_dai_dao_ju } from '../../../data/data';
import { tools } from '../../tools';
import { select_task_dai_dao_ju_item } from './select_task_dai_dao_ju_item';
const { ccclass, property } = _decorator;

@ccclass('select_task_dai_dao_ju')
export class select_task_dai_dao_ju extends Component {
    @property(Node) btn_close:Node = null;
    @property(Node) btn_add:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    private m_data:guo_ju_qing_dai_dao_ju = null;
    public show(data:guo_ju_qing_dai_dao_ju){
        this.m_data = data;
        this.btn_add.on(Node.EventType.TOUCH_END,()=>{
            this.addItem()
        })
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
        })

        this.content.removeAllChildren()
        for (let index = 0; index < this.m_data.widget_list.length; index++) {
            const element:dai_dao_ju_item = this.m_data.widget_list[index];
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(select_task_dai_dao_ju_item).initView(element,this.onItemDelete.bind(this),index)
        }
    }
    onItemDelete(item:select_task_dai_dao_ju_item){
        tools.show_dialog("是否删除？",()=>{
            item.node.removeFromParent()
            this.m_data.widget_list.splice(item.getIndex(),1)
        })
    }
    
    close(){
        this.node.active = false;
    }

    addItem(){
        const element:dai_dao_ju_item = new dai_dao_ju_item
        this.m_data.widget_list.push(element)
        let index = this.m_data.widget_list.length-1;
        let item = instantiate(this.item_prefab)
        item.parent = this.content;
        item.getComponent(select_task_dai_dao_ju_item).initView(element,this.onItemDelete.bind(this),index)
    }
}


