<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 12:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.db.DBUtil" %>
<%@ page import="com.censoft.db.Logger" %>
<%@ page import="java.sql.Connection" %>
    <%@ page import="java.util.Hashtable" %>
    <%@ page import="java.util.Vector" %>
        <%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    String action = pub.trimNull(request.getParameter("action"));
    String menu_name = pub.trimNull(request.getParameter("menu_name"));
    String p_id = pub.trimNull(request.getParameter("p_id"));
    String menu_order = pub.trimNull(request.getParameter("menu_order"));
    String menu_url = pub.trimNull(request.getParameter("menu_url"));
    String menu_pic = pub.trimNull(request.getParameter("menu_pic"));
    String sys_id = pub.trimNull(request.getParameter("sys_id"));
    String id = pub.trimNull(request.getParameter("id"));
    Hashtable ht = new Hashtable();
    String p_name = "";
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        if ("add".equals(action)) {
            id = DBUtil.getUUID();
            String add_time = pub.getCurrentServerTime();
            String add_user = (String) session.getAttribute("session_login_id");
            boolean is_up = cf.executeUpdate(conn, "insert into t_book_menu(menu_pic,menu_url,id,menu_name,p_id,add_time,add_user,menu_order,sys_id) values (?,?,?,?,?,?,?,?,?)", new Object[]{menu_pic, menu_url, id, menu_name, p_id, add_time, add_user, menu_order,sys_id});
            Logger.log(conn,"菜单管理", "添加", id, menu_name, request);
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        } else if ("update".equals(action)) {
            String update_time = pub.getCurrentServerTime();
            String update_user = (String) session.getAttribute("session_login_id");
            boolean is_up = cf.executeUpdate(conn, "update t_book_menu set menu_name=?,p_id=?,menu_url=?,menu_pic=?,menu_order=?,update_time=?,update_user=?,sys_id=? where id=?", new Object[]{menu_name, p_id, menu_url, menu_pic, menu_order, update_time, update_user,sys_id, id});
            Logger.log(conn,"菜单管理", "修改", id, menu_name, request);
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        } else if ("delete".equals(action)) {
            boolean is_up = cf.executeUpdate(conn, "delete from  t_book_menu where id=?", new Object[]{id});
            Logger.log(conn,"菜单管理", "删除", id, menu_name, request);
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        }
        if (!"".equals(id)) {
            v_list = cf.doQuery(conn, "select t1.*  from t_book_menu t1 where t1.id=?", null, new Object[]{id});
            if (v_list != null && v_list.size() > 0) {
                ht = (Hashtable) v_list.get(0);
            }
            p_id = pub.trimNull((String) ht.get("p_id"));
            action = "update";
        } else {
            action = "add";
        }

        if (!"".equals(p_id)) {
            v_list = cf.doQuery(conn, "select t1.*  from t_book_menu t1 where t1.id=?", null, new Object[]{p_id});
            if (v_list != null && v_list.size() > 0) {
                Hashtable ht_p = (Hashtable) v_list.get(0);
                p_name = pub.trimNull((String) ht_p.get("menu_name"));
            }
        }
    } catch (Exception e) {
        out.println("查询角色信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
<article class="page-container">
    <form class="form form-horizontal" id="form-admin-add">
        <input type="hidden" name="id" value="<%=id%>"/>
        <input type="hidden" name="p_id" value="<%=p_id%>"/>
        <input type="hidden" name="action" value="<%=action%>"/>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>上级菜单：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" readonly style="background: #e1e1e1;" class="input-text" value="<%=p_name%>"
                       placeholder="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>菜单名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("menu_name"))%>"
                       placeholder="" id="menu_name" name="menu_name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>菜单地址：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("menu_url"))%>"
                       placeholder="" id="menu_url" name="menu_url">“#”是父节点
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>菜单图标：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.change2HtmlText(pub.trimNull((String)ht.get("menu_pic")))%>"
                       placeholder="" id="menu_pic" name="menu_pic">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>排序：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("menu_order"))%>"
                       placeholder="" id="menu_order" name="menu_order">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>系统id：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("sys_id"))%>" placeholder="" id="sys_id" name="sys_id">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <a class="btn btn-primary radius" name="savecd" onclick="javascript:saveForm();"
                >&nbsp;&nbsp;保存&nbsp;&nbsp;</a>&nbsp;
                <a class="btn btn-success radius" name="addchild" onclick="javascript:newForm();"
                >&nbsp;&nbsp;添加下级&nbsp;&nbsp;</a>&nbsp;
                &nbsp;<a class="btn btn-success radius" name="addgen" onclick="javascript:newForm1();"
            >&nbsp;&nbsp;添加根&nbsp;&nbsp;</a>&nbsp;
                <a class="btn btn-danger radius" name="deletecd" onclick="javascript:deleteForm();"
                >&nbsp;&nbsp;删除菜单&nbsp;&nbsp;</a>&nbsp;
                <a class="btn btn-primary radius" name="setbt" onclick="javascript:btnList();"
                >&nbsp;&nbsp;设置按钮&nbsp;&nbsp;</a>&nbsp;
            </div>
        </div>
    </form>
</article>
<script>
    function btnList() {
        var url = "./btn_list-t.jsp?menu_id=<%=id%>";
        var index = layer.open({
            type: 2,
            title: "<%=pub.trimNull((String)ht.get("menu_name"))%>-设置按钮",
            content: url
        });
        layer.full(index);
    }

    function newForm() {
        window.location.href = "./menu_add-t.jsp?p_id=<%=id%>";
    }

    function newForm1() {
        window.location.href = "./menu_add-t.jsp";
    }

    function saveForm() {
        $.ajax({
            type: "POST",
            url: "./menu_add-t.jsp",
            data: $('#form-admin-add').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            parent.location.reload();
                        });
                } else {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // console.log("********************8")
                    });
                }
            }
        });
    }

    function deleteForm() {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: './menu_add-t.jsp?action=delete&id=<%=id%>',
                success: function (data) {
                    layer.msg('已删除!', {
                        icon: 1, time: 1000
                    }, function () {
                        parent.location.reload();
                    });
                },
                error: function (data) {
                    layer.msg('删除失败!', {
                        icon: 1, time: 1000
                    });
                },
            });
        });
    }

    $(function () {
        changeBtn();
    });

</script>