var items = {
    name: 'main',
    type: 'FormGridLayout',
    property: { margin: 18, spacing: 20, columns: 3 },
    pack: {},
    child: [
        {
            name: "num1",
            type: "LineEdit",
            pack: { label: "参数1", column_span: 1, label_alignment:"Left" },
            validate: function(obj,val,title,moment,self){
                if(moment === "EDIT"){
                    // return [title+'必须小于10','Error']
                    var reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
                    if(!reg.test(val)){
                        GUI.msgbox({
                            title: '错误提示',
                            detail: '请输入数字或小数'
                        })
                        this.getObject("num1").setData("value", 0);
                    }
                }
                return
            }
        },
        {
            name: "operator",
            type: "ComboBox",
            property: {
                item_list: ["+","-","*","/","%"],
                value: "+",
            },
            pack: { label: "操作", column_span: 1, label_alignment:"Left"  },
            validate: function(obj,val,title,moment,self){
                if(moment === "EDIT"){
                    // return [title+'必须小于10','Error']
                    if(val == "") {
                         GUI.msgbox({
                            title: '错误提示',
                            detail: '请输入符号'
                        })
                    }
                }
                return
            }
        },
        {
            name: "num2",
            type: "LineEdit",
            pack: { label: "参数2", column_span: 1, label_alignment:"Left"  },
            validate: function(obj,val,title,moment,self){
                if(moment === "EDIT"){
                    // return [title+'必须小于10','Error']
                    var reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
                    if(!reg.test(val)){
                        GUI.msgbox({
                            title: '错误提示',
                            detail: '请输入数字或小数'
                        })
                        this.getObject("num2").setData("value", 0);
                    }
                }
                return
            }
        },
        {
            type: "ToolButton",
            property: {
                text: "计算"
            },
            pack: { column_span: 1, label_alignment:"Left" },
            callback: function(obj, check, self) {
                var tempData = this.getAllValues();
                var num1 = tempData.num1
                var num1len = getNumFixed(num1)
                var num2 = tempData.num2
                var num2len = getNumFixed(num2)
                var temp = num1len >= num2len ? num1len : num2len
                temp = Number("1" + Array(temp+1).join(0))
                if(!num1 || !num2){
                    GUI.msgbox({
                        title: 'err',
                        detail: "缺少参数"
                    })
                    return
                }
                operator = tempData.operator
                if(operator == ""){
                    GUI.msgbox({
                        title: 'err',
                        detail: "缺少符号"
                    })
                }
                var res = ""
                switch (operator) {
                    case "+":
                        res = (Number(num1)*temp + Number(num2)*temp)/temp
                    break;
                    case "-":
                        res = (Number(num1)*temp - Number(num2)*temp)/temp
                    break;
                    case "*":
                        res = (Number(num1)*temp * Number(num2)*temp)/temp/temp
                    break;
                    case "/":
                        res = ((Number(num1)*temp) / (Number(num2)*temp))
                    break;
                    case "%":
                        res = (Number(num1)*temp % Number(num2)*temp)/temp
                    break;
                    default:
                        break;
                }
                this.getObject("res").setData("value", String(res));
            }
        },
        {
            name: "res",
            type: "label",
            pack: { label: "结果", column_span: 2, label_alignment:"Left"  },
        },
        {
            type: 'stretch'
        }
    ]
}

var box = {
    title: "简易计算器",
    size: "600x400",
    items: items,
    values: {},
    self: this
}

var data = GUI.showForm(box)


function getNumFixed(num){
    if(!/\./g.test(num)){ 
        return 0
    } else {
        var res = String(num).split(".")[1]
        return removeZero(res).length
    }
}

function removeZero(str){
    var s = str[str.length-1]
    if(s == "0"){
        return removeZero(str.substring(0, str.length-1)) 
    } 
    return str
}