<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/webpage/include/taglib.jsp"%>
<%@ attribute name="url" type="java.lang.String" required="true"%>
<%-- 使用方法： 1.将本tag写在查询的form之前；2.传入controller的url --%>
    <button id="btnImport" class="nav-btn nav-btn-import" data-toggle="tooltip" data-placement="left" title="导入"><i class="fa fa-folder-open-o"></i> 导入</button>
<div id="importBox" class="hide">
		<form id="importForm" action="${url}" method="post" enctype="multipart/form-data"
			 style="padding-left:20px;text-align:center;" onsubmit="loading('正在导入，请稍等...');"><br/>
			<input id="uploadFile" name="file" type="file" style="width:330px"/>导入文件不能超过5M，仅允许导入“xls”或“xlsx”格式文件！<br/>　　
		</form>
</div>

<div id="importDetail" class="hide single-form">
    <div class="container">

    <form id="saveImportForm" action="${ctx}/workreceiptsregister/workReceiptsRegister/saveImport" method="post">
        <div class="form-group layui-row first">
            <div class="form-group-label"><h2>收入明细</h2></div>
            <div class="layui-item layui-col-xs12 form-table-container" >
                <table id="contentTable" class="table table-bordered table-condensed can-edit read-only no-bottom-margin">
                    <thead>
                    <tr>
                        <th width="50px;"> <input id="registerId" type="checkbox" onchange="checkRegister(this);"></th>
                        <th>到账时间</th>
                        <th>到账金额</th>
                        <th>对方账号</th>
                        <th>对方户名</th>
                    </tr>
                    </thead>
                    <tbody id="workRegisterList">
                    </tbody>
                </table>
            </div>
        </div>
        <div class="form-group layui-row page-end"></div>
    </form>
    <script type="text/javascript">
        function checkRegister(obj) {
            var checkVal = $(obj).attr("checked");
            if(checkVal == "checked") {
                $("#workRegisterList input[type='checkbox']").prop("checked", "checked");
                top.$("#workRegisterList input[type='checkbox']").prop("checked", "checked");
            }else {
                $("#workRegisterList input[type='checkbox']").prop("checked", "");
                top.$("#workRegisterList input[type='checkbox']").prop("checked", "");
            }
        }
        function checkChange(obj) {
            var idx = $(obj).attr("id");
            var checkVal = $(obj).attr("checked");
            if(checkVal == "checked") {
                top.$("#"+idx).prop("checked", "checked");
            }else {
                top.$("#"+idx).prop("checked", "");
            }
        }
    </script>
</div>
</div>

<script type="text/template" id="workRegisterTpl">//<!--
					<tr id="registers{{idx}}">
                <td> <input type="checkbox" id="{{idx}}" onchange="checkChange(this);"></td>
                <td>
                    <input id="registers{{idx}}_arrivalDate" name="registers[{{idx}}].arrivalDate" type="text" value="{{row.arrivalDate}}"  readOnly="true"  class="form-control "/>
                </td>
                <td>
                    <input id="registers{{idx}}_arrivalAmt" name="registers[{{idx}}].arrivalAmt" type="text"  value="{{row.arrivalAmt}}" onchange="formatNum(this);" readOnly="true"  class="form-control  money"/>
                </td>
                <td>
                    <input id="registers{{idx}}_clientAccno" name="registers[{{idx}}].clientAccno" type="text" value="{{row.clientAccno}}"  readOnly="true"  class="form-control "/>
                </td>
                <td>
                    <input id="registers{{idx}}_clientAccname" name="registers[{{idx}}].clientAccname" type="text" value="{{row.clientAccname}}" readOnly="true"  class="form-control required"/>
                </td>
				</tr>//-->
</script>
<script type="text/javascript">
    var workRegisterIdx = 0;
    var workRegisterTpl = $("#workRegisterTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
$(document).ready(function() {
	$("#btnImport").click(function(){
		top.layer.open({
		    type: 1,
		    area: [800, 500],
		    title:"导入数据",
            skin: 'two-btns',
		    content:$("#importBox").html() ,
		    btn: ['确定', '关闭'],
			    btn1: function(index, layero){
                    var inputForm =top.$("#importForm");
                    // var fileObj = top.$("#uploadFile")[0].files[0];
                    top.$("#importForm").ajaxSubmit({
                            dataType: "json",
                            success: function (result) {
                                var data = result.data;
                                for (var i=0; i<data.length; i++){
                                    var arrivalAmt = data[i].arrivalAmt;
                                    if(arrivalAmt!=null){
                                        var money = parseFloat((arrivalAmt + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
                                        var l = money.split(".")[0].split("").reverse(),
                                            r = money.split(".")[1];
                                        t = "";
                                        for(j = 0; j < l.length; j ++ )
                                        {
                                            t += l[j] + ((j + 1) % 3 == 0 && (j + 1) != l.length ? "," : "");
                                        }
                                        data[i].arrivalAmt = t.split("").reverse().join("") + "." + r;
                                    }
                                    addRow('#workRegisterList', workRegisterIdx, workRegisterTpl,data[i]);
                                }

                                // var htmlCont = result;
                                top.layer.close(index);
                                top.layer.open({
                                    type: 1,
                                    area: ['95%', '95%'],
                                    title:"导入数据",
                                    skin: 'two-btns',
                                    content:$("#importDetail").html(),
                                    // content:htmlCont,
                                    btn: ['提交', '关闭'],
                                    btn1: function(index, layero){
                                        if(!selectLines()){
                                            parent.layer.msg("请至少选择一条导入记录！", {icon: 5});
                                            return false;
                                        }
                                        var inputForm =top.$("#saveImportForm");
                                        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
                                        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
                                        inputForm.submit();
                                        top.layer.close(index);
                                        workRegisterIdx = 0;
                                        $("#workRegisterList tr").remove();
                                    },
                                    btn2: function(index, layero){
                                        top.layer.close(index);
                                        workRegisterIdx = 0;
                                        $("#workRegisterList tr").remove();
                                    }
                                });
                            },
                            error: function () {
                                top.layer.msg("导入收入登记失败！");
                            }
                        });
                },
		    btn2: function(index, layero){
                top.layer.close(index);
            }
		});
	});
});

    function selectLines(){
        //获得名称为isSelect的所有checkbox
        var check = top.$("#workRegisterList input[type='checkbox']");
        var length = check.length;
        //循环每条记录
        for(var i=0; i<check.length;i++){
            var checkVal = $(check[i]).attr("checked");
            if(checkVal=="checked"){

            }else {
                var idx = $(check[i]).attr("id");
                top.$("#registers"+idx+"_arrivalDate").removeAttr("name");
                top.$("#registers"+idx+"_arrivalAmt").removeAttr("name");
                top.$("#registers"+idx+"_clientAccno").removeAttr("name");
                top.$("#registers"+idx+"_clientAccname").removeAttr("name");
                length--;
            }
        }
        if(length>0){
            return true;
        }else {
            return false;
        }
    }

    function addRow(list, idx, tpl, row){
        bornTemplete(list, idx, tpl, row, idx);
        workRegisterIdx +=1;
    }

    function bornTemplete(list, idx, tpl, row, idx1){
        var idx1 = $("#workRegisterList tr").length +1;
        $(list).append(Mustache.render(tpl, {
            idx: idx, delBtn: true, row: row,
            order:idx1 + 1, idx1:idx1
        }));
        $(list+idx).find("select").each(function(){
            $(this).val($(this).attr("data-value"));
        });
        $(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
            var ss = $(this).attr("data-value").split(',');
            for (var i=0; i<ss.length; i++){
                if($(this).val() == ss[i]){
                    $(this).attr("checked","checked");
                }
            }
        });
    }
</script>