
@{
    ViewData["Title"] = "财务会计流水";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "销售平台流水", "财务会计流水" };
}

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning">
            提示：1、没有会计科目编码和项目编码的是不参与会计统计的；2、数据中心费用类型缺失的是不参与销售利润报表统计
        </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" type="text" class="form-control" style="width:120px;" value="@(DateTime.Now.AddMonths(-1).ToString("yyyy-MM"))" placeholder="请输入月份" />
                    </div>
                    <div class="form-group">
                        <label>数据来源类型：</label>
                        <select id="datatype" name="datatype" class="form-control" style="width:160px;">
                            <option value="">请选择数据类型</option>
                            <option value="1">销售平台</option>
                            <option value="2">物流账单</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>平台：</label>
                        <select id="platform" name="platform" class="form-control" style="width:150px;">
                            <option value="">请选择平台</option>
                            @if (ViewBag.platform.Count > 0)
                            {
                                foreach (var item in ViewBag.platform)
                                {
                                    <option value="@item">@item</option>
                                }
                            }
                        </select>
                    </div>
                    <div class="form-group">
                        <label>站点：</label>
                        <select id="website" name="website" class="form-control" style="width:120px;">
                            <option value="">请选择站点</option>
                            @if (ViewBag.website.Count > 0)
                            {
                                foreach (var item in ViewBag.website)
                                {
                                    <option value="@item">@item</option>
                                }
                            }
                        </select>
                    </div>
                    <div class="form-group">
                        <label>时间</label>
                        <input id="start" name="start" type="text" class="form-control" style="width:130px;" value="" placeholder="开始时间" />
                        <span>至</span>
                        <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="" placeholder="结束时间" />
                    </div>
                    <div class="form-group">
                        <label>数据中心费用类型</label>
                        <select id="datacentercol" name="datacentercol" class="form-control" style="width:180px;">
                            <option value="">请选择费用类型</option>
                            @if (ViewBag.feeconfigs.Count > 0)
                            {
                                foreach (var item in ViewBag.feeconfigs)
                                {
                                    <option value="@item">@item</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>
                        <button type="button" class="btn btn-danger" onclick="deleteList();">批量删除</button>
                        <button id="btn_uploadFlowing" type="button" class="btn btn-warning">导入流水</button>
                        <a href="@Url.Content("~/templatefile/财务会计流水导入模版.xlsx")" target="_blank">流水模版下载</a>
                    </div>
                </div>
            </form>
        </div>

        <div class="ibox-content m-b-sm border-bottom">
            <button class="btn btn-xs btn-danger" onclick="ShowOrderFeeConfig()" style="margin-bottom:5px;">重要：财务会计流水取数逻辑配置</button>
            <a class="btn btn-xs btn-warning" style="margin-left:5px;margin-bottom:5px;" href="@Url.Content("~/templatefile/财务会计流水解析常见问题.pdf")" target="_blank">流水解析常见问题</a>
            <button class="btn btn-xs btn-success" onclick="ShowExchangeRate()" style="margin-bottom:5px;margin-left:5px;">月份汇率</button>
            <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;

        var websiteCount = '@ViewBag.website.Count';

        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });
            laydate.render({ elem:'#month',type:'month' })

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

            list();

            $('#platform').change(function () {
                //获取平台站点
                getWebsite();
            })

            $('#datatype').change(function () {
                //获取平台
                getPlatform();
                initUpload();
            })

            initUpload();
        })

        function list() {
            if ($('#month').val() == '') {
                alert('请选择月份');
                return false;
            }

            var columns = [
                { field: 'company', title: '主体', width: '160'},
                { field: 'platform', title: '平台', width: '130' },
                { field: 'website', title: '站点', width: '110'},
                {
                    field: 'datatime', title: '账单时间', width: '160'
                },
                {
                    field: 'orderno', title: '单号', width: '180', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.orderno + '">' + data.orderno + '</div>';
                    }
                },
                { field: 'feetype', title: '费用字段说明', width: '160' },
                { field: 'financecategory', title: '财务分类归集说明', width: '200' },
                { field: 'datacenter_col', title: '数据中心费用类型', width: '180'},
                /*{ field: 'financecategory', title: '会计科目分类', width: '130' },*/
                { field: 'subjectcode', title: '会计科目编码', width: '130' },
                { field: 'projectcode', title: '项目编码', width: '120' },
                {
                    field: 'currency', title: '币种', width: '100',iscount:true
                },
                {
                    field: 'amountval', title: '原币金额', width: '120',iscount:true
                },
                {
                    field: 'exchange_rate', title: '汇率', width: '100'
                },
                { field: 'amountval_rmb', title: 'RMB金额', width: '120', iscount: true },
                { field: 'note', title: '备注', width: '160' },
            ];

            var url = '@Url.Content("~/DataWareHouse/PlatformOrder/FinanceAccount_OrderBillingJson")' + '?' + $("#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;
                    }
                });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function exportxls() {

            if ($('#month').val() == '') {
                alert('请选择月份');
                return false;
            }

            if (confirm('确定要导出吗？')) {
                window.open('@Url.Content("~/DataWareHouse/PlatformOrder/ExportFinanceAccount_OrderBilling")' + '?' + $("#toolbar").serialize(), '_blank');
            }
        }

        function getWebsite() {
            var _platform = $('#platform').val();

            $.submit({
                url: '@Url.Content("~/DataWareHouse/PlatformOrder/ListWebsite?platform=")' + _platform,
                paramData: '',
                type:'POST',
                func: function (result) {
                    websiteCount = 0;
                    if (result.success) {
                        websiteCount = result.data.length
                        $('#website').html('<option value="">请选择站点</option>');
                        if (result.data.length > 0) {
                            for (var i in result.data) {
                                $('#website').append('<option value="' + result.data[i] + '">' + result.data[i] + '</option>');
                            }
                        }

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

        function getPlatform() {
            var _datatype = $('#datatype').val();

            $.submit({
                url: '@Url.Content("~/DataWareHouse/PlatformOrder/ListWebsite?datatype=")' + _datatype,
                paramData: '',
                type:'POST',
                func: function (result) {
                    if (result.success) {
                        $('#platform').html('<option value="">请选择平台</option>');

                        if (result.data.length > 0) {
                            for (var i in result.data) {
                                $('#platform').append('<option value="' + result.data[i] + '">' + result.data[i] + '</option>');
                            }
                        }

                        $('#website').html('<option value="">请选择站点</option>');
                    }
                    else {
                        alert(result.msg);
                    }

                }
            })
        }

        function initUpload() {

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

            uploadfile('btn_uploadFlowing',
                '@Url.Content("~/DataWareHouse/PlatformOrder/UploadFinanceAccount")' + '?datatype=' + datatype,
                function(result){
                    if(result.success)
                    {
                        alert('文件上传成功');

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

        function uploadfile(id,url,callback)
        {
            var _GUID = WebUploader.Base.guid();//一个GUID
            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,
                timeout: 0,
                accept: {
                    extensions: "xls,xlsx",
                    mimeTypes: ".xls,.xlsx"
                },
                chunked: false,//开始分片上传
                chunkSize: 2048000,//每一片的大小
                formData: {
                    guid: _GUID //自定义参数，待会儿解释
                }
            });

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

            uploader.on('uploadProgress', function (file, percentage) {
                //$("#uploader .progress-bar").width(percentage * 100 + '%');
                $('#txt_tip').html('正在上传文件，当前进度：' + percentage * 100 + '%');
            });

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

        function deleteList() {
            var datatype = $('#datatype').val();
            var platform = $('#platform').val();
            var website = $('#website').val();
            var month = $('#month').val();

            if (datatype == '') {
                alert('请选择数据类型');
                return false;
            }

            if (platform == '') {
                alert('请选择平台类型');
                return false;
            }

            if (month == '') {
                alert('请选择月份');
                return false;
            }

            if (websiteCount > 0 && website == '') {
                alert('请选择站点');
                return false;
            }


            if (confirm('是否删除当前条件下的所有数据？当前选择的条件是平台【' + platform + '】，站点【' + website + '】，月份【' + month + '】')) {
                if (confirm('再次确认要删除吗？删除后将不可恢复，并且影响月销售利润报表')) {
                    $.submit({
                        url: '',
                        paramData: '',
                        type: 'POST',
                        func: function (result) {

                        }
                    })
                }
            }
        }

        function ShowOrderFeeConfig() {
            layer_show('财务会计流水取数逻辑配置', '@Url.Content("~/DataWareHouse/PlatformOrder/OrderFeeConfig")' + '?platform=' + $('#platform').val() + '&datatype=' + $('#datatype').val(), '90%', '90%');
        }

        function ShowExchangeRate() {
            layer_show('月份汇率', '@Url.Content("~/DataWareHouse/PlatformOrder/ExchangeRate")' + '?month=' + $('#month').val(), '90%', '90%');
        }

    </script>
}
