<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="com.gx.webpage.framework.util.inoutformat.DataBean" %>
<%@ page import="com.gx.webpage.engine.pagedesigndbengine.factory.PageDesignDbEngineFactory"%>
<%@ page import="com.gx.webpage.engine.pagedesigndbengine.valueobject.*"%>
<%
    boolean isSave = false;
    boolean isError = false;
    String infStr = "";
    try{
        if(request.getMethod().equalsIgnoreCase("POST")){
            isSave = true;
            String tblName = DataBean.getStr(request, "tblName");
            String tblId = DataBean.getStr(request, "tblId");
            String desc = DataBean.getStr(request, "desc");
            String sql1 = DataBean.getStr(request, "sql_table");
            String sql2 = DataBean.getStr(request, "sql_table_join");
            String sql3 = DataBean.getStr(request, "sql_table_field");
            String sql4 = DataBean.getStr(request, "sql_table_input");
            String sql5 = DataBean.getStr(request, "sql_base_Str");
            FormsModel fm = new FormsModel();
            fm.setTblId(tblId);
            fm.setTblName(tblName);
            fm.setDescription(desc);
            fm.setSqlSelectColumn(sql3);
            fm.setSqlFromColumn(sql1);
            fm.setSqlWhereColumn(sql2);
            fm.setBaseSql(sql5);
            fm.setType(FormsModel.TYPE_VIEW);
            fm = PageDesignDbEngineFactory.getInstance().getPageDbEngineApi().createForms(fm, false, "",request);
            Collection col = DataBean.splitInputCol(sql4);
            Iterator iter = col.iterator();
            while (iter.hasNext())
            {
                Collection itemCol = (Collection)iter.next();
                Object[] items = itemCol.toArray();
                String table = (String)items[0];
                String field = (String)items[1];
                String fielda = (String)items[2];
                String title = (String)items[3];
                String type = (String)items[4];
                String logic = (String)items[5];
                FormsDetailModel fdm = new FormsDetailModel();
                fdm.setFormUuid(fm.getUuid());
                fdm.setTableId(table);
                fdm.setFieldId(field);
                fdm.setDefaultValue(fielda);
                fdm.setFieldName(title);
                fdm.setFieldType(type);
                fdm.setLogic(logic);
                fdm = PageDesignDbEngineFactory.getInstance().getPageDbEngineApi().createFormsDetail(fdm,request);
            }
        }
    }catch(Exception err){
        isError = true;
        infStr = DataBean.getErrStr(err);
    }
%>
<head>
     <meta http-equiv="Content-Language" content="zh-cn">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="./../../../webpage/common/css/common.css" rel="StyleSheet"  type="text/css" />
<script language="javascript" src="./../../../webpage/common/engine/constant.js"></script>
<script language="JScript.Encode" src="./../../../webpage/common/engine/common.js"></script>
<script language="JScript.Encode" src="./../../../webpage/common/engine/DTFControl.js"></script>
<script language="JScript.Encode" src="./../../../webpage/common/js/DataReferControl.js"></script>
<script language="JScript.Encode" src="./../../../webpage/design/js/SqlBuilder.js"></script>
<script language="javascript">
<!--
    var md = new SqlDefine();
    function btn_toList(){
        window.navigate("searchList.jsp");
    }
    function btn_submit()
    {
        var b = trimString(document.all("tblId",0).value);
        if(b.length == 0)
        {
            MSG.show("请先定义查询的ＳＱＬ语句．");
            return;
        }
        formsAdd.submit();
    }
    function btn_addNext()
    {
        window.navigate("searchAdd.jsp");
    }
    function btn_define()
    {
        var m = ShowSqlBuilder(md);
        if(m != null)
        {
            md = m;
            window.formsAdd.txtSqlPre.innerText = md.getPreSql();
            window.formsAdd.sql_table.value = md.dataSource + "#" + md.sql0;
            window.formsAdd.sql_table_join.value = md.sql1;
            window.formsAdd.sql_table_field.value = md.sql2;
            window.formsAdd.sql_table_input.value = md.sql3;
            window.formsAdd.sql_base_Str.value = md.getBaseSQL();
            window.formsAdd.tblId.value = md.tag;
            window.formsAdd.tblName.value = md.name;
        }
    }
//-->
</script>
    <title>新增自定义查询</title>
</head>
<body class="main">
<div align="center">
<h3 align="center">新增自定义查询</h3>
<%if(!isError){
    if(isSave){
%>
    <p align=center>新增成功．
    <p align=center>
    <input class="butAction" type="button" name="addNext" value="新增下一条" onclick="btn_addNext();">
    <input class="butAction" type="button" name="toList" value=" 返回列表 " onclick="btn_toList();">
<%}else{%>
<form id="formsAdd" name="formsAdd" method="POST">
<table class="content">
    <tr>
        <td width=30%>查询业务标识</td><td><input type="text" id="tblId" size=40 name="tblId" readonly></td>
    </tr>
    <tr>
        <td>查询业务名称</td><td><input type="text" id="tblName" name="tblName" size=40 readonly></td>
    </tr>
    <tr>
        <td>查询描述</td><td><textarea id="desc" name="desc" rows=4 cols=40></textarea></td>
    </tr>
    <tr>
        <td>SQL语句</td><td><textarea id=txtSqlPre name=txtSqlPre readonly cols=40 rows=10></textarea>
    </tr>
</table>
<input type=hidden id=sql_table name=sql_table value="">
<input type=hidden id=sql_table_join name=sql_table_join value="">
<input type=hidden id=sql_table_field name=sql_table_field value="">
<input type=hidden id=sql_table_input name=sql_table_input value="">
<input type=hidden id=sql_base_Str name=sql_base_Str value="">
</form>
<table class="buttable2">
<tr>
    <td align=center><input class="butAction" type="button" name="add" value="定　　义" onclick="btn_define();">
    <td align=center><input class="butAction" type="button" name="add" value="增　　加" onclick="btn_submit();">
    <td align=center><input class="butAction" type="button" name="toList" value="返回列表" onclick="btn_toList();">
</tr>
</table>
<%}}else{%>
<p align=center>新增失败．
<p align=center><%= infStr%>
<table class="buttable2">
<tr>
    <td width=50% align=center><input class="butAction" type="button" name="add" value="后　　退" onclick="CFPFORM.cancel();">
    <td width=50% align=center><input class="butAction" type="button" name="toList" value="返回列表" onclick="btn_toList();">
</tr>
</table>
<%}%>
</div>
</body>
</html>
