
@{
    ViewData["Title"] = "管理成本报表";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
}

<div class="row">
    <div class="col-sm-12">
        <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>
                        <select id="sel_type" class="form-control">
                            <option value="1">按部门</option>
                            <option value="2">按付款主体</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>付款时间</label>
                        <input class="form-control layer-date" id="start" style="width:120px;" placeholder="付款开始时间" value="@ViewBag.start.ToString("yyyy-MM-dd")" />
                        <input class="form-control layer-date" id="end" style="width:120px" placeholder="付款结束时间" value="@ViewBag.end.ToString("yyyy-MM-dd")" />
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <select id="sel_paycompanyid" class="form-control" style="display:none;">
                            <option value="">请选择付款主体</option> 
                            <option value="1">广州百伦供应链科技有限公司</option>
                            <option value="2">香港百伦科技有限公司</option>
                            <option value="5">广州电子服装仓</option>
                            <option value="7">深圳仓</option>
                            <option value="8">阳山仓</option>
                            <option value="13">广州电子服装仓—沙河仓</option>
                        </select>
                    </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-success" onclick="exportxls();">导出</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="ibox-content m-b-sm border-bottom">
            <div class="bootstrap-table">
                <div class="fixed-table-container">
                    <div class="fixed-table-body">
                        <table style="table-layout: fixed;" class="table table-border table-bordered table-hover table-bg table-sort">
                            <thead>
                                <tr id="tb_head" class="text-c"></tr>
                            </thead>
                            <tbody id="tb_body"></tbody>
                        </table>
                    </div>
                </div>
            </div>
         </div>
    </div>
</div>

@section css{ 
    <style>
        .text-c {background-color:#f5f5f5 !important;text-align:center !important;}
        .text-c th{height:30px !important;text-align:center;}
    </style>

}

@section scripts{ 
    <script>
        var tbhead_;
        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });

            list();
        })

        function list() {
            var type = $('#sel_type').val();

            if (type == '') {
                alert('请选择统计类型');
                return false;
            }

            var start = $('#start').val();
            var end = $('#end').val();
            var paycompanyid = $('#sel_paycompanyid').val();

            if (start == '') {
                alert('请选择付款开始时间');
                return false;
            }

            if (end == '') {
                alert('请选择付款结束时间');
                return false;
            }

            layer.load(2, { shade: [0.1, '#fff'], time: 30000 });
            $.submit({
                url: '@Url.Content("~/Reports/Finance/AdministrativeCostJson")',
                type: 'POST',
                paramData: 't=' + type + '&start=' + start+'&end='+end,
                func: function (result) {
                    layer.closeAll('loading');
                    if (result.success) {
                        var dt = result.list;
                        tbhead_ = result.col;
                        $('#tb_head').html('');
                        $('#tb_body').html('');

                        var str = '<th style="width:100px;">费用类型</th>';
                        for (var h = 0; h < result.col.length; h++) {
                            str += '<th style="width:100px;">' + (result.col[h] == null ? "无部门" : result.col[h]) + '</th>';
                        }

                        str += '<th style="width:100px;">总计</th>';
                        $('#tb_head').html(str);

                        //构造表格内容
                        var arrTotal = new Array();
                        for (var i = 0; i < dt.length - 1; i++) {
                            var rowamount = 0;
                            str = '<tr class="tr_' + i + '">';
                            str += '<td style="width:100px"><span style="cursor:pointer;" onclick="getChildren(' + i + ',\'' + dt[i].item1 + '\')">' + (dt[i].item1) + '</span></td>';

                            var item2 = dt[i].item2;
                            for (var j = 0; j < item2.length - 1; j++) {
                                var tempFloat = parseFloat(item2[j]);

                                str += '<td style="width:100px">' + (tempFloat > 0 ? '<span style="cursor:pointer" onclick="OpenDetail(\'' + dt[i].item1 + '\',\'' + (result.col[j] == null ? "" : result.col[j]) + '\',0)">' + numFormat(tempFloat) + '</span>' : tempFloat) + '</td>';
                                rowamount = add(rowamount, tempFloat);

                                if (arrTotal.length > j) {
                                    arrTotal[j] = add(arrTotal[j], parseFloat(item2[j]));  //(arrTotal[j] * 100 + parseFloat(item2[j]) * 100) / 100;
                                }
                                else {
                                    arrTotal.push(parseFloat(item2[j]));
                                }
                            }
                            str += '<td>' + item2[item2.length - 1] + '</td>';
                            str += '</tr>';
                            $('#tb_body').append(str);
                        }

                        str = '<tr><td>总计：</td>';
                        var lastItem = dt[dt.length - 1].item2;
                        for (var j = 0; j < lastItem.length; j++) {
                            str += '<td>' + numFormat(lastItem[j]) + '</td>';
                        }
                        str += '<td></td></tr>';

                        str += '<tr><td>合计：</td><td style="text-align:right;" colspan="' + (arrTotal.length + 1) + '">' + numFormat(result.amount) + '</td>'
                        $('#tb_body').append(str);

                    }
                    else {
                        alert(result.msg);
                    }
                }
            })
        }

        function add(arg1, arg2) {
            var r1, r2, m, c;
            try {
                r1 = arg1.toString().split(".")[1].length;
            }
            catch (e) {
                r1 = 0;
            }
            try {
                r2 = arg2.toString().split(".")[1].length;
            }
            catch (e) {
                r2 = 0;
            }

            m = Math.pow(10, Math.max(r1, r2));

            return parseFloat(parseInt(arg1 * m) + parseInt(arg2 * m)) / m;
        }

        function getChildren(trindex, typename) {
            if (typename == '利息支出') {
                layer.msg('利息支出没有子分类哦');
                return false;
            }
            
            var objTr = $('.tr_' + trindex + '_');
            if (objTr.length > 0) {
                if (objTr.attr('ishide') == 1) {
                    objTr.attr('ishide', 0);
                    objTr.css('display', '');
                }
                else {
                    objTr.attr('ishide', 1);
                    objTr.css('display', 'none');
                }
                return;
            }

            var type = $('#sel_type').val();

            if (type == '') {
                alert('请选择统计类型');
                return false;
            }

            var start = $('#start').val();
            var end = $('#end').val();
            var paycompanyid = $('#sel_paycompanyid').val();

            if (start == '') {
                alert('请选择付款开始时间');
                return false;
            }

            if (end == '') {
                alert('请选择付款结束时间');
                return false;
            }

            layer.load(2, { shade: [0.1, '#fff'], time: 30000 });
            $.ajax({
                url: "@Url.Content("~/Reports/Finance/AdministrativeCostChildrenJson")",
                type: 'post',
                dataType: 'json',
                data: {
                    t: type,
                    start: start,
                    end: end,
                    paycompanyid: paycompanyid,
                    categoryname: typename,
                    heads: tbhead_.join('|')
                },
                success: function (result) {
                    layer.closeAll('loading');
                    if (result.success) {
                        var dt = result.list;
                        var str = '<tr class="tr_' + trindex + '_"><td colspan="' + (tbhead_.length + 2) + '"><table class="table table-border table-bordered table-hover table-bg table-sort">';

                        //添加表头
                        str += '<thead><tr class="text-c"><th>' + typename + '子类</th>';
                        for (var h = 0; h < result.col.length; h++) {
                            str += '<th>' + (result.col[h] == "" ? "无部门" : result.col[h]) + '</th>';
                        }
                        str += '<th>总计</th></tr></thead><tbody>';

                        for (var i = 0; i < dt.length; i++) {
                            var rowamount = 0;
                            str += '<tr class="subtr_' + i + '">';
                            //str += '<td><span style="cursor:point;"' + (dt[i].item1 == '工资' ? 'onclick=getLandTax(' + i + ',"' + typename + '","' + dt[i].item1 + '")' : '') + '>' + (dt[i].item1) + '</span></td>';
                            str += '<td><span ' + '>' + (dt[i].item1) + '</span></td>';

                            var item2 = dt[i].item2;
                            for (var j = 0; j < item2.length; j++) {
                                var tempFloat = parseFloat(item2[j]);

                                str += '<td>' + (tempFloat > 0 ? '<span style="cursor:pointer;" onclick="OpenDetail(\'' + dt[i].item1 + '\',\'' + (result.col[j] == null ? "" : result.col[j]) + '\',1)">' + numFormat(tempFloat) + '</span>' : tempFloat) + '</td>';
                                rowamount = add(rowamount, tempFloat);
                            }
                            str += '<td>' + rowamount + '</td>';
                            str += '</tr>';
                        }
                        str += '</tbody></table></td><tr>';
                        $('.tr_' + trindex).after(str);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        }

        function OpenDetail(title, colname, issub) {
            var url = '@Url.Content("~/Reports/Finance/ListAdministrativeCost?")';

            var type = $('#sel_type').val();
            var start = $('#start').val();
            var end = $('#end').val();
            var paycompanyid = $('#sel_paycompanyid').val();
            var param = '';

            if (start == '') {
                alert('请选择付款开始时间');
                return false;
            }

            if (end == '') {
                alert('请选择付款结束时间');
                return false;
            }

            param += 'type=' + type;
            param += '&start=' + start;
            param += '&end=' + end;
            param += '&paycompanyid=' + paycompanyid;
            param += '&headname=' + colname;
            param += '&categoryname=' + title;
            param += '&issub=' + issub||0;

            if (title == '利息支出') {
                url = '@Url.Content("~/Reports/Finance/ListInterestExpense?")';
            }

            layer_show(title + '+' + colname + '的明细', url + param, '90%', '90%');
        }

        function numFormat(num) {
            var res = num.toString().replace(/\d+/, function (n) { // 先提取整数部分
                return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
                    return $1 + ",";
                });
            })
            return res;
        }

        function exportxls() {
            var type = $('#sel_type').val();

            if (type == '') {
                alert('请选择统计类型');
                return false;
            }

            var start = $('#start').val();
            var end = $('#end').val();
            var paycompanyid = $('#sel_paycompanyid').val();

            if (start == '') {
                alert('请选择付款开始时间');
                return false;
            }

            if (end == '') {
                alert('请选择付款结束时间');
                return false;
            }

            var url = '@Url.Content("~/Reports/Finance/ExportAdministrativeCost?")' + 'start=' + start + '&end=' + end + '&paycompanyid=' + paycompanyid;

            window.open(url, '_blank');
        }

    </script>
}
