import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { att_question_select, ui_att_item } from '../../../data/data';
import { question_btn_info } from './question_btn_info';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('attributes_question_select')
export class attributes_question_select extends Component {
    @property(Node) content:Node = null;
    @property(Prefab) itemPb:Prefab = null;
    @property(Node) btn_add:Node = null;
    private call_back = null;
    private m_data:att_question_select = null;
    public initView(call){
        this.call_back = call;
        this.btn_add.on(Node.EventType.TOUCH_END,()=>{
            this.addBtn()
        })
    }

    change(){
        if(this.call_back!=null){
            this.call_back(this.m_data)
        }
    }

    addBtn(){
        let item = instantiate(this.itemPb)
        item.parent = this.content;
        let data = new ui_att_item
        data.width = 357;
        data.height = 120;
        data.x = 100;
        data.y = 100;
        this.m_data.btn_question_list.push(data)
        this.update_att(this.m_data)
        this.change()
    }

    onItemDelete(index:number){
        this.m_data.btn_question_list.splice(index,1)
        this.change()
    }

    public update_att(data:att_question_select){
        this.m_data = data;
        this.content.removeAllChildren()
        if(this.content.children.length===this.m_data.btn_question_list.length){
            for (let index = 0; index < this.content.children.length; index++) {
                const element = this.content.children[index];
                element.getComponent(question_btn_info).initView(this.m_data.btn_question_list[index],index,this.onItemDelete.bind(this),config.attributes_list_type.question_select)
            }
        }else{
            for (let index = 0; index < this.m_data.btn_question_list.length; index++) {
                let item = instantiate(this.itemPb)
                item.parent = this.content
                item.getComponent(question_btn_info).initView(this.m_data.btn_question_list[index],index,this.onItemDelete.bind(this),config.attributes_list_type.question_select)
            }
        }
   
    }
}


