<%@ page import="com.censoft.common.base.Pub" %><%--
  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 contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ 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 menu_id = pub.trimNull(request.getParameter("menu_id"));
    String role_id = pub.trimNull(request.getParameter("role_id"));
    String action = pub.trimNull(request.getParameter("action"));
    String have_btns = "";
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        if ("shouquan".equals(action)) {
            String[] bts = request.getParameterValues("btn_id");
            boolean is_up = cf.executeUpdate(conn, "delete from t_book_role_menu_btn where role_id=? and menu_id=?", new Object[]{role_id, menu_id});

            String btn_ids = "";
            if (bts != null && bts.length > 0) {
                for (int i = 0; i < bts.length; i++) {
                    btn_ids += bts[i] + ",";
                }
            }
            is_up = cf.executeUpdate(conn, "insert into  t_book_role_menu_btn(btn_ids,role_id,menu_id) values (?,?,?) ", new Object[]{btn_ids, role_id, menu_id});
            Logger.log(conn,"按钮授权", "授权", role_id+","+menu_id, "授权"+btn_ids, request);
            if (!is_up) {
                out.println("修改按钮权限出错" + cf.getMsg());
            } else {
                out.println("success");
            }
            return;
        }
        String sql = "select * from   t_book_btn  where menu_id=? order by add_time desc";
        v_list = cf.doQuery(conn, sql, null, new Object[]{menu_id});
        if (v_list == null) {
            out.println("查询用户出错");
            return;
        }
        Vector v_btn = cf.doQuery(conn, "select btn_ids from t_book_role_menu_btn where role_id=? and menu_id=?", null, new Object[]{role_id, menu_id});
        if (v_btn != null && v_btn.size() > 0) {
            Hashtable ht = (Hashtable) v_btn.get(0);
            have_btns = pub.trimNull((String) ht.get("btn_ids"));
        }
    } catch (Exception e) {
        out.println("查询用户信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
<div class="page-container">
    <div class="mt-20" id="datalist">
        <form class="form form-horizontal" id="form-admin-add">
            <input type="hidden" name="menu_id" value="<%=menu_id%>"/>
            <input type="hidden" name="role_id" value="<%=role_id%>"/>
            <input type="hidden" name="action" value="shouquan"/>
            <table class="table table-border table-bordered table-hover table-bg table-sort">
                <thead>
                <tr class="text-c">
                    <th width="50"><span class="label label-success radius" style="cursor:pointer" onclick="btnSq();">授权</span></th>
                    <th width="">按钮名称</th>
                    <th width="150">按钮NAME</th>
                </tr>
                </thead>
                <tbody>
                <%
                    if (v_list != null && v_list.size() > 0) {
                        for (int i = 0; i < v_list.size(); i++) {
                            Hashtable ht = (Hashtable) v_list.get(i);
                            String btn_name = pub.trimNull((String) ht.get("btn_name"));
                            String add_time = pub.trimNull((String) ht.get("add_time"));
                            String btn_id = pub.trimNull((String) ht.get("btn_id"));
                            String id = pub.trimNull((String) ht.get("id"));
                %>
                <tr class="text-c">
                    <td><input type="checkbox" name="btn_id" <%if(have_btns.indexOf(btn_id)>=0){%>checked<%}%>
                               value="<%=btn_id%>">
                    </td>
                    <td><%=btn_name%>
                    </td>
                    <td><%=btn_id%>
                    </td>
                </tr>
                <%
                        }
                    }
                %>
                </tbody>
            </table>
        </form>
    </div>
</div>

<script type="text/javascript">
    function iGetInnerText(testStr) {
        var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
        resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
        resultStr = testStr.replace(/[\r\n]/g, ""); //去掉回车换行
        return resultStr;
    }

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