<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
    ,layer = layui.layer
    ,layedit = layui.layedit
    ,laydate = layui.laydate;

    //日期
    laydate.render({
            elem: '#beginDate1' //工程开始时间
        });
        laydate.render({
            elem: '#endDate1' //结束时间
        });
        laydate.render({
            elem: '#beginDate2' //项目开始时间
        });
        laydate.render({
            elem: '#endDate2' //项目结束时间
        });
    });
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: 0,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            treeDefaultClose: true,//默认折叠
            treeLinkage: true,
            elem: '#table1',
            cols: [[
                {field: 'gcmc',minWidth:200,width:260,title: '工程名称'},
                {field:'xmsl',width:145,title:"结算书数量"},
                {field:'cjsq',width:145,title: '创建时期'},
                {field:'djr',width:130,title:'登记人'},
                {field:'op',align:'center',title:"操作",minWidth:500,maxWidth:700,templet:function(d){
                        ////对操作进行初始化
                        var xml = "";
                        if(d.status==1){ //工程按钮
                            xml +="<div><a style='margin-top: 3px;' class='layui-btn layui-btn-primary layui-btn-xs' onclick=\"look( '${ctx}/project/form?id=" + d.id + "&tabId=1')\">查看</a>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-xs'  onclick=\"update( '${ctx}/project/form?id=" + d.id + "&tabId=2')\">修改</a>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-normal layui-btn-xs' lay-event='add'>添加结算书</a>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-xs layui-btn-danger' id=delPro"+d.id+" onclick=\"deleteProject('"+d.id+"')\">删除</a>"
                                // +"<br>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-warm layui-btn-xs' lay-event=''href=\"${ctx}/project/exportone?id=" + d.id+"\">导出wbs工程</a>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-normal layui-btn-xs'onclick=\"withhold( '${ctx}/project/withholdform?id=" + d.id + "&tabId=1')\">诚信扣款</a></div>";
                                // +"<a style='margin-top: 3px;' class='layui-btn layui-btn-warm layui-btn-xs'onclick=\"materialOffset( '${ctx}/project/materialOffsetForm?id=" + d.id + "&tabId=1')\">甲供材抵扣</a>"
                                // +"<a style='margin-top: 3px;' class='layui-btn layui-btn-warm layui-btn-xs'onclick=\"taxDeductions( '${ctx}/project/taxDeductionsForm?id=" + d.id + "&tabId=1')\">税额扣减</a></div>";

                        }
                        if(d.status==2){ //项目表头操作
                            xml +="<span class='myspan' self-id ="+d.pid+">操作</span>";
                        }
                        if(d.status==3){ //项目类型
                            xml +="<a style='margin-top: 3px;' self-id="+d.pid+" class='layui-btn layui-btn-xs' href=\"${ctx}/jkxl/list?id="+d.id+"&type="+d.type+"\">编辑</a>"
                                +"<a style='margin-top: 3px;' class='layui-btn layui-btn-xs layui-btn-danger' id=del"+d.id+" onclick=\"deleteItem('" + d.id + "')\">删除</a>";
                        }

                        return xml;
                    }}
            ]],
            data: [
                <c:if test="${ not empty page.list}">
                    <c:forEach items="${page.list}" var="data" varStatus="index">
                        {
                            "id": "${data.id}",
                            "gcmc":"${data.projectName}",
                            "xmsl": "${data.number}",
                            "cjsq": "<fmt:formatDate value="${data.createDate}" pattern="yyyy/MM/dd"/>",
                            "djr":"${data.userName}",
                            "status":"1",
                            "pid": -1
                        },
                        {
                            "id": "2",
                            "gcmc":"结算书名称",
                            "xmsl": "供应商",
                            "cjsq": "结算书划分",
                            "djr":"创建时间",
                            "status":"2",
                            "pid": "${data.id}"
                        },
                    </c:forEach>
                </c:if>
                <c:forEach items="${page.list}" var="item" varStatus="index">
            <c:forEach items="${item.wbsItems}" var="items" varStatus="index">
            {
                "id": "${items.id}",
                "type":"${items.type}",
                "gcmc":"${items.itemName}",
                "xmsl": "${items.unit}",
                "cjsq":"<c:if test="${items.type eq '1'}">架空线路</c:if><c:if test="${items.type eq '2'}">电缆线路</c:if><c:if test="${items.type eq '3'}">变电站工程</c:if>",
                "djr":"<fmt:formatDate value="${items.createDate}" pattern="yyyy/MM/dd"/>",
                "status":"3",
                "pid": "${items.projectId}"
    },
    </c:forEach>
    </c:forEach>
    ],
        done: function () {
            $(".myspan").closest("tr").css('background-color','#f2f2f2');
            // $("#br_div").closest("tr").css('height','break-word');
            layer.closeAll('loading');
        }
    });
    };
    renderTable1();
    // treetable.foldAll('#table1');
    table.on('tool(table1)', function(obj,target){
        var data = obj.data;
        if(obj.event == 'add'){ //添加项目
            $("#addtext").val(data.id);
            layui.use(['table','layer'],function(){
                layer.open({
                    type: 2,
                    title: '添加结算书',
                    offset:['7%','20%'],
                    area: ['60%', '90%'],
                    shade: 0.8,
                    shadeClose: true,
                    content: '${ctxp}/webpage/modules/sg/project/addItemForm.jsp'
                    , btn: ['确定','关闭']
                    ,yes: function(index, layero){
                        // var bodyparent = layer.getChildFrame('body', index);
                        // bodyparent.find('#newProject').click();
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var inputForm = body.find('#inputForm');
                        var top_iframe;
                        if(target){
                            top_iframe = target;//如果指定了iframe，则在改frame中跳转
                        }else{
                            top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
                        }
                        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
                        iframeWin.contentWindow.doSubmit();

                        // layer.close(index);//关闭对话框。

                    }
                    ,btn2: function(index, layero){
                        layer.close(index);
                    }
                })
            })
        }
    });
});
function refresh(){ //刷新
    $("#fromSumbit").submit();
}
function addProject(){ //添加工程
    layui.use(['table','layer'],function(){
        layer.open({
            type: 2,
            title: '添加工程',
            offset:['7%','20%'],
            area: ['60%', '90%'],
            shade: 0.8,
            shadeClose: true,
            content: '${ctxp}/webpage/modules/sg/project/addprojectForm.jsp'
            , btn: ['确定','关闭']
            ,yes: function(index, layero){
                var bodyparent = layer.getChildFrame('body', index); //当前页面 获取子类（弹出层body）的属性
                bodyparent.find('#btnSub').click();  //获取子类页面的按钮,点击事件。
            }
            ,btn2: function(index, layero){
                layer.close(index);
            }
        })
    })
}
function look(url) { //工程 查看
    layer.open({
        type: 2,
        title: '查看',
        area: ['70%', '90%'],
        offset:['7%','14%'],
        shade: 0.8,
        shadeClose: true,
        fixed: true, //不固定
        maxmin: true,
        content: url,//查看的路径
        btn: ['关闭']
    })
}

    function withhold(url) { //诚信扣款
       layer.open({
        type: 2,
        title: '诚信扣款',
        area: ['70%', '90%'],
        offset:['7%','14%'],
        shade: 0.8,
        shadeClose: true,
        fixed: true, //不固定
        maxmin: true,
        content: url,//查看的路径
        btn: ['确定','关闭'],
           yes: function(index, layero){
                 var bodyparent = layer.getChildFrame('body', index); //获取子类页面body下内容
                 bodyparent.find('#updatebtn').click(); // 点击修改页面隐藏的按钮
               }
           ,btn2: function(index, layero){
               layer.close(index);
               }

    })
}
    function materialOffset(url) { //甲供材扣抵
    layer.open({
        type: 2,
        title: '甲供材扣抵',
        area: ['70%', '90%'],
        offset:['7%','14%'],
        shade: 0.8,
        shadeClose: true,
        fixed: true, //不固定
        maxmin: true,
        content: url,//查看的路径
        btn: ['确定','关闭'],
        yes: function(index, layero){
            var bodyparent = layer.getChildFrame('body', index); //获取子类页面body下内容
            bodyparent.find('#updatebtn').click(); // 点击修改页面隐藏的按钮
        }
        ,btn2: function(index, layero){
            layer.close(index);
        }

    })
}

    function taxDeductions(url) { //税金处理
    layer.open({
        type: 2,
        title: '税金处理',
        area: ['70%', '90%'],
        offset:['7%','14%'],
        shade: 0.8,
        shadeClose: true,
        fixed: true, //不固定
        maxmin: true,
        content: url,//查看的路径
        btn: ['确定','关闭'],
        yes: function(index, layero){
            var bodyparent = layer.getChildFrame('body', index); //获取子类页面body下内容
            bodyparent.find('#updatebtn').click(); // 点击修改页面隐藏的按钮
        }
        ,btn2: function(index, layero){
            layer.close(index);
        }
    })
}
function update(url) { //修改
    layer.open({
        type: 2,
        title: '修改工程',
        area: ['70%', '90%'],
        offset:['6%','14%'],
        shade: 0.8,
        shadeClose: true,
        fixed: false, //不固定
        maxmin: true,
        content: url
        , btn: ['确定','关闭']
        ,yes: function(index, layero){
            var bodyparent = layer.getChildFrame('body', index); //获取子类页面body下内容
            bodyparent.find('#updatebtn').click(); // 点击修改页面隐藏的按钮
        }
        ,btn2: function(index, layero){
            layer.close(index);
        }
    })
}
function deleteItem(id) { //逻辑删除项目
    layer.msg('您确定要删除吗？', {
         icon: 3
        , time: 0 //不自动关闭
        ,offset:['38%','38%']
        , btn: ['确定', '取消']
        , yes: function (index) {
            $.ajax({
                type: "post",
                url: "${ctx}/project/tem/deleteItem",
                data: {'id': id},
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.close(index);
                        var text = $("#delPro"+data.msg).closest('tr').find("td").eq("1").find("div").html();
                        $("#del"+id).closest('tr').remove();
                        if (text-1 >= 0){
                            $("#delPro"+data.msg).closest('tr').find("td").eq("1").find("div").html(text-1);
                        }else {
                            $("#delPro"+data.msg).closest('tr').find("td").eq("1").find("div").html(0);
                        }
                    } else {
                        layer.msg(data.msg, {icon: 5,offset:['40%','40%']});
                    }
                }
            })
        }
    })
}
function deleteProject(id) {//删除工程
    layer.msg('您确定要删除吗？', {
        icon: 3
        , time: 0 //不自动关闭
        , btn: ['确定', '取消']
        ,offset:['38%','38%']
        , yes: function (index) {
            $.ajax({
                type: "post",
                url: "${ctx}/project/deleteProject",
                data: {'id': id},
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.close(index);
                        $("[self-id="+id+"]").closest('tr').remove();
                        $("#delPro"+id).closest('tr').remove();
                    } else {
                        layer.msg(data.msg, {icon: 5,offset:['37%','37%']});
                    }
                }
            })
        }
    })
}
$(document).ready(function(e) { //触发隐藏的 搜索框
    $("#noneBtn").click(function(e) {
        $("#noneDiv").toggle();
    })
});
</script>