
@{
    ViewData["Title"] = "数据仓";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "库存", "库存周转报表", "数据仓" };
}

<div class="row">
    <div class="col-sm-12">
        <div id="leftcontain" style="position:relative;float:left;width:215px;margin-right:-220px;background-color:#fff;height:780px;border-top:solid 1px #E7EAEC;display:none;">
            <div id="ztree" class="ztree" style="margin:5px 10px;max-height: 800px;overflow: auto;">

            </div>
        </div>
        <div style="float:right;width:100%;">
            <div id="rightcontain">
                <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-success" onclick="showleft();">分类筛选</button>
                            </div>
                            <div class="form-group">
                                <select id="warehousetype" name="warehousetype" class="form-control">
                                    <option value="">仓库类型不限</option>
                                    <option value="国内仓">国内仓</option>
                                    <option value="海外仓">海外仓</option>
                                    <option value="FBA仓">FBA仓</option>
                                    <option value="第三方仓库">第三方仓库</option>
                                    <option value="耗材仓">耗材仓</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="areaid" name="areaid" class="form-control">
                                    <option value="">仓库国家不限</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="warehousecode" name="warehousecode" class="form-control">
                                    <option value="">仓库不限</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="groupname" name="groupname" class="form-control">
                                    <option value="">分组不限</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control layer-date" id="start" name="start" style="width:120px;" placeholder="开发开始时间" value="" />
                                <input type="text" class="form-control layer-date" id="end" name="end" style="width:120px" placeholder="开发结束时间" value="" />
                            </div>
                            <div class="form-group">
                                <select id="monitorstatus" name="monitorstatus" class="form-control">
                                    <option value="">监控状态不限</option>
                                    <option value="1">正在监控</option>
                                    <option value="2">停止监控</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="cleanstatus" name="cleanstatus" class="form-control">
                                    <option value="">清货状态不限</option>
                                    <option value="1">正在清货</option>
                                    <option value="0">未清货</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="infringestatus" name="infringestatus" class="form-control">
                                    <option value="">侵权状态不限</option>
                                    <option value="1">已侵权</option>
                                    <option value="0">未侵权</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select id="skutip" name="skutip" class="form-control">
                                    <option value="">Sku标签不限</option>
                                    <option value="耗材">耗材</option>
                                    <option value="成品">成品</option>
                                    <option value="半成品">半成品</option>
                                    <option value="闲置中转">闲置中转</option>
                                    <option value="需要清货">需要清货</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <span>库存数量：</span>
                                <input id="storestart" name="storestart" class="form-control" type="number" placeholder="开始区间" style="width:100px" />
                                <span>至</span>
                                <input id="storeend" name="storeend" class="form-control" type="number" placeholder="结束区间" style="width:100px;" />
                            </div>
                            <div class="form-group">
                                @*<select id="skucategory" name="skucategory" class="form-control" style="width:180px;">
                                        <option value="">Sku分类不限</option>
                                    </select>*@
                            </div>
                            <div class="form-group">
                                <input id="searchkey" name="searchkey" class="form-control" type="text" placeholder="多个sku可用英文逗号隔开" />
                            </div>
                            <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-success" onclick="setGroupName();">设置分组</button>
                                <button id="btn_Upload" type="button" class="btn btn-upload">导入分组</button>
                                <a class="btn btn-info" href="~/templatefile/skugroup.xls" target="_blank">下载Sku分组模板</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>
    </div>
</div>

<div id="group-contain" style="display:none;">
    <div style="width:300px;height:300px;text-align:center;margin-bottom:20px;">
        <div style="margin:10px;margin-top:20px;">
            <p>
                <input id="txtgroupname_" data-provide="typeahead" class="form-control" placeholder="请输入分组名称" data-items="8" autocomplete="off" />
            </p>
        </div>
    </div>
</div>


@section css{
    <link href="~/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <link href="~/js/webuploader-0.1.5/webuploader.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/jquery.ztree.core.min.js"></script>
    <script src="~/js/jquery.ztree.excheck.min.js"></script>
    <script src="~/js/typeahead 0.11.1.js" type="text/javascript"></script>
    <script src="~/js/webuploader-0.1.5/webuploader.min.js"></script>
    <script type="text/javascript">
        var tb;
        var zTreeObj;
        var listgroup;
        var BASE_URL = '@(Url.Content("~/js/webuploader-0.1.5/"))';

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

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

            list();

            listwarehouse();

            //listcategory();

            $('#warehousetype').change(function () {
                listwarehouse();
            })

            $('#areaid').change(function () {
                listwarehouse();
            })

            listcategory12();

            listarea();

            groupname();

            
            uploadfile('btn_Upload',
                '@Url.Content("~/Reports/Inventory/UploadSkuGroup")',
                function(result){
                    if(result.success)
                    {
                        alert('上传成功！');
                    }
                    else
                    {
                        alert(result.msg);
                    }
            });

        })


        var columns = [
            {
                field: 'id', title: '<input class="chkall" type="checkbox" onclick="selectall(this)" />', width: '30', formatter: function (idx, data) {
                    return '<input class="chkitem" type="checkbox" val="' + data.sku + "|" + data.warehouse_code + '" />';
                }
            },
            {
                field: 'groupname', title: '分组名称', width: '150', formatter: function (idx, data) {
                    //var str = '<p class="mules" title="' + data.groupname + '">' + data.groupname + '</p>';
                    var str = '<a href="http://pro.bailuntec.com/proMgr/productMgr?groupName=' + data.groupname + '" target="_blank" >' + data.groupname + '</a>';
                    return str;
                }
            },
            { field: 'warehousetype', title: '仓库类型', width: '100' },
            { field: 'warehousename', title: '仓库名称', width: '130' },
            {
                field: 'sku', title: 'sku', width: '100', formatter: function (idx, data) {
                    var str = '<p class="mules" title="' + data.sku + '">' + data.sku + '</p>';
                    return str;
                }
            },
            {
                field: 'category_name', title: '分类', width: '180', formatter: function (idx, data) {
                    var str = '<p class="mules" title="' + data.category_name + '">' + data.category_name + '</p>';
                    return str;
                }
            },
            {
                field: 'skuname', title: '中文名称', width: '180', iscount: true, formatter: function (idx, data) {
                    var str = '<p class="mules" title="' + data.skuname + '">' + data.skuname + '</p>';
                    return str;
                }
            },
            { field: 'status', title: '监控状态', width: '100', sortable: true },
            { field: 'clearancestatus', title: '清货状态', width: '100', sortable: true },
            { field: 'tortstatus', title: '侵权状态', width: '100', sortable: true },
            { field: 'str_skutag', title: 'sku标签', width: '120', sortable: true },
            { field: 'buyprice', title: '采购价', width: '100', sortable: true },
            { field: 'usable_stock', title: '库存数量', width: '110', sortable: true, iscount: true },
            { field: 'amount_stock', title: '库存金额', width: '110', sortable: true, iscount: true },
            { field: 'quantity_purchase', title: '采购在途数量', width: '120', sortable: true, iscount: true },
            { field: 'purchase_amount', title: '采购在途金额', width: '120', sortable: true, iscount: true },
            { field: 'quantity_transfer', title: '调拨在途数量', width: '120', sortable: true, iscount: true },
            { field: 'amount_transit', title: '调拨在途金额', width: '120', sortable: true, iscount: true },
            { field: 'quantity_unshipped', title: '待发货数量', width: '120', sortable: true, iscount: true },
            { field: 'quantity_out_stock', title: '缺货数量', sortable: true, width: '110', iscount: true },
            { field: 'oneday_total_sales', title: '昨日销量', sortable: true, width: '110', iscount: true },
            { field: 'sevenday_average_sales', title: '近七日日均销量', width: '140', sortable: true, iscount: true },
            { field: 'fourteenday_average_sales', title: '近十四日均销量', width: '140', sortable: true, iscount: true },
            { field: 'thirtyday_average_sales', title: '近三十日均销量', width: '140', sortable: true, iscount: true },
            { field: 'amount_sevendaysale', title: '7天总销售额', width: '140', sortable: true, iscount: true }
        ];
        
        function list() {

            var s = '';

            try {
                s = getChkNode();
            } catch (e) {
                s = '';
            }

            var url = '@Url.Content("~/Reports/Inventory/DataWareHouseJson")' + '?' + $("#toolbar").serialize() + '&skucategoryids=' + s;

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

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

        function getCount() {
            var s = '';

            try {
                s = getChkNode();
            } catch (e) {
                s = '';
            }

            $.submit({
                type:'POST',
                url: '@Url.Content("~/Reports/Inventory/DataWareHouseCountJson")',
                paramData: $("#toolbar").serialize() + '&skucategoryids=' + s,
                func: function (result) {
                    if (result) {
                        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 result.count_row) {
                                    if (key == v) {
                                        tr.find('td').eq(c).children('div').first().html(result.count_row[v]);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            })
        }

        function listwarehouse() {
            var wtype = $('#warehousetype').val();
            var area = $('#areaid').val();

            $.submit({
                url: '@Url.Content("~/Home/ListWareHouse")',
                paramData: 'warehousetype=' + wtype + '&areaid=' + area,
                type:'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#warehousecode').html('<option value="">仓库不限</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#warehousecode').append('<option value="' + result[i].warehouse_code + '">' + result[i].warehouse_name + '</option>');
                        }
                    }
                }
            })

        }

        function listarea() {
            $.submit({
                url: '@Url.Content("~/Home/ListWarehouseArea")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#areaid').html('<option value="">仓库国家不限</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#areaid').append('<option value="' + result[i].id + '">' + result[i].name + '</option>');
                        }
                    }
                }
            })
        }

        function groupname() {
            $.submit({
                url: '@Url.Content("~/Home/ListGroupName")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#groupname').html('<option value="">分组不限</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#groupname').append('<option value="' + result[i] + '">' + result[i] + '</option>');
                        }

                        listgroup = result;
                    }
                }
            })
        }

        function listcategory() {
            $.submit({
                url: '@Url.Content("~/Home/ListSkuCategory")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#skucategory').html('<option value="">Sku分类不限</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#skucategory').append('<option value="' + result[i].id + '">' + result[i].name + '</option>');
                        }
                    }
                }
            })
        }


        function listcategory12() {
            $.submit({
                url: '@Url.Content("~/Reports/Orders/ListSkuCategoryOneAndSecondLevel")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    var setting = {
                        check: {
                            chkStyle:'checkbox',
                            enable: true
                        }
                    };
                    zTreeObj = $.fn.zTree.init($("#ztree"), setting, eval('('+result+')'));
                }
            })
        }

        function selectall(data) {
            var ischeck = $(data).is(":checked");
            $('.chkitem').each(function () {
                $(this).prop('checked', (ischeck));
            })
        }

        function setGroupName() {
            var arr = '';
            $('.chkitem').each(function () {
                var title = $(this).attr('val');
                if ($(this).prop('checked')) {
                    arr += (title + ";");
                }
            })

            if (arr.length == 0) {
                layer.alert('请至少选择一个Sku+仓库');
                return false;
            }

            layer.open({
                title:'设置分组',
                area: [300,300],
                type: 1,
                content: $('#group-contain').html().replace('txtgroupname_', 'txtgroupname'),
                btn: ['提交', '取消'],
                btn1: function (index, layero) {
                    var val = $('#txtgroupname').val();
                    if (val == '') {
                        layer.msg('分组信息不能为空！');
                    }
                    else {
                        $.submit({
                            type:'POST',
                            url: '@Url.Content("~/Reports/Inventory/SaveGroupName")',
                            paramData: 's=' + arr +'&groupname='+val,
                            func: function (result) {
                                if (result.success) {
                                    layer.msg('提交成功！');
                                    groupname();
                                    list();
                                }
                                else {
                                    layer.msg(result.msg);
                                }
                            }
                        })
                    }
                },
                success: function (layero, index) {
                    $('#txtgroupname').typeahead({
                        source: function (query, process) {
                            process(listgroup);
                        }
                    });
                }
            })

        }

        function getChkNode() {
            var s = '';
            var o = $.fn.zTree.getZTreeObj("ztree");
            var nodes = o.getCheckedNodes();
            for (var i in nodes) {
                if (nodes[i].children == null || nodes[i].children.length<=0)
                {
                    s += (nodes[i].id) + '|';
                }
            }

            return s;
        }

        function showleft() {
            var c = $('#leftcontain').css('display');
            if (c == 'none') {
                $('#leftcontain').css('display', 'inline');
                $("#rightcontain").animate({ marginLeft: "220px" });
            }
            else {
                $('#leftcontain').css('display', 'none');
                $("#rightcontain").animate({ marginLeft: "0px" });
            }
        }

        var uindex;
        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('startUpload', function () {
                uindex = layer.load();
            })

            uploader.on('uploadSuccess', function (file, response) {
                layer.close(uindex);

                if (callback != undefined)
                {
                    callback(response);
                }

            });

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

    </script>
}

