
@{
    ViewData["Title"] = ViewBag.month + "月销售利润明细";
    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">
                        <button type="button" class="btn btn-primary" onclick="list();"><i class="fa fa-search"></i>&nbsp;查询</button>
                        <button type="button" class="btn btn-danger" onclick="DelBatch();">批量删除</button>
                        <button id="btn_Upload" type="button" class="btn btn-success">导入</button>
                        <a href="~/templatefile/monthsaleprofit_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;
        }

        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
}
@section scripts{
    <script src="~/js/webuploader-0.1.5/webuploader.min.js"></script>
    <script>

        var tb;
        var BASE_URL = '@(Url.Content("~/js/webuploader-0.1.5/"))';
        var uid = 0;
        var username = '';
        var isInit = 1;

        $(document).ready(function (){
            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 150));

            list();


        })

        function list() {
            var columns = [
                {
                    field: 'id', title: '<input type="checkbox" class="chkall" />', width: '90', formatter: function (idx, data) {
                        return '<input type="checkbox" class="chk" val="'+data.id+'" />';
                    }
                },
                { field: 'month', title: '月份', width: '90' },
                { field: 'type', title: '类型', width: '130', iscount: true },
                { field: 'amount', title: '金额', width: '100', sortable: true,iscount:true},
                { field: 'note_one', title: '备注1', width: '260'},
                { field: 'note_two', title: '备注2', width: '260'},
                { field: 'createtime', title: '创建时间', width: '130'},
                {
                    field: 'option', title: '操作', width: '100px', formatter: function (idx, data) {
                        var s = '<button class="btn btn-danger btn-xs" onclick="DelDetail(\'' + data.id + '\');">删除</button>';
                        return s;
                    }
                }
            ];

            var url = '@Url.Content("~/Reports/Finance/ListMonthSaleProfitDetailJson")' + '?month=@(ViewBag.month)'+ '&col=@(ViewBag.col)'+'&isnew=@(ViewBag.isnew)';

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

                        if (isInit == 1) {

                            $('.chkall').click(function () {
                                var ischeck = $(this).is(':checked');
                                if (ischeck) {
                                    $('.chk').each(function () {
                                        $(this).prop('checked', true);
                                    })
                                }
                                else {
                                    $('.chk').each(function () {
                                        $(this).prop('checked', false);
                                    })
                                }
                            })

                            uploadfile('btn_Upload',
                                '@Url.Content("~/Reports/Finance/UploadMonthSaleProfitDetail")' + '?month=@(ViewBag.month)'+ '&col=@(ViewBag.col)' +  '&colname=@(ViewBag.colname)' + '&uid=' + uid + '&username=' + username+'&isnew=@(ViewBag.isnew)',
                                function(result){
                                    if(result.success)
                                    {
                                        alert('上传成功！');
                                        setTimeout(function () { list();}, 1.5 * 1000);
                                    }
                                    else
                                    {
                                        alert(result.msg);
                                    }
                                });
                            isInit = 0;
                        }
                    }
                });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function DelDetail(id) {
            if (confirm('确定要删除该记录吗？')) {
                $.submit({
                url: '@Url.Content("~/Reports/Finance/DelMonthSaleProfitDetail")',
                type: "POST",
                paramData: 'id='+id,
                func: function (result) {
                    if (result.success) {
                        alert("删除成功！");
                        list();
                    }
                    else {
                        alert(result.msg);
                    }
                }
            })
            }
        }

        function DelBatch() {
            if (confirm('确定要批量删除选中的记录吗？')) {
                var _s = '';
                $('.chk').each(function () {
                    if ($(this).is(':checked')) {
                        _s += $(this).attr('val') + ',';
                    }
                })

                if (_s.length == 0) {
                    alert('请选择需要删除的项');
                    return;
                }

                $.submit({
                    url: '@Url.Content("~/Reports/Finance/DelMonthSaleProfitDetailBatch")',
                    type: "POST",
                    paramData: 'ids=' + _s,
                    func: function (result) {
                        if (result.success) {
                            alert("删除成功！");
                            list();
                        }
                        else {
                            alert(result.msg);
                        }
                    }
                })
            }

        }

        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>

}

