<%@ page contentType="text/html;charset=UTF-8" %>
<script>
    $(document).ready(function () {
        //定义方法---在bootstrap数据循环中使用，必须定义在bootstrapTable之前
        window.operateEvents = {
            'change .con': function (e, value, row, index) {
                let val = $('#con' + index).val();
                jp.confirm('确认修改施工费允许少付吗？', function () {
                    jp.loading();
                    jp.get("${ctx}/cost/check/updateCon?projectId=" + row.projectId + "&constructionUnderPayment=" + val, function (data) {
                        if (data.success) {
                            $('#dataTable').bootstrapTable('refresh');
                            jp.success(data.msg);
                        } else {
                            jp.error(data.msg);
                        }
                    });
                },function () {
                    $('#dataTable').bootstrapTable('refresh');
                })
            },
            'change .des': function (e, value, row, index) {
                let val = $('#des' + index).val();
                jp.confirm('确认修改设计费允许少付吗？', function () {
                    jp.loading();
                    jp.get("${ctx}/cost/check/updateDes?projectId=" + row.projectId + "&designUnderPayment=" + val, function (data) {
                        if (data.success) {
                            $('#dataTable').bootstrapTable('refresh');
                            jp.success(data.msg);
                        } else {
                            jp.error(data.msg);
                        }
                    })
                },function () {
                    $('#dataTable').bootstrapTable('refresh');
                })
            },
            'change .sup': function (e, value, row, index) {
                let val = $('#sup' + index).val();
                jp.confirm('确认修改监理费允许少付吗？', function () {
                    jp.loading();
                    jp.get("${ctx}/cost/check/updateSup?projectId=" + row.projectId + "&supervisorUnderPayment=" + val, function (data) {
                        if (data.success) {
                            $('#dataTable').bootstrapTable('refresh');
                            jp.success(data.msg);
                        } else {
                            jp.error(data.msg);
                        }
                    })
                },function () {
                    $('#dataTable').bootstrapTable('refresh');
                })
            }
        };
        $('#dataTable').bootstrapTable({
            //请求方法
            method: 'post',
            //类型json
            dataType: "json",
            contentType: "application/x-www-form-urlencoded",
            //显示检索按钮
            showSearch: true,
            //显示刷新按钮
            showRefresh: true,
            //显示切换手机试图按钮
            showToggle: true,
            //显示 内容列下拉框
            showColumns: true,
            //显示到处按钮
            showExport: true,
            //显示切换分页按钮
            showPaginationSwitch: true,
            //最低显示2行
            minimumCountColumns: 2,
            //是否显示行间隔色
            striped: true,
            //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            cache: false,
            //是否显示分页（*）
            pagination: true,
            //排序方式
            sortOrder: "asc",
            //初始化加载第一页，默认第一页
            pageNumber: 1,
            //每页的记录行数（*）
            pageSize: 10,
            //可供选择的每页的行数（*）
            pageList: [10, 25, 50, 100],
            //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
            url: "${ctx}/cost/check/data",
            //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
            //queryParamsType:'',
            ////查询参数,每次调用是会带上这个参数，可自定义
            queryParams: function (params) {
                var searchParam = $("#searchForm").serializeJSON();
                searchParam.pageNo = params.limit === undefined ? "1" : params.offset / params.limit + 1;
                searchParam.pageSize = params.limit === undefined ? -1 : params.limit;
                searchParam.orderBy = params.sort === undefined ? "" : params.sort + " " + params.order;
                return searchParam;
            },
            //分页方式：client客户端分页，server服务端分页（*）
            sidePagination: "server",
            contextMenuTrigger: "right",//pc端 按右键弹出菜单
            contextMenuTriggerMobile: "press",//手机端 弹出菜单，click：单击， press：长按。
            contextMenu: '#context-menu',
            onContextMenuItem: function (row, $el) {
                if ($el.data("item") == "edit") {
                    edit(row.id);
                } else if ($el.data("item") == "view") {
                    view(row.id);
                } else if ($el.data("item") == "delete") {
                    jp.confirm('确认要删除该数据记录吗？', function () {
                        jp.loading();
                        jp.get("${ctx}/sg/settlement/delete?id=" + row.projectId, function (data) {
                            if (data.success) {
                                $('#dataTable').bootstrapTable('refresh');
                                jp.success(data.msg);
                            } else {
                                jp.error(data.msg);
                            }
                        })

                    });

                }
            },
            onClickRow: function (row, $el) {
            },
            onLoadSuccess: function () {  //加载成功时执行
                getTdValue();
            },
            onShowSearch: function () {
                $("#search-collapse").slideToggle();
            },
            columns: [
                {
                    field: 'projectId',
                    width: '150px',
                    title: '项目编码'
                }
                ,{
                    field: 'projectName',
                    width: '200px',
                    title: '项目名称'
                }
                ,{
                    field: 'constructionAmountDue',
                    width: '130px',
                    title: '施工费应付金额'
                }
                ,{
                    field: 'constructionAmountPaid',
                    width: '130px',
                    title: '施工费实付金额'
                }
                ,{
                    field: 'constructionCostDifference',
                    width: '130px',
                    title: '施工费差额'
                }
                ,{
                    field: 'constructionUnderPayment',
                    width: '130px',
                    title: '施工费允许少付',
                    events: operateEvents,
                    formatter: function (value,row,index) {
                        var item = ${fns:toJson(fns:getDictList('finance_underpayment'))}
                        var option
                        var headOption
                        $.each(item,function (i,obj) {
                            if (value == obj.value) {
                                headOption = headOption + "<option value='" + obj.value + "' selected>" + obj.label + "</option>";
                            } else {
                                headOption = headOption + "<option value='"+obj.value+"'>"+obj.label+"</option>";
                            }
                        })
                        option = '<select class="con" id="con'+index+'" name="sup">' +
                            headOption +
                            '</select>'
                        return option;
                    }
                }
                ,{
                    field: 'designAmountDue',
                    width: '130px',
                    title: '设计费应付金额'
                }
                ,{
                    field: 'designAmountPaid',
                    width: '130px',
                    title: '设计费实付金额',
                }
                ,{
                    field: 'designCostDifference',
                    width: '130px',
                    title: '设计费差额'
                }
                ,{
                    field: 'designUnderPayment',
                    width: '130px',
                    title: '设计费允许少付',
                    events: operateEvents,
                    formatter: function (value,row,index) {
                        var item = ${fns:toJson(fns:getDictList('design_underpayment'))}
                        var option
                        var headOption
                        $.each(item,function (i,obj) {
                            if (value == obj.value) {
                                headOption = headOption + "<option value='" + obj.value + "' selected>" + obj.label + "</option>";
                            } else {
                                headOption = headOption + "<option value='"+obj.value+"'>"+obj.label+"</option>";
                            }
                        })
                        option = '<select class="des" id="des'+index+'" name="sup">' +
                            headOption +
                            '</select>'
                        return option;
                    }
                }
                ,{
                    field: 'supervisorAmountDue',
                    width: '130px',
                    title: '监理费应付金额'
                }
                ,{
                    field: 'supervisorAmountPaid',
                    width: '130px',
                    title: '监理费实付金额',
                }
                ,{
                    field: 'supervisorDifference',
                    width: '130px',
                    title: '监理费差额'
                }
                ,{
                    field: 'supervisorUnderPayment',
                    width: '130px',
                    title: '监理费允许少付',
                    events: operateEvents,
                    formatter: function (value,row,index) {
                        var item = ${fns:toJson(fns:getDictList('supervision_underpayment'))}
                        var option
                        var headOption
                       $.each(item,function (i,obj) {
                           if (value == obj.value) {
                               headOption = headOption + "<option value='" + obj.value + "' selected>" + obj.label + "</option>";
                           } else {
                               headOption = headOption + "<option value='"+obj.value+"'>"+obj.label+"</option>";
                           }
                       })
                        option = '<select class="sup" id="sup'+index+'" name="sup">' +
                            headOption +
                            '</select>'
                        return option;
                    }
                }
            ]
    });


        if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {//如果是移动端
            $('#dataTable').bootstrapTable("toggleView");
        }

        //加载完成后，对第二次是否完成领退料判断变并变更行样式
        function getTdValue() {
            var tableId = document.getElementById("dataTable");
            if (tableId.rows.length>2){
                for(var i = 1;i < tableId.rows.length;i++) {
                    if(tableId.rows[i].cells[4].innerHTML!="-" && tableId.rows[i].cells[4].innerHTML!="0"){
                        tableId.rows[i].cells[4].setAttribute("style","color: red;");
                    }
                    if(tableId.rows[i].cells[8].innerHTML!="-" && tableId.rows[i].cells[8].innerHTML!="0"){
                        tableId.rows[i].cells[8].setAttribute("style","color: red;");
                    }
                    if(tableId.rows[i].cells[12].innerHTML!="-" && tableId.rows[i].cells[12].innerHTML!="0"){
                        tableId.rows[i].cells[12].setAttribute("style","color: red;");
                    }
                }
            }
        }

        $('#dataTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', !$('#dataTable').bootstrapTable('getSelections').length);
            $('#view,#edit').prop('disabled', $('#dataTable').bootstrapTable('getSelections').length != 1);
        });

        $("#btnImport").click(function () {
            jp.open({
                type: 2,
                area: [500, 200],
                auto: true,
                title: "导入数据",
                content: "${ctx}/tag/importExcel",
                btn: ['确定', '关闭'],
                btn1: function (index, layero) {
                    var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    iframeWin.contentWindow.importExcel('${ctx}/cost/check/import', function (data) {
                        if (data.success) {
                            jp.success(data.msg);
                            refresh();
                        } else {
                            jp.error(data.msg);
                        }
                        jp.close(index);
                    });//调用保存事件
                    return false;
                },

                btn2: function (index) {
                    jp.close(index);
                }
            });
        });


        $("#export").click(function () {//导出Excel文件
            let projectId = $('#projectId').val();
            let projectName = $('#projectName').val();
            jp.downloadFile('${ctx}/cost/check/export?projectId=' + projectId + '&projectName=' + projectName);
        });

        $("#search").click("click", function () {// 绑定查询按扭
            $('#dataTable').bootstrapTable('refresh');
        });

        $("#reset").click("click", function () {// 绑定查询按扭
            $("#searchForm  input").val("");
            $("#searchForm  select").val("");
            $("#searchForm  .select-item").html("");
            $('#dataTable').bootstrapTable('refresh');
        });

    });

function getIdSelections() {
    return $.map($("#dataTable").bootstrapTable('getSelections'), function (row) {
        return row.projectId
    });
}

function deleteAll() {
    jp.confirm('确认要删除该数据记录吗？', function () {
        jp.loading();
        jp.get("${ctx}/sg/settlement/deleteAll?ids=" + getIdSelections(), function (data) {
            if (data.success) {
                $('#dataTable').bootstrapTable('refresh');
                jp.success(data.msg);
            } else {
                jp.error(data.msg);
            }
        })

    })
}

//刷新列表
function refresh() {
    $('#dataTable').bootstrapTable('refresh');
}

function edit(id) {//没有权限时，不显示确定按钮
    if (id == undefined) {
        id = getIdSelections();
    }
    jp.openSaveDialog('编辑', "${ctx}/sg/settlement/form?id=" + id, '800px', '500px');
}

function view(id) {//没有权限时，不显示确定按钮
    if (id == undefined) {
        id = getIdSelections();
    }
    jp.openViewDialog('查看', "${ctx}/sg/settlement/form?id=" + id, '800px', '500px');
}
</script>