import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { scene_item_data, zhao_xi_jie_data, zhao_xi_jie_item_data } from '../../../data/data';
import { select_task_zhao_xi_jie_item } from './select_task_zhao_xi_jie_item';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('select_task_zhao_xi_jie')
export class select_task_zhao_xi_jie 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:zhao_xi_jie_data = null;
    public show(data:zhao_xi_jie_data){
        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_id_list.length; index++) {
            const element:zhao_xi_jie_item_data = this.m_data.widget_id_list[index];
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(select_task_zhao_xi_jie_item).initView(element,this.onItemDelete.bind(this),index)
        }
    }

    onItemDelete(item:select_task_zhao_xi_jie_item){
        tools.show_dialog("是否删除？",()=>{
            item.node.removeFromParent()
            this.m_data.widget_id_list.splice(item.getIndex(),1)
        })
    }
    
    close(){
        this.node.active = false;
    }

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


 