<%--
  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;
    Vector v_roles = null;
    String action = pub.trimNull(request.getParameter("action"));
    String btn_name = pub.trimNull(request.getParameter("btn_name"));
    String btn_id = pub.trimNull(request.getParameter("btn_id"));
    String menu_id = pub.trimNull(request.getParameter("menu_id"));
    String id = pub.trimNull(request.getParameter("id"));
    Hashtable ht = new Hashtable();
    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_btn_id");
            boolean is_up = cf.executeUpdate(conn, "insert into t_book_btn(menu_id,id,btn_name,btn_id,add_time) values (?,?,?,?,?)", new Object[]{menu_id, id, btn_name, btn_id, add_time});
            Logger.log(conn,"菜单按钮管理", "添加", id, btn_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_btn_id");
            boolean is_up = cf.executeUpdate(conn, "update t_book_btn set btn_name=?,btn_id=?  where id=?", new Object[]{btn_name, btn_id, id});
            Logger.log(conn,"菜单按钮管理", "修改", id, btn_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_btn where id=?", new Object[]{id});
            Logger.log(conn,"菜单按钮管理", "删除", id, btn_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 * from t_book_btn where id=?", null, new Object[]{id});
            ht = (Hashtable) v_list.get(0);
            action = "update";
        } else {
            action = "add";
        }
    } 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="menu_id" value="<%=menu_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" class="input-text" value="<%=pub.trimNull((String)ht.get("btn_name"))%>"
                       placeholder="" id="btn_name" name="btn_name">
            </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("btn_id"))%>"
                       placeholder="" id="btn_id" name="btn_id">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="button" onclick="javascript:saveForm();"
                       value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>
<script>
    function saveForm() {
        $.ajax({
            type: "POST",
            url: "./btn_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();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                } else {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // console.log("********************8")
                    });
                }
            }
        });
    }

    changeBtn();
</script>