
class BuildViewsEvent {
    getCondition(eventSet){ // 获取显示隐藏的条件展示
        let condition = "";
        if(eventSet.eventRes == 'display'){
            let setting = eventSet.setting;
            condition += "if( ";
            Object.keys(setting).forEach(field => {
                if(field != "display"){
                    let fieldSet = setting[field] || {};
                    condition += (fieldSet["bracketsBeg"] || "") + "formData['" + field + "'] ";
                    if(fieldSet["operator"] == "contain"){
                        condition += ".indexOf('" + fieldSet["value"] + "') > -1 ";
                    }else{
                        condition +=  fieldSet["operator"] + " '" +fieldSet["value"] + "' ";
                    }
                    condition += (fieldSet["bracketsEnd"] || "") + " " +(fieldSet["linkOperator"] || "") + " ";
                }
            });
            let display = setting["display"] == "show";
            condition += " ) {\n    sopt." + (display ? "showTrsByField" : "hideFields") + "('"
                + eventSet.eventEffect.join(",") + "');\n} else {\n    sopt." +
                (display ? "hideFields" : "showTrsByField") + "('" + eventSet.eventEffect.join(",") +"');\n}";
        }
        return condition;
    }
    /**
     * 根据类型生成事件的内容
     */
    buildFunc(eventSet){
        if(!eventSet){
            return eventSet;
        }
        let func = "";
        let settingObj = eventSet.setting;
        let arr = [];
        switch (eventSet.eventRes){
            case "caculate":
                func += "let result = 0;\nresult += ";
                (eventSet.eventCol || []).forEach(item => {
                    if(settingObj[item] || settingObj[item] == "0"){
                        arr.push("Number(formData['" + item + "']) * " + Number(settingObj[item]));
                    }else{
                        arr.push("Number(formData['" + item + "']) * 1");
                    }
                });
                func += arr.join(" + ");
                (eventSet.eventEffect || []).forEach(item => {
                    func += ";\nformData['"+item+"'] = result";
                });
                func += ";";
                break;
            case "display":
                func = this.getCondition(eventSet);
                break;
            default:
                break;
        }
        eventSet.funcBody = func;
        return eventSet;
    }
}
export { BuildViewsEvent }