
@{
    ViewData["Title"] = "Sku仓库";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
}

<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:600px;border-top:solid 1px #E7EAEC;display:none;">
            <div id="ztree" class="ztree" style="margin:5px 10px;max-height: 600px;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">
                        @*<input id="code" name="code" type="hidden" value="@ViewBag.warehousecode" />*@
                        <div class="form-inline" style="line-height:40px;">
                            <div class="form-group">
                                <label>&nbsp;</label>
                                <button type="button" class="btn btn-success" onclick="showleft();">分类筛选</button>
                            </div>
                            <div class="form-group">
                                <label>产品库</label>
                                <select id="push_status" name="push_status" class="form-control">
                                    <option value="">是否已推送产品库</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>仓库</label>
                                <select id="warehousecode" name="warehousecode" class="form-control" style="width:160px;">
                                    <option value="">请选择仓库</option>
                                    @if (ViewBag.listWarehouse.Count > 0)
                                    {
                                        foreach (var item in ViewBag.listWarehouse)
                                        {
                                            <option value="@item.warehouse_code" @(ViewBag.warehousecode == item.warehouse_code ? "selected" : "")>@item.warehouse_name</option>
                                        }
                                    }
                                </select>
                            </div>
                            <div class="form-group">
                                <label>14日日均销量</label>
                                <input id="minordercount" name="minordercount" class="form-control" type="number" style="width:135px;" placeholder="14日均销量开始" />
                                <span>至</span>
                                <input id="maxordercount" name="maxordercount" class="form-control" type="number" style="width:135px;" placeholder="14日均销量结束" />
                            </div>
                            <div class="form-group">
                                <label>库存数量</label>
                                <input id="stockmin" name="stockmin" class="form-control" type="number" style="width:135px;" placeholder="库存数量开始" />
                                <span>至</span>
                                <input id="stockmax" name="stockmax" class="form-control" type="number" style="width:135px;" placeholder="库存数量结束" />
                            </div>
                            <div class="form-group">
                                <label>库存金额</label>
                                <input id="stockamountmin" name="stockamountmin" class="form-control" type="number" style="width:125px;" placeholder="库存金额开始" />
                                <span>至</span>
                                <input id="stockamountmax" name="stockamountmax" class="form-control" type="number" style="width:125px;" placeholder="库存金额结束" />
                            </div>
                            <div class="form-group">
                                <label>可销售天数</label>
                                <select id="available_days" name="available_days" class="form-control" style="width:160px;">
                                    <option value="">请选择可销售天数</option>
                                    <option value="-15">少于15天</option>
                                    <option value="15-30">15-30天</option>
                                    <option value="30-45">30-45天</option>
                                    <option value="45-60">45-60天</option>
                                    <option value="60-90">60-90天</option>
                                    <option value="90-120">90-120天</option>
                                    <option value="120-180">120-180天</option>
                                    <option value="180-">180天以上</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>日期</label>
                                <input id="day" name="day" type="text" class="form-control" style="width:130px;" value="@(ViewBag.day)" placeholder="统计日期" />
                            </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-warning" onclick="exportlist()">导出</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>
    </div>
</div>
@section css{
    <link href="~/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <link href="~/css/bootstrap-table-fixed-columns.css" rel="stylesheet" />
    <style>
        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
}
@section scripts{
    <script src="~/js/jquery.ztree.core.min.js"></script>
    <script src="~/js/jquery.ztree.excheck.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
    <script src="~/js/bootstrap-table-fixed-columns.js" type="text/javascript"></script>
    <script type="text/javascript">
        var tb;
        var zTreeObj;
        $(document).ready(function () {
            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 160));
            laydate.render({ elem: '#day' });
            if ('@(ViewBag.available_days)' != '') {
                $('#available_days').val('@(ViewBag.available_days)');
            }

            list();

            //listwarehouse();

            //listcategory();

            listcategory12();

        })

        function list() {
            var columns = [
                {
                    field: 'bailun_sku', title: 'sku编码', width: '160', sortable: true, formatter: function (idx, data) {
                        if (data.push_status == '是') {
                            return '<a href="http://pro.bailuntec.com/proMgr/productMgr?skuCode=' + data.bailun_sku + '" target="_blank" >' + data.bailun_sku + '</a>';
                        }
                        return data.bailun_sku;

                    }
                },
                { field: 'warehouse_name', title: '所在仓库', width: '180', sortable: true },
                {
                    field: 'push_status', title: '推送产品库', width: '120', sortable: true, formatter: function (idx, data) {
                        var s = '<span style="color:' + (data.push_status=='是'?"#01B468":"red") + ';">' + data.push_status + '</span>';
                        return s;
                    }
                },
                {
                    field: 'sku_image', title: '主图', width: '80', sortable: false, formatter: function (idx, data) {
                        var s = '<a href="' + data.sku_image + '" target="_blank"><img src="' + data.sku_image + '" style="height:20px" /></a>';
                        return s;
                    }
                },
                {
                    field: 'category_name', title: '分类名称', width: '180', sortable: false, formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.category_name + '">' + data.category_name + '</div>';
                    }
                },
                {
                    field: 'sku_title_en', title: '英文标题', width: '160', sortable: false, formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.sku_title_en + '">' + data.sku_title_en + '</div>';
                    }
                },
                {
                    field: 'sku_title_cn', title: '中文标题', width: '160', sortable: false, iscount: true, formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.sku_title_cn + '">' + data.sku_title_cn + '</div>';
                    }
                },
                {
                    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: '130', sortable: true, iscount: true
                },
                { field: 'purchase_amount', title: '采购在途金额', width: '130', sortable: true, iscount: true },
                {
                    field: 'quantity_transfer', title: '调拨在途数量', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'amount_transit', title: '调拨在途金额', width: '150', sortable: true, iscount: true
                },
                {
                    field: 'noshippingcount', title: '待发货数量', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'nostockcount', title: '缺货数量', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'quantity_tuneout_onway', title: '调出在途数', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'quantity_tuneout_30days', title: '30天调出数', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'amount_tuneout_30days', title: '30天调出金额', width: '150', sortable: true, iscount: true
                },
                {
                    field: 'quantity_tunein_30days', title: '30天调入数', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'amount_tunein_30days', title: '30天调入金额', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'oneday_total_sales', title: '昨日总销量', width: '130', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '昨日总销量' + '\',1,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.oneday_total_sales + '</a>' : data.oneday_total_sales);
                }
                },
                {
                    field: 'amount_onedaysale', title: '昨日总销售额', width: '130', sortable: true, iscount: true
                },
                {
                    field: 'sevenday_total_sales', title: '七天销量', width: '100', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '七天销量' + '\',7,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.sevenday_total_sales + '</a>' : data.sevenday_total_sales);
                    }
                },
                {
                    field: 'sevenday_total_sales_amount', title: '七天销售额', width: '120', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '七天销售额' + '\',7,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.sevenday_total_sales_amount + '</a>' : data.sevenday_total_sales_amount);
                    }
                },
                {
                    field: 'fourteenday_total_sales', title: '十四天销量', width: '120', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '十四天销量' + '\',14,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.fourteenday_total_sales + '</a>' : data.fourteenday_total_sales);
                    }
                },
                {
                    field: 'fourteenday_total_sales_amount', title: '十四天销售额', width: '120', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '十四天销售额' + '\',14,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.fourteenday_total_sales_amount + '</a>' : data.fourteenday_total_sales_amount);
                    }
                },
                {
                    field: 'thirtyday_total_sales', title: '三十天销量', width: '130', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '三十天销量' + '\',30,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.thirtyday_total_sales + '</a>' : data.thirtyday_total_sales);
                    }
                },
                {
                    field: 'thirtyday_total_sales_amount', title: '三十天销售额', width: '130', sortable: true, iscount: true, formatter: function (idx, data) {
                        return ((data.hq_type != 'FBA仓' && data.hq_type != '半成品仓') ? '<a href="javascript:;" onclick="ShowOrderList(\'' + data.warehouse_name + '三十天销售额' + '\',30,\'' + data.warehouse_code + '\',\'' + data.bailun_sku + '\')">' + data.thirtyday_total_sales_amount + '</a>' : data.thirtyday_total_sales_amount);
                    }
                },
                {
                    field: 'cash_in_30days', title: '30天现金流收入', width: '140', sortable: true, iscount: true, formatter: function (idx, data) {
                        return '<a href="javascript:;" onclick="ShowCashFlow(\'' + data.warehouse_code + '\',\'' + data.warehouse_name + '\',\'' + data.bailun_sku + '\')">' + data.cash_in_30days + '</a>';
                }
                },
                {
                    field: 'cash_out_30days', title: '30天现金流支出', width: '140', sortable: true, iscount: true, formatter: function (idx, data) {
                        return '<a href="javascript:;" onclick="ShowCashFlow(\'' + data.warehouse_code + '\',\'' + data.warehouse_name + '\',\'' + data.bailun_sku + '\')">' + data.cash_out_30days + '</a>';
                }
                },
                { field: 'cash_net', title: '净值(30d)', width: '130', sortable: true, iscount: true },
                { field: 'pin_rate_stock', title: '动销率%(30d)', width: '120', sortable: true, iscount: true },
                { field: 'available_days', title: '可销售天数', width: '110', sortable: true },
                { field: 'stock_age_6months', title: '半年以上库龄', width: '130', sortable: true },
                { field: 'stock_age_6months_amount', title: '半年以上库龄金额', width: '150', sortable: true }
            ];

            var s = '';

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

            var url = '@Url.Content("~/Reports/Warehouse/ListWareHouseSkuJson")' + '?' + $("#toolbar").serialize() + '&skucategoryids=' + s + '&onedaysalescount=@(ViewBag.onedaysalescount)' + '&isfinish=@(ViewBag.isfinish)' +'&category=@(ViewBag.category)';

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

                        //if (d.isexport == 1) {
                        //    $('#btnexport').show();
                        //}
                    }
                }, {
                    fixedcol: true,
                    fixedcolnum: 1
                });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function listwarehouse() {
            $.submit({
                url: '@Url.Content("~/Home/ListWareHouse")',
                paramData: '',
                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 listcategory() {
            $.submit({
                url: '@Url.Content("~/Reports/Orders/ListSkuCategory")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#categoryid').html('<option value="">请选择sku分类</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#categoryid').append('<option value="' + result[i].c_ID + '">' + result[i].c_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 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" });
            }
        }

        function exportlist() {
            if (confirm("确定要导出SKU明细吗？")) {

                var s = '';

                try {
                    s = getChkNode();
                } catch (e) {
                    s = '';
                }
                window.open('@Url.Content("~/Reports/Warehouse/ExportWareHouseSku?")' + $("#toolbar").serialize() + '&skucategoryids=' + s+'&onedaysalescount=@(ViewBag.onedaysalescount)', '_blank');
            }
        }

        function ShowCashFlow(code, name,sku) {
            layer_show(name + '现金流明细', 'http://aims.bailuntec.com/view/reports/cash-flow-sku.html?warehouse_code=' + code+'&bailun_sku='+sku, '90%', '90%');
        }

        function ShowOrderList(name,t,warehousecode,sku) {
            var start = '@(ViewBag.StartOneDate)';
            var end = '@(ViewBag.EndDate)';

            if (t == 7) {
                start = '@(ViewBag.StartSevenDate)';
            }
            else if (t == 14) {
                start = '@(ViewBag.StartFourteenDate)';
            }
            else if (t == 30) {
                start = '@(ViewBag.StartThirtyDate)';
            }

            var url = 'http://oms.bailuntec.com/#/bailun-order/all-orders?baseQueryKey=SkuNo_Accurate&dateType=payTime&baseQueryValue=' + sku + '&daterange=' + start + '&daterange=' + end + '&wareNo=' + warehousecode + '&orderStatus=0&orderStatus=1&orderStatus=3';
            console.log(url);
            layer_show(name + " 的订单明细", url, '90%', '90%');
        }

    </script>

}

