<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript">
    var str = ''
    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: [[ //生产表头
            {field:'itemName',title:'项目名称'}
            ,{field:'unitType',title:'单位工程',edit: 'text'}
            ,{field:'integrityFee',title:'诚信扣款'}
            ,{field:'materialoffset',title:'扣抵工程款'}
            ,{field:'taxdeductions',title:'税额扣减'}

        ]]
        ,data:[ //数据导入
        <c:if test="${ not empty list}">
        <c:forEach items="${list}" var="data" varStatus="index">
        {
            "id":"${data.id}",
            "unitType": "${data.unitType}",
            "integrityFee": "${data.integrityFee}",
            "itemName": "${data.itemName}",
            "materialoffset":"${data.decFund}",
            "taxdeductions":"${data.texDeal}"
        },
        </c:forEach>
        </c:if>
        ]
        ,page: false
        ,done: function(res, curr, count){
                    for(var p in res.data){
                        str = str+res.data[p].id+',';
                     }
                 $('table tr').each(function () {
                      var unitType = $(this).find('td:eq(1)').text()
                        $(this).find('td:eq(3)').attr("data-edit","text")
                        $(this).find('td:eq(4)').attr("data-edit","text")
                        $(this).find('td:eq(2)').attr("data-edit","text")
                      if(unitType != null && unitType != ''){
                         $(this).find('td:eq(2)').attr("data-edit","text")
                      }
                })
              }
          });

    table.on('edit(longtable)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
       // console.log(obj.data); //所在行的所有相关数据
       var field = obj.field;
       if(field === 'unitType'){
           var value = obj.value;
           var flag = false;
           var fee1 = '';
           // $(this).closest("tr").find("td").eq("2").find("div").html(value);
           $(this).closest("tr").find("td").eq("2").attr("data-edit","text");

           if(value == ''){
               obj.data.integrityFee = ''
               // $('table tr').each(function () {
               //     var unitType = $(this).find('td:eq(1)').text()
               //      if(unitType == ''){
               //        $(this).find('td:eq(2)').text('')
               //     }
               //  })
              $(this).closest("tr").find("td").eq("2").find("div").html('');
              // $(this).closest("tr").find("td").eq("2").removeAttr("data-edit");
           }else{
              $('table tr').each(function () {
                  var unitType = $(this).find('td:eq(1)').text()
                  var fee = $(this).find('td:eq(2)').text()
                   if(unitType == value){
    // $(this).closest("tr").find("td").eq("2").find("div").html(fee);
                    flag = true;
                    fee1 = fee;
                   }
               })
              if(flag){
                    $(this).closest("tr").find("td").eq("2").find("div").html(fee1);
            }

          }
        }
        if(field === 'integrityFee'){
           var value = obj.value;
           if(isNaN(value)){ //判断输入是否是数字
                layer.msg('请输入有效数字',{icon: 5,offset:['40%','40%']});
                 return false;
           }
            if(value<0){
                layer.msg('诚信扣款不能为负数',{icon: 5,offset:['40%','40%']});
                return false;
            }
        }
        if(field === 'materialoffset'){
            var value = obj.value;
            if(isNaN(value)){ //判断输入是否是数字
                layer.msg('请输入有效数字',{icon: 5,offset:['40%','40%']});
                return false;
            }
            if(value<0){
                layer.msg('扣抵工程款不能为负数',{icon: 5,offset:['40%','40%']});
                return false;
            }
        }
        if(field === 'taxdeductions'){
            var value = obj.value;
            if(isNaN(value)){ //判断输入是否是数字
                layer.msg('请输入有效数字',{icon: 5,offset:['40%','40%']});
                return false;
            }
            if(value<0){
                layer.msg('税金不能为负数',{icon: 5,offset:['40%','40%']});
                return false;
            }
        }
    });
    });
    function updateProject(){
        var nameString = "";
        var nameString1 = "";
        var materialString = "";
        var taxString = "";
        $('table tr').each(function () {
            var unitType = $(this).find('td:eq(1)').text();
            var fee = $(this).find('td:eq(2)').text();
            var materialOffset = $(this).find('td:eq(3)').text();
            var taxDeductions = $(this).find('td:eq(4)').text();
            if(unitType == ''){
               unitType = null
            }else{
              if(fee == ''){
                layer.msg('您已输入单位工程,该诚信扣款不能为空',{icon: 5,offset:['40%','40%']});
                throw SyntaxError;
              }
            }
            if(fee == ''){
                fee = null
            }else{
                judge(fee,'诚信扣款');
            }
            if(materialOffset == ''){
                materialOffset = null;
            }else{
                judge(materialOffset,'扣抵工程款');
            }
            if(taxDeductions == ''){
                taxDeductions = null;
            }else{
                judge(taxDeductions,'税额扣减');
            }
            nameString += unitType+",";
            nameString1 += fee+",";
            materialString += materialOffset+",";
            taxString+=taxDeductions+",";
        })
        var unitTypes = nameString.substring(5,nameString.length);
        var fees = nameString1.substring(5,nameString1.length);
        var materialStrings = materialString.substring(5,materialString.length);
        var taxStrings= taxString.substring(5,taxString.length);
        var ids = str.substring(0,str.length-1);
        $.ajax({
        type:"post",
        url:"${ctx}/project/withhold",
        data:{'ids':ids,'fees':fees,'unitTypes':unitTypes,'decFunds':materialStrings,'texs':taxStrings},
        success:function(result){
           layer.msg(result.msg, {icon: 6,offset:['38%','38%']},function () {
            parent.location.reload();
         });
         }
    });
}

function judge(value,name){
    if(isNaN(value)){ //判断输入是否是数字
        layer.msg(name+':请输入有效数字',{icon: 5,offset:['40%','40%']});
        throw SyntaxError;
    }
        if(value<0){
        layer.msg(name+'不能为负数',{icon: 5,offset:['40%','40%']});
        throw SyntaxError;
    }
}

</script>