
@{
    ViewData["Title"] = "平台应收款";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";

}

<div class="row">
    <div class="col-sm-12">
        <div class="ibox-content m-b-sm border-bottom">
            <div class="alert alert-warning">
                说明：<br />
                预收账款(ebay、shopify、etsy) = 期初预收余额+本期收款（订单付款金额）-发货金额-退款（未发货的）+其他<br />
                应收账款 = 期初应收余额+本期销售收入-平台扣费-放款金额-退款金额+其他
            </div>
        </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>
                        <select id="type" name="type" class="form-control" style="width:160px;">
                            <option value="1">预收</option>
                            <option value="2">应收</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>日期</label>
                        <input id="start" name="start" type="text" class="form-control" style="width:130px;" value="@(ViewBag.start)" />
                        <span>至</span>
                        <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="@(ViewBag.end)" />
                    </div>
                    <div class="form-group">
                        <label>平台类型</label>
                        <select id="platform" name="platform" class="form-control" style="width:130px">
                            <option value="">请选择平台</option>

                        </select>
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <button type="button" class="btn btn-success" onclick="list();">搜索</button>
                    </div>
                </div>
            </form>
        </div>
        <div id="tb_ebay" class="ibox-content m-b-sm border-bottom">
            <table id="roletable" style="table-layout:fixed;"></table>
        </div>
        <div id="tb_noebay" class="ibox-content m-b-sm border-bottom" style="display:none;">
            <table id="roletable_noebay" 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;
        var tb2;
        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });

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

            list();

            listPlatform();
        })

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

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

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

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

            //预收账款
            var columns = [
                { field: 'day', title: '统计日期', width: '110' },
                { field: 'platform', title: '平台类型', width: '110', iscount: true },
                { field: 'amount_start', title: '期初预收余额', width: '130', iscount: true },
                { field: 'amount_sale_pay', title: '本期收款', width: '110', iscount: true },
                { field: 'amount_shipping', title: '发货金额', width: '110',iscount: true },
                { field: 'amount_refund', title: '退款(未发货)', width: '100', iscount: true },
                {
                    field: 'amount_other', title: '其他金额', width: '110', iscount: true, formatter: function (idx, data) {
                        return '<span>' + data.amount_other + '</span><a style="margin-left:3px;" href="javascript:;" onclick="showmodel(' + data.id + ',\'' + data.day + '\',\'' + data.platform + '\',\'' + data.amount_other + '\');">编辑</a>';
                    }
                },
                {
                    field: 'amount_end', title: '备注', width: '130', iscount: false, formatter: function (idx, data) {
                        return '<span>' + data.note + '</span><a style="margin-left:3px;" href="javascript:;" onclick="shownotemodel(' + data.id + ',\'' + data.day + '\',\'' + data.platform + '\',\'' + data.note + '\');">编辑</a>';
                    }
                },
                { field: 'amount_end', title: '预收账款', width: '130', iscount: true }
            ];

            //应收账款
            if (type == 2) {
                columns = [
                    { field: 'day', title: '统计日期', width: '110' },
                    { field: 'platform', title: '平台类型', width: '110', iscount: true },
                    { field: 'amount_start', title: '期初应收余额', width: '130', iscount: true },
                    { field: 'amount_sale_shipping', title: '本期销售收入', width: '130', iscount: true },
                    { field: 'amount_platformfee', title: '平台扣费', width: '110', iscount: true },
                    //{
                    //    field: 'amount_incoming', title: '放款金额', width: '110', iscount: true, formatter: function (idx, data) {
                    //        return '<span>' + data.amount_incoming + '</span><a style="margin-left:3px;" href="javascript:;" onclick="showmodel(' + data.id + ',\'' + data.day + '\',\'' + data.platform + '\',\'' + data.amount_incoming + '\',2);">编辑</a>';
                    //    }

                    //},
                    {
                        field: 'amount_incoming', title: '放款金额', width: '110', iscount: true

                    },
                    { field: 'amount_refund', title: '退款(已发货)', width: '130', iscount: true },
                    {
                        field: 'amount_other', title: '其他金额', width: '110', iscount: true, formatter: function (idx, data) {
                            return '<span>' + data.amount_other + '</span><a style="margin-left:3px;" href="javascript:;" onclick="showmodel(' + data.id + ',\'' + data.day + '\',\'' + data.platform + '\',\'' + data.amount_other + '\');">编辑</a>';
                        }
                    },
                    {
                        field: 'amount_end', title: '备注', width: '130', iscount: false, formatter: function (idx, data) {
                            return '<span>' + data.note + '</span><a style="margin-left:3px;" href="javascript:;" onclick="shownotemodel(' + data.id + ',\'' + data.day + '\',\'' + data.platform + '\',\'' + data.note + '\');">编辑</a>';
                        }
                    },
                    { field: 'amount_end', title: '期末应收账款', width: '130', iscount: true }
                ];
            }


            var url = '@Url.Content("~/Reports/Finance/PlatformReceivableJson")' + '?' + $("#toolbar").serialize();

            var platform = $('#platform').val();

            if (type == 1) {

                $('#tb_noebay').hide();
                $('#tb_ebay').show();

                if (platform != '' && platform != 'Ebay') {
                    layer.msg('在选择了查看Ebay类型的预收款时，只能选择Ebay平台。');

                    return false;
                }


                if (tb == undefined) {
                    tb = OnlyTable("roletable", columns, url, "", {
                        showfooter: true, showCount: true, loadsuccess: function (d) {
                            //替换汇总行的相关列值

                        }
                    });
                }
                else {
                    tb.bootstrapTable('refresh', { url: url });
                }

            }
            else {

                if (platform != '' && platform == 'Ebay') {
                    layer.msg('在选择了查看非Ebay类型的应收款时，不能选择Ebay平台。');

                    return false;
                }

                $('#tb_ebay').hide();
                $('#tb_noebay').show();

                if (tb2 == undefined) {
                    tb2 = OnlyTable("roletable_noebay", columns, url, "", {
                        showfooter: true,loadsuccess: function (d) {
                            //替换汇总行的相关列值
                            var tr = $('#tb_noebay').find('.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;
                                        }
                                    }
                                }
                            }

                        }
                    });
                }
                else {
                    tb2.bootstrapTable('refresh', { url: url });
                }
            }

        }

        function listPlatform() {
            var lindex = layer.load();
            $.submit({
                url: '@Url.Content("~/Reports/Orders/ListPlatform")',
                type:'POST',
                paramData: '',
                func: function (result) {
                    layer.close(lindex);
                    if (result != null && result != undefined) {
                        $('#platform').html('<option value="">选择平台</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#platform').append('<option value="' + result[i] + '">' + result[i]+'</option>');
                        }
                    }
                }
            })
        }

        var _day;
        var _platform;
        var _amount;
        var _t = 1;
        var _note;

        function showmodel(i,d, p, m,t) {
            _day = d;
            _platform = p;
            _amount = m;

            if (t != undefined) {
                _t = t;
            }

            layer.prompt({ title: p+" "+d+" 的其他金额", value: m }, function (text, index) {
                layer.close(index);
                SaveOther(i,text);
            });

        }

        function SaveOther(id,val) {

            $.submit({
                url: '@Url.Content("~/Reports/Finance/UpdatePlatformReceivable")',
                type:'POST',
                paramData: 'id=' + id +'&amount='+val+'&type='+_t,
                func: function (result) {
                    if (result.success) {
                        layer.msg('提交成功！');
                        setTimeout(function () {
                            list();
                        }, 2 * 1000);
                    }
                    else {
                        layer.msg(result.msg);
                    }
                }
            })
        }

        function shownotemodel(i, d, p, n) {
            _day = d;
            _platform = p;
            _note = n;

            layer.prompt({ title: p + " " + d + " 的备注", value: n }, function (text, index) {
                layer.close(index);
                SaveNote(i, text);
            });
        }

        function SaveNote(id,val) {

            $.submit({
                url: '@Url.Content("~/Reports/Finance/UpdatePlatformReceivableNote")',
                type:'POST',
                paramData: 'id=' + id +'&note='+val,
                func: function (result) {
                    if (result.success) {
                        layer.msg('提交成功！');
                        setTimeout(function () {
                            list();
                        }, 2 * 1000);
                    }
                    else {
                        layer.msg(result.msg);
                    }
                }
            })
        }


    </script>
}

