
@{
    ViewData["Title"] = "Paypal收入流水";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "财务报表", "Paypal收入流水" };
}

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning">
            说明：1、只支持csv文件格式导入。2、导入csv文件时，文件名需要增加帐号信息，如[download.csv]，增加帐号后的文件名：[download==dudios.csv]
        </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>Paypal帐号：</label>
                        <select id="account" name="account" class="form-control" style="width:160px;">
                            <option value="">选择Paypal帐号</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>报告时间</label>
                        <input id="start" name="start" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd"))" />
                        <span>至</span>
                        <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.ToString("yyyy-MM-dd"))" />
                    </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 id="btnUpload_chs" type="button" class="btn btn-success">导入paypal中文数据</button>
                        <button id="btnUpload_en_inside" type="button" class="btn btn-success">导入paypal英文【国内】账户</button>
                        <button id="btnUpload_en_outside" type="button" class="btn btn-success">导入paypal英文【国外】帐号</button>
                    </div>
                </div>
            </form>
        </div>

        <div class="ibox-content m-b-sm border-bottom">
            <table id="roletable" style="table-layout:fixed;"></table>
        </div>
    </div>
</div>
@section css{
    <link href="~/js/webuploader-0.1.5/webuploader.css" rel="stylesheet" />
    <link href="~/css/bootstrap-table-fixed-columns.css" rel="stylesheet" />
    <style>
        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .webuploader-pick {
            position: relative;
            display: block;
            cursor: pointer;
            background: none;
            padding: 0px;
            color: #fff;
            text-align: center;
            border-radius: 3px;
            overflow: hidden;
        }

        .webuploader-container {
            background-color: cornflowerblue !important;
        }
    </style>
}
@section scripts{
    <script src="~/js/bootstrap-table-fixed-columns.js" type="text/javascript"></script>
    <script src="~/js/webuploader-0.1.5/webuploader.min.js"></script>

    <script type="text/javascript">
        var BASE_URL = '@(Url.Content("~/js/webuploader-0.1.5/"))';
        var tb;
        var companyid = 0;
        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });

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

            list();

            listAccount();
        })

        function list() {
            var columns = [
                { field: 'record_time', title: '报告日期', width: '160', sortable: true },
                { field: 'account_name', title: '帐号', width: '180' },
                { field: 'time_zone', title: '时区', width: '70'},
                {
                    field: 'type', title: '类型', width: '190', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.type + '">' + data.type + '</div>';
                    }},
                { field: 'status', title: '状态', width: '120', iscount: true},
                { field: 'currency', title: '币种', width: '70'},
                { field: 'gross', title: '总额', width: '100', sortable: true, iscount: true },
                { field: 'fee', title: '费用', width: '90', sortable: true, iscount: true },
                { field: 'net', title: '净额', width: '90', sortable: true, iscount: true },
                {
                    field: 'from_email', title: '发件人邮箱地址', width: '190', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.from_email + '">' + data.from_email + '</div>';
                    }
                },
                {
                    field: 'to_email', title: '收件人邮箱地址', width: '190', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.to_email + '">' + data.to_email + '</div>';
                    }
                },
                {
                    field: 'transaction_id', title: '交易号', width: '160', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.transaction_id + '">' + data.transaction_id + '</div>';
                    }
                },
                { field: 'shipping_handling_amount', title: '运费/手续费', width: '120', sortable: true, iscount: true },
                { field: 'insurance_amount', title: '保险金额', width: '90', sortable: true, iscount: true },
                { field: 'sales_tax', title: '营业税', width: '90', sortable: true, iscount: true },
                {
                    field: 'custom_number', title: '自定义号码', width: '220', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.custom_number + '">' + data.custom_number + '</div>';
                    } },
                { field: 'quantity', title: '数量', width: '90', sortable: true, iscount: true },
                { field: 'balance', title: '余额', width: '90', sortable: true, iscount: true },
                { field: 'country_code', title: '国家', width: '130' },
                { field: 'balance_impact', title: '余额变动', width: '90' },
                { field: 'createtime', title: '导入时间', width: '150' }
            ];

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

            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;
                                    }
                                }
                            }
                        }
                        companyid = d.companyid;

                        uploadfile('btnUpload_chs',
                            '@Url.Content("~/Reports/Finance/UploadEbayIncome")' + '?companyid=' + companyid +'&formattype=1',
                            function(result){
                                if(result.success)
                                {
                                    alert('上传成功！');
                                }
                                else
                                {
                                    alert(result.msg);
                                }

                                window.location = window.location;
                            });

                        uploadfile('btnUpload_en_inside',
                            '@Url.Content("~/Reports/Finance/UploadEbayIncome")' + '?companyid=' + companyid +'&formattype=3',
                            function(result){
                                if(result.success)
                                {
                                    alert('上传成功！');
                                }
                                else
                                {
                                    alert(result.msg);
                                }
                                window.location = window.location;
                            });
                        uploadfile('btnUpload_en_outside',
                            '@Url.Content("~/Reports/Finance/UploadEbayIncome")' + '?companyid=' + companyid +'&formattype=2',
                            function(result){
                                if(result.success)
                                {
                                    alert('上传成功！');
                                }
                                else
                                {
                                    alert(result.msg);
                                }
                                window.location = window.location;
                            });

                    }
                }, {
                        fixedcol: true,
                        fixedcolnum:2
                    });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function listAccount() {
            $.submit({
                url: '@Url.Content("~/Reports/Orders/ListPaypalAccount")',
                type:'POST',
                paramData: '',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#account').html('<option value="">选择Paypal帐号</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#account').append('<option value="' + result[i] + '">' + result[i]+'</option>');
                        }
                    }
                }
            })
        }

        function exportlist() {
            if (confirm("确定要导出吗?")) {
                window.open('@Url.Content("~/Reports/Orders/ExportEbayOrders")' + '?' + $("#toolbar").serialize(), '_blank');
            }

        }

        function uploadfile(id,url,callback)
        {
            var uploader = new WebUploader.Uploader({
                // swf文件路径
                swf: BASE_URL + 'Uploader.swf',
                // 文件接收服务端。
                server: url!=undefined?url:'@Url.Content("~/File/UploadFile")',
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#'+id,
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,
                auto: true,
                multiple: true,
            });

            uploader.on('uploadSuccess', function (file, response) {
                if(callback!=undefined)
                {
                    callback(response);
                }
            });

            uploader.on('uploadError', function (file) {
                layer.msg("上传出错");
            });
        }

    </script>
}

