<%--
  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="java.sql.Connection" %>
<%@ page import="java.util.*" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="org.omg.CosCollection.IteratorInBetween" %>
<%@ page import="com.censoft.db.DBUtil" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.DateFormat" %>
<%!
    Pub pub = new Pub();

    public int getCounts(ConnectionFactory cf,Connection conn,String ly_id,String reg_id){
        StringBuffer sb = new StringBuffer();
        sb.append("select reg_id from zc_register" +
                " where zc_id in (select  zc_id from zc_ly where ly_id =?) and reg_id=?");

        Vector list = cf.doQuery(conn,sb.toString(),null,new Object[]{ly_id,reg_id});
        if(list==null){
            System.out.println("查询出错"+cf.getMsg());
            return 1;
        }
        return list.size();
    }
%>
<%
    ConnectionFactory cf = new ConnectionFactory();
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    Vector v_list_1 = null;
    Date sys_date = new Date();
    String id = df.format(sys_date)+sys_date.getTime();
    String page_number = pub.trimNull(request.getParameter("page_number"));
    if ("".equals(page_number)) page_number = "1";
    String page_size = pub.trimNull(request.getParameter("page_size"));
    String xm_id = pub.trimNull(request.getParameter("xm_id"));
    String ly_id = pub.trimNull(request.getParameter("ly_id"));
    if("".equals(ly_id)){
        ly_id=id;
    }
    String action = pub.trimNull(request.getParameter("action"));
    if("".equals(action)){
        action="add";
    }
    String ly_dept = pub.trimNull(request.getParameter("ly_dept"));
    String ly_user = pub.trimNull(request.getParameter("ly_user"));
    if ("".equals(page_size)) page_size = "10";
    String ly_reason="";
    String ly_time="";
    String tel="";
    String beizhu="";
    String cl_user="";
    String cl_tel="";
    List list_s = new ArrayList();
    Hashtable ht=null;
    String ly_status_view="";
    try {
        StringBuffer sb = new StringBuffer();
        StringBuffer sb_list = new StringBuffer();
        sb.append("select ly_id,xm_id,ly_dept,ly_reason,ly_time,ly_user,ly_status,tel,cl_user,cl_tel,beizhu,add_time from zc_ly where ly_id=? ");
        sb.append(" order by add_time desc");
        sb_list.append("select  reg_id,zc_id,zc_name,zc_fl,sb_type,counts,jiazhi,add_time from zc_register where zc_id in (select  zc_id from zc_ly where ly_id =?)");
        sb_list.append(" order by add_time desc");
        list_s.add(ly_id);
        conn = com.censoft.db.DBUtil.getConnection();
        v_list = cf.doQuery(conn, sb.toString(),null, list_s.toArray());
        v_list_1 = cf.doQuery(conn, sb_list.toString(),null, list_s.toArray());
        if (v_list == null) {
            out.println("查询出错" + cf.getMsg());
            return;
        }
        if (v_list_1 == null) {
            out.println("查询出错" + cf.getMsg());
            return;
        }
        if (v_list.size()>0&&!"".equals(ly_id)) {
            ht=(Hashtable)v_list.get(0);
            xm_id=pub.trimNull((String)ht.get("xm_id"));
            ly_dept=pub.trimNull((String)ht.get("ly_dept"));
            ly_reason=pub.trimNull((String)ht.get("ly_reason"));
            ly_time=pub.trimNull((String)ht.get("ly_time"));
            ly_user=pub.trimNull((String)ht.get("ly_user"));
            tel=pub.trimNull((String)ht.get("tel"));
            cl_user=pub.trimNull((String)ht.get("cl_user"));
            cl_tel=pub.trimNull((String)ht.get("cl_tel"));
            beizhu=pub.trimNull((String)ht.get("beizhu"));
            ly_status_view = pub.trimNull((String) ht.get("ly_status"));
        }
        String telSegmentUrl="./list_ly_zc_add-t.jsp?ly_id="+ly_id;
%>
<article class="page-container" style="overflow: hidden;">
    <form class="form form-horizontal" id="form-admin-add">
        <input type="hidden" name="action" id="action" value="<%=action%>"/>
        <input type="hidden" name="update_id" value=""/>
        <input type="hidden" name="ly_id" value="<%=ly_id%>"/>
        <div class="cl pd-5  bk-gray divOverflow" style="overflow:hidden;margin-bottom: 20px;">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">所属项目：</label>
                <div class="formControls col-xs-8 col-sm-3" id="xm_id" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>disabled="disabled" <%}%>>
                    <script>
                        var value='<%=xm_id%>';
                        $.post("../zd/zd_view_list.jsp?value="+value+"&name_id=xm_id&zd_e_name=xm_id&math="+Math.random(),function(data){
                                document.getElementById("xm_id").innerHTML=data;
                            }
                        );
                    </script>
                </div>
                <label class="form-label col-xs-4 col-sm-2">领用单位：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="ly_dept" id="ly_dept" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=ly_dept%>" class="input-text ">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">领用原因：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="ly_reason" id="ly_reason" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=ly_reason%>" class="input-text ">
                </div>
                <label class="form-label col-xs-4 col-sm-2">领用时间：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="ly_time" id="ly_time" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<%=ly_time%>" class="input-text Wdate">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">领用人：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="ly_user" id="ly_user" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=ly_user%>" class="input-text ">
                </div>
                <label class="form-label col-xs-4 col-sm-2">联系电话：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="tel" id="tel" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=tel%>" class="input-text ">
                </div>
            </div>
            <%--<div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">处理人：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="cl_user" id="cl_user" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=cl_user%>" class="input-text ">
                </div>
                <label class="form-label col-xs-4 col-sm-2">联系电话：</label>
                <div class="formControls col-xs-8 col-sm-3">
                    <input type="text" name="cl_tel" id="cl_tel" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> value="<%=cl_tel%>" class="input-text ">
                </div>
            </div>--%>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">备注：</label>
                <div class="formControls col-xs-8 col-sm-8">
                <textarea name="beizhu" cols="" rows="2" class="textarea" <%if("2".equals(ly_status_view)||"3".equals(ly_status_view)){%>readonly<%}%> placeholder=""
                          dragonfly="true"><%=beizhu%></textarea>
                </div>
            </div>
        </div>
    </form>
</article>
<div>
    <%if(!"1".equals(ly_status_view)&&!"2".equals(ly_status_view)&&!"3".equals(ly_status_view)){%>
    <a class="btn btn-primary radius" onclick="addRules('资产添加','<%=telSegmentUrl%>');" >
        添加
    </a>
    <%}%>
</div>
<div class="table-list">
    <table class="table  table-hover table-bg">
        <thead>
        <tr class="text-c">
            <th width="50px">资产名称</th>
            <th width="50px">资产分类</th>
            <th width="50px">设备类型</th>
            <th width="50px">价值</th>
            <th width="50px">数量</th>
            <%if(!"2".equals(ly_status_view)&&!"3".equals(ly_status_view)){%>
            <th width="50px">操作</th>
            <%}%>
        </tr>
        </thead>
        <tbody>
        <%
            if (v_list_1 != null && v_list_1.size() > 0) {
                for (int i = 0; i < v_list_1.size(); i++) {
                    Hashtable ht_view = (Hashtable) v_list_1.get(i);
                    String reg_id_view=pub.trimNull((String)ht_view.get("zc_id"));
                    //int counts=getCounts(cf,conn,ly_id,reg_id_view);
                    String zc_name_view=pub.trimNull((String)ht_view.get("zc_name"));
                    String zc_fl_view=pub.trimNull((String)ht_view.get("zc_fl"));
                    String sb_type_view=pub.trimNull((String)ht_view.get("sb_type"));
                    String jiazhi_view=pub.trimNull((String)ht_view.get("jiazhi"));
                    if("".equals(jiazhi_view)){
                        jiazhi_view="0";
                    }
                    int jiazhi = Integer.parseInt(jiazhi_view)*1;

        %>
        <tr <%if(i%2==0){%>id="tr_doub"<%}else{%>id="tr_single"<%}%> class="text-c">
            <td>
                <%=zc_name_view%>
            </td>
            <td>
                <%=zc_fl_view%>
            </td>
            <td>
                <%=sb_type_view%>
            </td>
            <td>
                <%=jiazhi%>
            </td>
            <td>
                1
            </td>
            <%if(!"2".equals(ly_status_view)&&!"3".equals(ly_status_view)){%>
            <td>
                <a title="删除" href="javascript:void(0);" onclick="deleteById('<%=reg_id_view%>','<%=ly_id%>');" class="ml-5" >
                    <i class="Hui-iconfont">删除</i>
                </a>
            </td>
            <%}%>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</div>
<div class="cl mb-10  bk-gray " style="height: 50px;">
    <div class="col-sm-3 pd-10" style="width:100%;text-align: center;">
        <input class="btn btn-primary radius btn-green" type="button" onclick="javascript:saveForm('tj');" value="&nbsp;&nbsp;申请&nbsp;&nbsp;">
        <input class="btn btn-primary radius btn-green" type="button" onclick="javascript:saveForm('save');" value="&nbsp;&nbsp;保存&nbsp;&nbsp;">
    </div>
</div>
<script>
    function addRules(title, url) {
        var action = $("#action").val();
        if(action!='update'){
            layer.msg('请先保存再添加', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {

            });
            return false;
        }
        var abc = layer.open({
            title:title,
            type: 2,
            content: url,
            area: ['100%', '100%'],
            maxmin: true
        });
    }
    function closeIframe() {
        parent.searchModel();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    function saveForm(type){
        if(type=='tj'){
            $("#action").val("shenqing");
        }
        if($("#xm_id_id").val()==''){
            layer.msg('所属项目不能为空', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }if($("#ly_dept").val()==''){
            layer.msg('领用单位不能为空', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }if($("#ly_user").val()==''){
            layer.msg('领用人不能为空', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }if($("#ly_time").val()==''){
            layer.msg('领用时间不能为空', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }
        if($("#tel").val().length!=8&&$("#tel").val().length!=11){
            layer.msg('联系电话应为8位或11位', {
                icon: 5,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }
        $.ajax({
            type: "POST",
            url: "./action.jsp",
            data: $('#form-admin-add').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            $("#action").val("update");
                            parent.searchModel();
                            if(type=='tj'){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        });
                } else {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                    });
                }
            }
        });
    }
    function deleteById(id,ly_id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: "POST",
                url: "./action.jsp?action=delete&delete_id="+id+"&ly_id="+ly_id,
                data: $('#form-admin-add').serialize(),
                success: function (msg) {
                    if (msg.indexOf("success") >= 0) {
                        layer.msg('操作成功!', {icon: 1, time: 1000},
                            function () {
                                location.reload();
                                parent.searchModel();
                            });
                    } else {
                        layer.msg('操作失败', {
                            icon: 5,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                        });
                    }
                }
            });
        })
    }
</script>
<%
    } catch (Exception e) {
        out.println("查询异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>