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

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning">
            说明：数据从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;">月份销售利润表</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/Finance/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);


                            for (var h in result.head) {
                                s = '<tr>' + result.head[h].item2;  //项目名称

                                for (var i in result.data) {

                                    //描述
                                    var objDesc;
                                    for (var d in result.listdesc) {
                                        if (result.listdesc[d]['month'] == result.data[i]["month"]) {
                                            objDesc = result.listdesc[d];
                                        }
                                    }

                                    //值
                                    for (var c in result.data[i]) {
                                        if (c == result.head[h].item1) {
                                            var str_template = result.head[h].item3;
                                            str_template = str_template.replace('{' + result.head[h].item1 + '}', result.data[i][c]);
                                            str_template = str_template.replace('{' + result.head[h].item1 + '}', result.data[i][c]);
                                            str_template = str_template.replace('{month}', result.data[i]['month']);
                                            try {
                                                str_template = str_template.replace('{' + result.head[h].item1 + '_}', result.data[i][c+'_']);
                                            } catch (e) {

                                            }

                                            //基础
                                            var col_1 = result.head[h].item4;
                                            col_1 = col_1.replace('{' + result.head[h].item1 + '}', result.data[i][c]);

                                            if (parseFloat(col_1) >= 0) {
                                                s += '<td>' + col_1 + '</td>';
                                                
                                            }
                                            else {
                                                if (col_1.indexOf('-') > 0) {
                                                    s += '<td>' + ((result.data[i][c]).replace(/,/g, '') - (result.data[i][c + '_'])).toFixed(2) + '</td>';
                                                }
                                                else {
                                                    s += '<td>' + ((result.data[i][c])) + '</td>';
                                                }
                                                
                                            }
                                            
                                            //调整
                                            var col_2 = result.head[h].item5;
                                            col_2 = col_2.replace('{' + result.head[h].item1 + '}', result.data[i][c]);
                                            col_2 = col_2.replace('{' + result.head[h].item1 + '_}', result.data[i][c + '_']);

                                            var temps = '';
                                            if (objDesc != undefined && objDesc != null) {
                                                str_template = str_template.replace('{' + result.head[h].item1 + '_desc}', "说明：" + objDesc[result.head[h].item1]);
                                                temps = objDesc[result.head[h].item1];
                                            }
                                            else {
                                                str_template = str_template.replace('{'+result.head[h].item1 + '_desc}', "说明：无");
                                            }

                                            s += '<td>' + str_template.replace('{' + result.head[h].item1 + '+}', col_2) + '<a style="color:#bbb;" onclick=\"EditDescription(\'' + result.head[h].item1 + '\',\'' + temps + '\',\'' + result.data[i]['month'] + '\',\'' + clearHtml(result.head[h].item2) + '\')\">修改说明</a><a style="color:#bbb;" onclick=\"ShowDetail(\'' + result.head[h].item1 + '\',\'' + result.data[i]['month'] + '\',\'' + clearHtml(result.head[h].item2) + '\')\">明细</a></td>';

                                            //合计
                                            s += ('<td>' + result.data[i][c] + '</td>');
                                            

                                            break;
                                        }
                                    }
                                }
                                s += '</tr>';
                                $('#tb').find('tbody').append(s);
                            }
                            
                        }
                        else {
                            layer.msg('没有' + $('#month').val()+'月份的销售利润数据。');
                        }
                    }
                    else {
                        layer.msg(result.msg);
                    }

                    return false;
                }
            })
        }

        function Edit(col, name,mon,v) {
            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/Finance/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>
}
