<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript">
layui.config({
    base: '${ctxp}/static/plugin/assets/'
}).extend({
    treetable: 'treetable-lay/treetable'
}).use(['layer', 'table', 'treetable'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var treetable = layui.treetable;
    var renderTable1 = function () {
    layer.load(2);
    treetable.render({
    treeColIndex: 1,
    treeSpid: -1,
    treeIdName: 'id',
    treePidName: 'pid',
    treeDefaultClose: false,
    treeLinkage: false,
    id:'checktable',
    elem: '#longtable',
    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":"${data.wbsCode}",
        "wbsys": "${data.describe}",
        "wbsCode": "${data.wbsCode}",
        "bl":'',
        "fee":'',
        "pid": "${data.parentNode}"
    },
    </c:forEach>
    </c:if>
    ],
    done: function () {
    $(".layui-icon-triangle-d").closest('tr').find("[data-field='bl']").data('edit', false);
    $(".layui-icon-triangle-d").closest('tr').find("[data-field='fee']").data('edit', false);
    layer.closeAll('loading');
}
});
};
    renderTable1();
    //计算比例/金额
    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;
}
});
    $('#btn-search').click(function () {
    var keyword = $('#edt-search').val();
    var searchCount = 0;
    $('#longtable').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
    $(this).css('background-color', 'transparent');
    var text = $(this).text();
    if (keyword != '' && text.indexOf(keyword) >= 0) {
    $(this).css('background-color', 'rgba(250,230,160,0.5)');
    if (searchCount == 0) {
    treetable.expandAll('#longtable');
    $('html,body').stop(true);
    $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
}
    searchCount++;
}
});
    if (keyword == '') {
    layer.msg("请输入搜索内容", {icon: 5,offset:['40%','40%']});
} else if (searchCount == 0) {
    layer.msg("没有匹配结果", {icon: 5,offset:['40%','40%']});
}
});

    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/bddtz",
    // 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>