<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript">
    layui.use(['table','form','layedit', 'laydate'], function(){
        var table = layui.table //生产数据表格
        ,form = layui.form;
        form.render();//表格渲染
        table.render({
        elem: '#longtable'
        ,title: '单独调整页'
        ,id:'checktable'
        ,limit:Number.MAX_VALUE
        ,cols: [[ //生产表头
            {type: 'checkbox'}
            ,{field:'wbsys',title:'分部分项'}
            ,{field:'wbsCode',title:'WBS识别码'}
            ,{field:'bl',title:'比例(%)',edit: 'text'}
            ,{field:'fee',title:'分摊金额',edit: 'text'}
        ]]
        ,data:[ //数据导入
        <c:if test="${ not empty list}">
        <c:forEach items="${list}" var="data" varStatus="index">
        {
            "id":"1",
            "wbsys": "${data.describe}",
            "wbsCode": "${data.wbsCode}",
            "bl":'',
            "fee":''
        },
        </c:forEach>
        </c:if>
        ]
        ,page: false
    });
    //计算比例/金额
    table.on('edit(longtable)', function(obj){
        var field = obj.field;
        var moneycount = $("#readid2").text();
        if(field == 'bl'){
            var value = obj.value;
            var data = obj.data;
            var blvalue = value/100;
            if(value==''){
                var w = $(this).closest("tr").find("td").eq("4").find("div").html("");
                data.fee="";
                return false;
            }
            if(isNaN(blvalue)){
                layer.msg('请输入有效数字',{icon: 5,offset:['40%','40%']});
                return false;
            }
            var bfb = (blvalue*moneycount);
            var w = $(this).closest("tr").find("td").eq("4").find("div").html(bfb);
            data.fee=bfb;
        }else{
            var value = obj.value;
            var data = obj.data;
            var feevalue = value/moneycount;
            if(value==''){
            $(this).closest("tr").find("td").eq("3").find("div").html("");
            data.bl="";
            return false;
        }
        if(isNaN(value)){ //判断输入是否是数字
            layer.msg('请输入有效数字',{icon: 5,offset:['40%','40%']});
            return false;
        }
        var bfb2 = (feevalue*100);
        $(this).closest("tr").find("td").eq("3").find("div").html(bfb2);
        data.bl=bfb2;
        }
    });
    var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
            var checkStatus = table.checkStatus('checktable'),
            data = checkStatus.data;
            var moneycount2 = $("#readid2").text();
            var val = $('input[name="mydiv"]:checked').val();
            if(val==null){
            layer.msg('请选择操作方式',{icon: 5,offset:['40%','40%']});
            return false;
        }
        var jsondata=JSON.stringify(data);
        var objdata = JSON.parse(jsondata);
        var blcount = 0;
        var feecount = 0;
        for (var i = 0;i<objdata.length;i++) {
            blcount=Number(blcount)+Number(objdata[i].bl) ;
            feecount=Number(feecount)+Number(objdata[i].fee);
        }
        if(blcount!=100||feecount!=moneycount2){
            layer.msg('比例/金额分配不正确!',{icon: 5,offset:['40%','40%']});
            return false;
        }
        var newid2 = parent.$('#newid').val();//获取父类input框的值
        var xmmc2 = parent.$('#xmmc1').val();
        var fyje2 = parent.$('#fyje1').val();
        layer.msg('加载中', {
            icon: 16
            ,shade: 0.01
            ,time:false
            ,offset:['40%','40%']
        });
        $.ajax({
            type:"post",
            url:"${ctx}/jkxl/ddtz",
            // data:JSON.stringify({id:newid2,type:xmmc2,fee:fyje2}),
            data:{id:newid2,type:xmmc2,fee:fyje2,tableData:jsondata,state:val },
            success:function(result){
                parent.location.reload();
            }
        });
    }
    };
        $('.suretable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var newid2 = parent.$('#newid').val();
        var xmmc2 = parent.$('#xmmc1').val();
        var fyje2 = parent.$('#fyje1').val();
        $("#mylable2").text(xmmc2);
        $("#readid2").text(fyje2);
    });
</script>