
@{
    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>
                        <input id="day" name="day" class="form-control" style="width:120px;" value="@(ViewBag.day)" />
                    </div>

                    <div class="form-group">
                        <button type="button" class="btn btn-success" onclick="list();">查询</button>
                        <button id="btn_Upload" class="btn btn-success">导入</button>
                        <a id="btn_UpdateTemplate" class="btn btn-default" href="@Url.Content("~/templatefile/balance_template.xlsx")" target="_blank">下载导入模版</a>
                    </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" />
    <style>
        .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/webuploader-0.1.5/webuploader.min.js"></script>
    <script>
        var BASE_URL = '@(Url.Content("~/js/webuploader-0.1.5/"))';
        var tb;
        $(document).ready(function () {
            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 100));

            laydate.render({ elem: '#day' });

            list();
        })

        function list() {
            var day = $('#day').val();
            if (day == '') {
                alert('请选择日期');
                return false;
            }

            var columns = [
                { field: 'fromsource', title: '来源', width: '140', sortable: false, iscount: true },
                { field: 'detailname', title: '类型/名称', width: '110', sortable: false },
                { field: 'amount', title: '金额', width: '110', sortable: true, iscount: true },
                { field: 'note1', title: '备注1', width: '150', sortable: false },
                { field: 'note2', title: '备注2', width: '150', sortable: false }
            ];

            var url = '@Url.Content("~/Reports/Finance/ListBalanceSheetSubDetailJson")' + '?type=@(ViewBag.type)&day='+day;

            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;
                                    }
                                }
                            }
                        }

                        uploadfile('btn_Upload',
                            '@Url.Content("~/Reports/Finance/UploadBalanceSheetSubDetail?t="+ ViewBag.type)' + '&uid=' + d.uid +'&username='+d.username,
                            function(result){
                                if(result.success)
                                {
                                    alert('上传成功！');
                                    list();
                                }
                                else
                                {
                                    alert(result.msg);
                                }
                        });
                    }
                });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        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
            });

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

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

    </script>

}

