package cn.cerc.ui.vcl;

import java.util.HashMap;
import java.util.Map;

import cn.cerc.ui.core.Component;
import cn.cerc.ui.core.HtmlWriter;
import cn.cerc.ui.core.UIOriginComponent;
import cn.cerc.ui.parts.UIComponent;

public class UIForm extends UIOriginComponent {
    private String action;
    private String method = "post";
    private Map<String, String> items = new HashMap<>();
    private String enctype;
    private UIComponent top;
    private UIComponent bottom;

    public UIForm() {
        super(null);
    }

    public UIForm(UIComponent owner) {
        super(owner);
    }

    @Deprecated
    public UIForm(UIComponent owner, String id) {
        super(owner);
        this.setId(id);
    }

    @Override
    @Deprecated
    public void setOwner(Component owner) {
        super.setOwner(owner);
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    @Override
    public void output(HtmlWriter html) {
        outHead(html);
        if (top != null) {
            html.print("<div role='top'>");
            top.output(html);
            html.println("</div>");
        }
        super.output(html);
        if (bottom != null) {
            html.print("<div role='bottom'>");
            bottom.output(html);
            html.println("</div>");
        }
        outFoot(html);
    }

    /**
     * 输入头部
     *
     * @param html 输出器
     */
    public void outHead(HtmlWriter html) {
        html.print("<form");
        if (this.action != null) {
            html.print(" action=\"%s\"", this.action);
        }
        if (this.method != null) {
            html.print(" method=\"%s\"", this.method);
        }
        if (this.getId() != null) {
            html.print(" id=\"%s\"", this.getId());
        }
        if (this.enctype != null) {
            html.print(" enctype=\"%s\"", this.enctype);
        }
        html.println(">");
        for (String key : items.keySet()) {
            String value = items.get(key);
            html.print("<input");
            html.print(" type=\"hidden\"");
            html.print(" name=\"%s\"", key);
            html.print(" id=\"%s\"", key);
            html.print(" value=\"%s\"", value);
            html.println("/>");
        }
    }

    /**
     * 输出尾部
     *
     * @param html 输出器
     */
    public void outFoot(HtmlWriter html) {
        html.println("</form>");
    }

    public void addHidden(String key, String value) {
        items.put(key, value);
    }

    public String getEnctype() {
        return enctype;
    }

    public void setEnctype(String enctype) {
        this.enctype = enctype;
    }

    public Map<String, String> getItems() {
        return items;
    }

    public UIComponent getTop() {
        if(top == null)
            top = new UIOriginComponent(this); 
        return top;
    }

    public void setTop(UIComponent top) {
        this.top = top;
    }

    public UIComponent getBottom() {
        if(bottom == null)
            bottom = new UIOriginComponent(this); 
        return bottom;
    }

    public void setBottom(UIComponent bottom) {
        this.bottom = bottom;
    }

}

@@ -1198,330 +1198,27 @@
     
-if (top != null) %7B%0A            html.print(%22%3Cdiv role='top'%3E%22);%0A            top.output(html);%0A            html.println(%22%3C/div%3E%22);%0A        %7D%0A        super.output(html);%0A        if (bottom != null) %7B%0A            html.print(%22%3Cdiv role='bottom'%3E%22);%0A            bottom.output(html);%0A            html.println(%22%3C/div%3E%22);%0A        %7D
+super.output(html);
 %0A   
@@ -2153,111 +2153,414 @@
     
-%7D%0A%0A    /**%0A     * %E8%BE%93%E5%87%BA%E5%B0%BE%E9%83%A8%0A     *%0A     * @param html %E8%BE%93%E5%87%BA%E5%99%A8%0A     */%0A    public void outFoot(HtmlWriter html) %7B
+    if (top != null) %7B%0A            html.print(%22%3Cdiv role='top'%3E%22);%0A            top.output(html);%0A            html.println(%22%3C/div%3E%22);%0A        %7D%0A    %7D%0A%0A    /**%0A     * %E8%BE%93%E5%87%BA%E5%B0%BE%E9%83%A8%0A     *%0A     * @param html %E8%BE%93%E5%87%BA%E5%99%A8%0A     */%0A    public void outFoot(HtmlWriter html) %7B%0A        if (bottom != null) %7B%0A            html.print(%22%3Cdiv role='bottom'%3E%22);%0A            bottom.output(html);%0A            html.println(%22%3C/div%3E%22);%0A        %7D
 %0A   

Update UIForm.java