import { _decorator, Component, EditBox, Node } from 'cc';
import { tools } from './tools';
const { ccclass, property } = _decorator;

@ccclass('dialog_input')
export class dialog_input extends Component {
    @property(EditBox) editBox:EditBox = null;
    @property(Node) btn_cancel:Node = null;
    @property(Node) btn_sure:Node = null;
    private m_sure_call = null;
    private m_cancel_call = null;
    start() {
        this.editBox.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=> {
        })
        this.btn_cancel.on(Node.EventType.TOUCH_END, ()=>{
            if(this.m_cancel_call!=null){
                this.m_cancel_call()
            }
            this.close()
        })
        this.btn_sure.on(Node.EventType.TOUCH_END, ()=>{
            let string = tools.trimLeftAndRightblank(this.editBox.string)
            if(string.length==0) {
                return tools.showToast('内容不能是空的')
            }
            if(this.m_sure_call!=null){
                this.m_sure_call(string)
            }
            this.close()
        })
    }

    public show(content:string,sure_call,cancel_call){
        this.m_sure_call = sure_call;
        this.m_cancel_call = cancel_call;
        this.editBox.string = content;
    }

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

}

