<%--
  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 dept_name = pub.trimNull(request.getParameter("dept_name"));
    String dept_order = pub.trimNull(request.getParameter("dept_order"));
    String id = pub.trimNull(request.getParameter("id"));
    System.out.println("***********"+id);
    String p_id = pub.trimNull(request.getParameter("p_id"));
    Hashtable ht = new Hashtable();
    String have_roles = "";
    String p_name = "";
    try {

        conn = com.censoft.db.DBUtil.getConnection();
        if ("add".equals(action)) {
            String add_time = pub.getCurrentServerTime();
            String add_user = (String) session.getAttribute("session_login_id");
            boolean is_up = cf.executeUpdate(conn, "insert into t_book_dept(id,dept_name,dept_order,p_id,add_time,add_user) values (?,?,?,?,?,?)", new Object[]{DBUtil.getUUID(),dept_name,dept_order,p_id,add_time,add_user});
            Logger.log(conn,"机构管理", "添加", id, dept_order, 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_dept set  dept_name=?,dept_order=?,update_time=?,update_user=? where id=?", new Object[]{dept_name, dept_order, update_time, update_user, id});
            Logger.log(conn,"机构管理", "修改", id, dept_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_dept where id=?", new Object[]{id});
            Logger.log(conn,"机构管理", "删除", id, dept_name, request);
            return;
        }
        if (!"".equals(id)) {
            v_list = cf.doQuery(conn, "select t1.*  from t_book_dept t1  where t1.id=?", null, new Object[]{id});
            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_dept 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("dept_name"));
			}
            
        }

    } catch (Exception e) {
        out.println("查询机构信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
    <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%>"/>
        <B>父机构名称：</B><%=p_name%>
            &nbsp;
        <B>机构名称：</B><input type="text" class="input-text" style="width:150px;" value="<%=pub.trimNull((String)ht.get("dept_name"))%>"
                       placeholder="" id="dept_name" name="dept_name">
        &nbsp;
        <B>排序：</B><input type="text" class="input-text" style="width:50px;"value="<%=pub.trimNull((String)ht.get("dept_order"))%>"
                       placeholder="" id="dept_order" name="dept_order">
        <%%>
        <a class="btn btn-primary radius" name="addjg" onclick="javascript:saveForm();">保存机构</a>
        &nbsp; <a class="btn btn-danger radius" name="deletejg" onclick="javascript:deleteForm();">删除机构</a>
        <a class="btn btn-primary radius" name="addchild" onclick="javascript:getDept('','<%=id%>')">添加子机构</a>
    </form>
<script>
    function deleteForm(){
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: './dept_ajax.jsp?action=delete&id=<%=id%>',
                success: function (data) {
                    layer.msg('已删除!', {
                        icon: 1, time: 1000
                    }, function () {
                        window.location.reload();
                    });
                },
                error: function (data) {
                    layer.msg('删除失败!', {
                        icon: 1, time: 1000
                    });
                },
            });
        });
    }
    function saveForm() {
        $.ajax({
            type: "POST",
            url: "./dept_ajax.jsp",
            data: $('#form-admin-add').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            window.location.reload();
                        });
                } else {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // console.log("********************8")
                    });
                }
            }
        });
    }
</script>