
@{
    ViewData["Title"] = "4K月度销售利润报表";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "财务报表", "4K月度销售利润报表" };
}

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning" style="display:none;">
            @*说明：数据从2019年9月份开始*@
        </div>
        <div class="ibox-content m-b-sm border-bottom">
            <form id="toolbar">
                <div class="form-inline" style="line-height:40px;">
                    <div class="form-group">
                        <label>请选择开始月份</label>
                        <input id="month" name="month" class="form-control" style="width:130px;" placeholder="年.月" value="@(DateTime.Now.AddMonths(-1).ToString("yyyy-MM"))" />
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <button type="button" class="btn btn-primary" onclick="list();"><i class="fa fa-search"></i>&nbsp;查询</button>
                        @*<button type="button" class="btn btn-danger" onclick="exportxls();">导出</button>*@
                    </div>
                </div>
            </form>
        </div>
        <div class="ibox-content m-b-sm border-bottom">
            <h2 style="text-align:center;">4K月份销售利润表</h2>
            <div class="bootstrap-table">
                <div class="fixed-table-container">
                    <div class="fixed-table-body">
                        <table id="tb" style="table-layout: fixed;" class="table table-border table-bordered table-hover table-bg table-sort form-inline">
                            <thead>

                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section css{
    <style>
        a {
            margin-left: 10px;
        }
    </style>
}

@section scripts{
    <script src="~/css/Layer-2.1/extend/layer.ext.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            laydate.render({ elem: '#month', type: 'month' });

            list();
        })

        function list() {
            $.submit({
                url: '@Url.Content("~/Reports/Finance4K/ListMonthSaleProfit")',
                type: 'POST',
                paramData: $("#toolbar").serialize(),
                func: function (result) {
                    if (result.success) {
                        $('#tb').find('thead').html('');
                        $('#tb').find('tbody').html('');
                        if (result.data.length > 0) {
                            var s = '';

                            var m = '<tr><th style="height:40px;text-align: center;line-height: 40px;" rowspan="2">项目</th>';
                            var m2 = '<tr>';
                            for (var i in result.data) {
                                m += ('<th style="height:40px;text-align: center;line-height: 40px;" colspan="3">' + result.data[i]['month'] + '月份</th>');

                                m2 += '<td>基础数据</td><td>调整数据</td><td>合计数据</td>';

                            }
                            m2 += '</tr>';
                            m += '</tr>';



                            $('#tb').find('thead').html(m + m2);

                            //组装行
                            var rows = '';

                            for (var i in result.head) {
                                var col = result.head[i];
                                var row = col.item3 == 0 ? '<tr><td>' + col.item1 + '</td>':'<tr><td style="font-weight:bold;">' + col.item1 + '</td>';

                                for (var d in result.data) {
                                    var objData = result.data[d];
                                    

                                    for (var a in objData) {
                                        if (a == col.item2) {
                                            row += '<td>' + objData[a] + '</td>';
                                            
                                            continue;
                                        }
                                    }

                                    //查看是否有编辑对象
                                    var objEdit = undefined;
                                    for (var e in result.listEdit) {
                                        if (objData.month == result.listEdit[e].month) {
                                            objEdit = result.listEdit[e];
                                            continue;
                                        }
                                    }

                                    if (objEdit != undefined) {
                                        for (var a in objEdit) {
                                            if (a == col.item2) {
                                                row += '<td>' + objEdit[a] + (col.item3==1?"":('<a href="javascript:;" onclick="EditFee(\''+col.item1+'\',\'' + col.item2 + '\',' + objEdit[a] + ',\'' + objData.month + '\')">编辑</a>'))+'</td>';
                                                
                                                continue;
                                            }
                                        }
                                    }
                                    else {
                                        row += '<td>0' + (col.item3 == 1 ? "" :'<a href="javascript:;" onclick="EditFee(\'' + col.item1 +'\',\'' + col.item2 + '\',0,\'' + objData.month + '\')">编辑</a>')+'</td>';
                                    }


                                    //合计
                                    var objCount = undefined;
                                    for (var e in result.listCount) {
                                        if (objData.month == result.listCount[e].month) {
                                            objCount = result.listCount[e];
                                            continue;
                                        }
                                    }
                                    if (objCount != undefined) {
                                        for (var a in objCount) {
                                            if (a == col.item2) {
                                                //合计
                                                row += '<td>' + objCount[a] + '</td>';
                                            }
                                        }
                                    }
                                    else {
                                        //合计
                                        row += '<td>0</td>';
                                    }


                                }

                                row += '</tr>';
                                rows += row;
                            }

                            $('#tb').find('tbody').append(rows);


                        }
                        else {
                            layer.msg('没有' + $('#month').val()+'月份的销售利润数据。');
                        }
                    }
                    else {
                        layer.msg(result.msg);
                    }

                    return false;
                }
            })
        }

        function EditFee(name, col,v,mon) {
            layer.prompt({ title: '编辑' + name, value:v}, function (text, index) {
                layer.close(index);
                Save(col, text, mon);
            });
        }

        function Save(col, val,mon) {
            $.submit({
                url: '@Url.Content("~/Reports/Finance4K/SaveMonthSaleProfit")',
                paramData: 'month=' + mon+'&'+col+'='+val,
                type:'POST',
                func: function (result) {
                    if (result.success) {
                        layer.msg('提交成功');
                        setTimeout(function () {
                            list();
                        }, 1.5 * 1000);
                    }
                    else {
                        layer.msg(result.msg);
                    }
                }
            })
        }

        function EditDescription(col, val, mon, name) {
            layer.prompt({ title: '编辑 ' + name, value: val }, function (text, index) {
                layer.close(index);
                SaveDescription(col, text, mon);
            });
        }

        function SaveDescription(col, val, mon) {
            $.submit({
                url: '@Url.Content("~/Reports/Finance/UpdateMonthSaleProfitDescription")',
                paramData: 'month=' + mon+'&'+col+'='+val,
                type:'POST',
                func: function (result) {
                    if (result.success) {
                        layer.msg('提交成功');
                        setTimeout(function () {
                            list();
                        }, 1.5 * 1000);
                    }
                    else {
                        layer.msg(result.msg);
                    }
                }
            })
        }

        function ShowDetail(col, mon, name) {
            layer_show(mon + ' ' + name + "的明细", '@Url.Content("~/Reports/Finance/MonthSaleProfitDetails")' + '?month=' + mon + '&col=' + col + '&colname=' + name, '95%', '95%');
        }

        function clearHtml(s) {
            // 去除 富文本格式
            s = s.replace(/(\n)/g, "");
            s = s.replace(/(\t)/g, "");
            s = s.replace(/(\r)/g, "");
            s = s.replace(/<\/?[^>]*>/g, "");
            s = s.replace(/\s*/g, "");

            return s;
        }
    </script>
}
