
@{
    ViewData["Title"] = "物流供应商往来报表";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
}

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning">
            说明：如报表出现了需合并或者需删除的供应商数据，请点击【往来统计配置】，按需设置合并或者删除的供应商，设置完后，设置好需要调整的记录的日期，然后点击【重跑往来统计数据】
        </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="start" name="start" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"))" />
                        <span>至</span>
                        <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"))" />
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <select id="suppliername" name="suppliername" class="form-control" style="width:220px">
                            <option value="">请选择物流供应商</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <button type="button" class="btn btn-success" onclick="list();">搜索</button>
                        <button type="button" class="btn btn-primary" onclick="showConfig();">往来统计配置</button>
                        <button type="button" class="btn btn-warning" onclick="doTask()">重跑往来统计数据</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="ibox-content m-b-sm border-bottom">
            <div style="margin:10px 0px;">未对账金额：@(ViewBag.amount)</div>
            <table id="roletable" style="table-layout:fixed;"></table>
        </div>
    </div>
</div>
@section css{
    <style>
        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
}

@section scripts{
    <script src="~/css/Layer-2.1/extend/layer.ext.js"></script>
    <script type="text/javascript">
        var tb;
        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });

            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 170));

            list();

            listsupplier();
        })

        function list() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('请选择开始日期');
                return false;
            }

            if (end == '') {
                alert('请选择结束日期');
                return false;
            }

            var columns = [
                { field: 'suppliername', title: '供应商名称', width: '180' },
                { field: 'str_day', title: '日期', width: '110', iscount: true },
                { field: 'amount_start', title: '期初金额', width: '110', iscount: true },
                { field: 'amount_happen', title: '发生额', width: '110', iscount: true },
                { field: 'amount_pay', title: '付款额', width: '110', iscount: true },
                { field: 'amount_receipt', title: '收款额', width: '110', iscount: true },
                {
                    field: 'amount_other', title: '调整金额', width: '110', iscount: true, formatter: function (idx, data) {
                        return '<div>' + data.amount_other + '<a style="margin-left:5px;color:#bbb;" onclick="updateAmount(\'' + data.suppliername + '\',\'' + data.str_day + '\',' + data.amount_other+');">修改</a></div>';
                    }
                },
                { field: 'amount_end', title: '期末金额', width: '120', iscount: true }
            ];

            var url = '@Url.Content("~/Reports/Logistics/SupplierTrancationReportJson")' + '?start=' + start + '&end=' + end + '&suppliername=' + $('#suppliername').val();

            if (tb == undefined) {
                tb = OnlyTable("roletable", columns, url, "", {
                    showfooter: true, loadsuccess: function (d) {
                        //替换汇总行的相关列值
                        var tr = $('.fixed-table-footer').find('tr');
                        for (var c in columns) {
                            var key = columns[c].field;
                            if (columns[c].iscount) {
                                for (var v in d.count_row) {
                                    if (key == v) {
                                        tr.find('td').eq(c).children('div').first().html(d.count_row[v]);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }, undefined, 1000);
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        var suppliername = '';
        var day = '';

        function updateAmount(n, d, v) {
            if (d.indexOf('至') > 0) {
                alert("请选择开始日期和结束日期一致时，再修改调整金额。");
                return false;
            }

            suppliername = n;
            day = d;

            layer.prompt({ title: '编辑' + d + n, value: v }, function (text, index) {
                layer.close(index);
                Save(text);
            });
        }

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

        function listsupplier() {
            $.submit({
                url: '@Url.Content("~/Reports/Logistics/ListLogisticsSupplier")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    $('#suppliername').html('<option value="">请选择物流供应商</option>');
                    if (result) {
                        for (var i in result) {
                            $('#suppliername').append('<option value="' + result[i] + '">' + result[i]+'</option>');
                        }
                    }
                }
            })
        }

        function exportFile() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('请选择开始日期');
                return false;
            }

            if (end == '') {
                alert('请选择结束日期');
                return false;
            }

            window.open('@Url.Content("~/Reports/Finance/ExportFirstSupplierPay?start=")' + start + '&end=' + end, '_blank');
        }

        function showConfig() {
            layer_show('物流供应商往来统计配置', '@Url.Content("~/Supplier/Home/ListLogisticsSupplierConfig")', '95%', '95%');
        }

        function doTask() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('请选择开始日期');
                return false;
            }

            if (end == '') {
                alert('请选择结束日期');
                return false;
            }

            if (confirm('确定要重跑' + start + "至" + end + "的往来数据吗？")) {
                var loading = layer.load(0, {
                shade: false
                });

                $.submit({
                    url: '@Url.Content("~/Reports/Logistics/ReRunLogisticsSupplierTransaction")',
                    paramData: 'start='+start+'&end='+end,
                    type: 'POST',
                    func: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            alert('运行成功');
                        }
                        else {
                            alert(result.msg);
                        }
                    }
                })
            }


        }

    </script>
}


