<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="com.gx.webpage.engine.menupage.designdbengine.factory.*" %>
<%@ page import="com.gx.webpage.engine.menupage.designdbengine.valueobject.*" %>
<%@ page import="com.gx.webpage.framework.valueobject.*" %>
<%@ page import="com.gx.webpage.engine.menupage.designdbengine.api.*" %>
<%@ page import="com.gx.webpage.framework.util.inoutformat.DataBean" %>
<%
    String errStr = "";
    boolean isSave = false;
    boolean isError = false;
    String parentUuid = DataBean.getStr(request, "parentUuid");
    String parentMenuName = DataBean.getStr(request, "parentName");
    String addRoot = DataBean.getStr(request, "addRoot");
    SysMenuModel menuModel = null;
    String icon = "";
    String name = "";
    String desc = "";
    String uuid = "";
    int indexNum = 0;
    int menuLevel = 0;
    int systemId = 0;
    String targetPage = "";
    int nowSysId = 0;
    int nowMenuLevel = 0;
    String parentUuidPath = "";
    String parentCatpath = "";
    boolean topMenuFlg = false;
    try
    {
        UserInfoCtxModel uicm = new UserInfoCtxModel();
        uicm.setUserId("cc");
        EbiMenuMgr delegate = MenuFactory.getInstance().getEbiMenuMgr();
        if(parentMenuName==null || parentMenuName.trim().length()==0){
            topMenuFlg = true;
            //isError = true;
        }else{
            SysMenuModel tempSm = delegate.findSysMenu(parentUuid,false,uicm);
            parentUuidPath = tempSm.getParentCatPath();
            if(parentUuidPath==null || parentUuidPath.trim().length()==0){
              parentUuidPath = "com.cc.menu."+parentUuid;
              parentCatpath = parentUuid;
            }else{
              parentCatpath = parentUuidPath+"."+parentUuid;
              parentUuidPath = "com.cc.menu"+"."+parentUuidPath+"."+parentUuid;
            }
            if(tempSm.getSystemId()==0){
              nowSysId = SysMenuModel.getSysId(parentMenuName);
              nowMenuLevel = 1;
            }else{
              nowSysId = tempSm.getSystemId();
              nowMenuLevel = tempSm.getMenuLevel() +1;
            }
        }
        if(request.getMethod().equalsIgnoreCase("POST"))
        {
            isSave = true;
            icon = DataBean.getStr(request, "icon");
            name = DataBean.getStr(request, "name");
            desc = DataBean.getStr(request, "desc");
            indexNum = DataBean.getInt(request, "indexNum");
            menuLevel = SysMenuModel.getMenuLevel(DataBean.getStr(request, "menuLevel"));
            targetPage = DataBean.getStr(request, "targetPage");
            systemId = SysMenuModel.getSysId(DataBean.getStr(request, "systemId"));
            menuModel = new SysMenuModel();
            menuModel.setDescription(desc);
            menuModel.setSystemId(systemId);
            menuModel.setName(name);
            menuModel.setParentUuid(parentUuid);
            menuModel.setIcon(icon);
            menuModel.setIndexNum(indexNum);
            menuModel.setMenuLevel(menuLevel);
            menuModel.setTargetPage(targetPage);
            menuModel.setParentTarget("parent.cfpMain");
            menuModel.setParentCatPath(parentCatpath);
            //AclModel aclModel = PowerHelper.getInstance().getAclModel(request, uicm);
            //boolean needAddAcl = false;
            //if(aclModel != null){
            //    needAddAcl = true;
            //}
            uicm.setNeedAcl(false);
            uicm.setNeedAddAcl(false);
            uicm.setAclType(uicm.ACLTYPE_ROW);
            uicm.setRowCatPath(parentUuidPath);
            menuModel = delegate.createSysMenu(menuModel,uicm);
            if(menuModel == null)
            {
                isError = true;
            }
        }
    }
    catch(Exception ex)
    {
      errStr = DataBean.getErrStr(ex);
      isError = true;
    }
%>
<html>
    <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="javascript">
<!--
     function formSubmit()
    {
<%if(addRoot.equals("true")){%>
        var ret = UTIL.checkNull("name", "请填写菜单的名称.");
        if(ret == false) return;
<%}else{%>
        var ret = UTIL.checkNull("name", "请填写菜单的名称.");
        if(ret == false) return;
<%}%>
        if(acl != null){
            acl.submit(addForm);
        }
        window.addForm.submit();
    }
  var acl = null;
  function addAclSetting()
  {
    if(acl == null)
    {
        acl = new CfpDefaultPowerDefModel();
        acl.setCatPath("<%=parentUuidPath%>");
        acl.setName("菜单权限");
        acl.setPowerType(acl.POWER_TYPE_ROW);
        acl.setAddWithForm(true);
    }
    acl = CHILDWIN.showPowerDialog(acl);
  }
//-->
</script>
    </head>
    <body class="main">
<%if(addRoot.equals("true")) {
    if(!("System".equals(UserInfoCtxModel.getSysUicm().getUserId()))){
      out.println("<html> <meta http-equiv='refresh' content='1;url=menuInfo.jsp'> </html>");
      return;
    }
%>
    <p class="title">添加根菜单信息
<%}else{%>
    <p class="title">添加菜单
<%}%>
<%
   if(isSave)
   {
       if(isError)
       {
%>
           <p class="error">保存失败. (<%= errStr%>)
           <p class="error"><input class="butACTION" type="button" value="返　　回" name="butBack" onclick = "CFPFORM.cancel();">
<%
       }
       else
       {
%>
           <p class="information">保存成功.
           <p class="information"><input class="butACTION" type="button" value="添加下一个" name="butAddNext" onclick = "window.navigate('menuAdd.jsp?parentUuid=<%= parentUuid%>&parentName=<%=parentMenuName%>')">
                                  <input class="butACTION" type="button" value="确　　定" name="butBack" onclick = "window.navigate('menuInfo.jsp?uuid=<%= menuModel.getUuid()%>')">
           <script language="javascript">
           <!--
              try{
                  parent.window.leftFrame.window.location.reload();
              }catch(e){}
           //-->
           </script>
<%
       }
   }
   else
   {
       if(isError)
       {
        if(topMenuFlg){
%>
           <p class="error">这是系统级菜单，不可编辑.
<%
        }else{
%>
           <p class="error">错误，请联系管理员．(<%= errStr%>)
<%
       }}
       else
       {
%>
<form name="addForm" id="addForm" method="POST" >
  <div align="center">
    <table class="content">
      <tr>
        <td width = "30%">所属系统</td>
        <td width = "70%">
          <input type="text" disabled="false" size="50" name="systemId" id="systemIdTemp" value="<%=(String)SysMenuModel.viewMapSysId.get(Integer.toString(nowSysId))%>">
        </td>
        <input type="hidden" name="systemId"  value="<%=(String)SysMenuModel.viewMapSysId.get(Integer.toString(nowSysId))%>">
      </tr>
      <tr>
        <td width = "30%">菜单层次</td>
        <td width = "70%">
          <input type="text" disabled="false" size="50" name="menuLevel" id="menuLevel" value="<%=(String)SysMenuModel.viewMapMenuLevel.get(Integer.toString(nowMenuLevel))%>">
        </td>
        <input type="hidden" name="menuLevel"  value="<%=(String)SysMenuModel.viewMapMenuLevel.get(Integer.toString(nowMenuLevel))%>">
      </tr>
<%if(!addRoot.equals("true")){%>
      <tr>
        <td width = "30%">上级菜单名称<input type = "hidden" name = "parentUuid" value = "<%= parentUuid%>"></td>
        <td width = "70%"><input class="txtInput" type="text" name="txtMenuName" size="50" disabled="false" value="<%=parentMenuName%>">
                          </td>
      </tr>
<%}%>
      <tr>
        <td width = "30%">本级菜单名称</td>
        <td width = "70%"><input class="txtInput" type="text" name="name" size="50"></td>
      </tr>
      <tr>
        <td width = "30%">菜单序列号</td>
        <td width = "70%"><input class="txtInput" type="text" name="indexNum" size="50" value="1"></td>
      </tr>
<%if(addRoot.equals("true")|| nowMenuLevel==SysMenuModel.MENULEVEL_THREE){%>
      <tr>
        <td width = "30%">图标位置</td>
        <td width = "70%"><input class="txtInput" type="text" name="icon" size="50"></td>
      </tr>
      <tr>
        <td width = "30%">操作页面</td>
        <td width = "70%"><input class="txtInput" type="text" name="targetPage" size="50"></td>
      </tr>
<%}%>
      <tr>
        <td width = "30%">描述</td>
        <td width = "70%"><textarea rows="5" name="desc" cols="50"></textarea></td>
      </tr>
    </table>
    <table class="buttable1">
      <tr>
        <td align = "center" width = "25%"><input class="butACTION" type="button" value="添加菜单" name="butSubmit" onclick="formSubmit();"></td>
        <td align = "center" width = "25%"><input class="butACTION" type="button" value="添加权限" name="butSubmit" onclick="addAclSetting();"></td>
        <td align = "center" width = "25%"><input class="butACTION" type="reset"  value="全部重写" name="butReset"></td>
      </tr>
    </table>
  </div>
</form>
<% }} %>
    </body>
</html>
