<%--
  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;
    String action = pub.trimNull(request.getParameter("action"));
    String dept_name = pub.trimNull(request.getParameter("zd_name"));
    String zd_e_name = pub.trimNull(request.getParameter("zd_e_name"));
    String dept_order = pub.trimNull(request.getParameter("zd_order"));
    if("".equals(dept_order)){
        dept_order="1";
    }
    String id = pub.trimNull(request.getParameter("id"));
    String remark = pub.trimNull(request.getParameter("beizhu"));
    String p_id = pub.trimNull(request.getParameter("p_id"));
    Hashtable ht = new Hashtable();
    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");
            Vector checkList=cf.doQuery(conn,"select * from zc_zd where zd_e_name=?  ",null,new Object[]{zd_e_name});
            if(checkList!=null && checkList.size()>0){
                out.println("英文名称已存在");
                return;
            }
            boolean is_up = cf.executeUpdate(conn, "insert into zc_zd(id,zd_name,zd_e_name,zd_order,p_id,add_time,add_user,beizhu) values (?,?,?,?,?,?,?,?)", new Object[]{DBUtil.getUUID(),dept_name,zd_e_name,dept_order,p_id,add_time,add_user,remark});
            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");
            Vector checkList=cf.doQuery(conn,"select * from zc_zd where zd_e_name=? and id!=? ",null,new Object[]{zd_e_name,id});
            if(checkList!=null && checkList.size()>0){
                out.println("英文名称已存在");
                return;
            }
            boolean is_up = cf.executeUpdate(conn, "update zc_zd set  zd_name=?,zd_e_name=?,zd_order=?,add_time=?,add_user=?,beizhu=? where id=?", new Object[]{dept_name, zd_e_name,dept_order, update_time, update_user,remark, 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)) {
            Vector isHaveChildDept = cf.doQuery(conn,"select id from zc_zd where p_id=?"
                    ,null,new Object[]{id});
            if(isHaveChildDept!=null && isHaveChildDept.size()>0){
                out.println("当前字典有下级，不能删除");
                return;
            }
            boolean is_up = cf.executeUpdate(conn, "delete from  zc_zd where id=?", new Object[]{id});
            String content = "";
            if(is_up){
                content = "删除成功";
                out.println("success");
            }else{
                content = "删除失败";
            }
            Logger.log(conn,"字典管理", "删除", id, content, request);
            return;
        }
        if (!"".equals(id)) {
            v_list = cf.doQuery(conn, "select t1.*  from zc_zd 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 zc_zd t1  where t1.id=?", null, new Object[]{p_id});
            Hashtable ht_p = (Hashtable) v_list.get(0);
            p_name = pub.trimNull((String)ht_p.get("zd_name"));
        }

    } catch (Exception e) {
        out.println("查询机构信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
    <div class="panel-header">
        字典
    </div>
    <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">
            <div class="col-xs-offset-1">
                <a class="btn btn-success radius" onclick="saveForm();">保存</a>
                <a class="btn btn-danger radius" onclick="deleteForm();">删除</a>

            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4">上级字典名称：</label>
            <div class="formControls col-xs-4">
                <input type="text" class="input-text" placeholder="" disabled value="<%=p_name%>">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4">字典名称：</label>
            <div class="formControls col-xs-4">
                <input type="text" class="input-text"  value="<%=pub.trimNull((String)ht.get("zd_name"))%>" placeholder="" id="zd_name" name="zd_name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4">英文名称：</label>
            <div class="formControls col-xs-4">
                <input type="text" class="input-text"  value="<%=pub.trimNull((String)ht.get("zd_e_name"))%>" placeholder="" id="zd_e_name" name="zd_e_name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4">序号：</label>
            <div class=" formControls col-xs-4">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("zd_order"))%>" placeholder="" id="zd_order" name="zd_order">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4">备注：</label>
            <div class=" formControls col-xs-4">
                <textarea  class="textarea" id="beizhu" placeholder="" name="beizhu"><%=pub.trimNull((String)ht.get("beizhu"))%></textarea>
            </div>
        </div>

    </form>
<script>
    function deleteForm(){
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: './dept_add.jsp?action=delete&id=<%=id%>',
                success: function (msg) {
                    if (msg.indexOf("success") >= 0) {
                        layer.msg('已删除!', {
                            icon: 1, time: 1000
                        }, function () {
                            window.location.reload();
                        });
                    }else {
                        layer.msg('操作失败,'+msg, {
                            icon: 5,
                            time: 3000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            // console.log("********************8")
                        });
                    }
                },
                error: function (data) {
                    layer.msg('删除失败!', {
                        icon: 1, time: 1000
                    });
                },
            });
        });
    }
    function saveForm() {
        $.ajax({
            type: "POST",
            url: "./dept_add.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('操作失败'+msg, {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // console.log("********************8")
                    });
                }
            }
        });
    }
</script>