<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 2019/8/11
  Time: 9:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
    <title>单独调整</title>
    <link rel="stylesheet" href="${ctxp}/static/plugin/assets/layui/css/layui.css">
    <link rel="stylesheet" href="${ctxp}/static/plugin/assets/common.css"/>
    <script type="text/javascript" src="${ctxp}/static/plugin/assets/jquery-3.2.1.min.js"></script>
    <script src="${ctxp}/static/plugin/assets/layui/layui.js"></script>
</head>
<style>
    .layui-table-view .layui-table {width:100%}
    .layui-table-cell .layui-form-checkbox[lay-skin="primary"]{

        top: 50%;

        transform: translateY(-50%);

    }
</style>
<body>
<div class="layui-btn-group suretable" style="display: none;">
    <button class="layui-btn"  data-type="getCheckData" id="freenl">确定</button>
</div>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="" style="margin-left: 10px;margin-top: 10px;">
            <span id="mylable2">项目调整</span>
            <span>:</span>
            <span id="readid2">200</span>
        </div>
        <div class="" style="margin-left: 10px;">
            <input type="radio" name="mydiv" value="1" title="增加">
            <input type="radio" name="mydiv" value="2" title="减少">
        </div>
    </div>
</form>
<!--<div>-->
<table class="layui-hide" id="longtable" lay-filter="longtable"></table>
<!--</div>-->



<script>
    layui.use(['table','form','layedit', 'laydate'], function(){
        var table = layui.table
            ,form = layui.form;
        form.render();
        table.render({
            elem: '#longtable'
            ,title: '单独调整页'
            ,id:'checktable'
            ,cols: [[
                {type: 'checkbox',width:30}
                ,{field:'wbsys',width:100,title:'分部分项'}
                ,{field:'wbsCode',width:120,title:'WBS识别码'}
                ,{field:'bl',width:100,title:'比例(%)',edit: 'text'}
                ,{field:'fee',width:100,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>
                // {
                //     "id":"2",
                //     "wbsys": "杆塔工程",
                //     "wbsCode": "39200000",
                //     "bl":'',
                //     "fee":''
                // },
                // {
                //     "id":"3",
                //     "wbsys": "接地工程",
                //     "wbsCode": "39300000",
                //     "bl":'',
                //     "fee":''
                // },
                // {
                //     "id":"4",
                //     "wbsys": "架线工程",
                //     "wbsCode": "39400000",
                //     "bl":'',
                //     "fee":''
                // },
                // {
                //     "id":"5",
                //     "wbsys": "附件安装工程",
                //     "wbsCode": "39500000",
                //     "bl":'',
                //     "fee":''
                // },
                // {
                //     "id":"6",
                //     "wbsys": "辅助工程",
                //     "wbsCode": "39600000",
                //     "bl":'',
                //     "fee":''
                // }
            ]
            ,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('请输入有效数字');
                    return false;
                }
                var bfb = (blvalue*moneycount).toFixed(2);
                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('请输入有效数字');
                    return false;
                }
                var bfb2 = (feevalue*100).toFixed(2);
                $(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('请选择操作方式');
                    return false;
                }
                var jsondata=JSON.stringify(data);
		     // var e= jsondata.stringify(jsondata);
                var objdata = JSON.parse(jsondata);
             //    var objdata = JSON.stringify(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('比例/金额分配不正确!');
                    return false;
                }
                var newid2 = parent.$('#newid').val();
                var xmmc2 = parent.$('#xmmc1').val();
                var fyje2 = parent.$('#fyje1').val();

                $.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>
</body>
</html>
