@{
    ViewData["Title"] = "SKU进销存";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "财务报表", "SKU进销存" };
}

<div class="row">
    <div class="col-sm-12">
        <div class="alert alert-warning">
            说明：差异=期末-期初
        </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>
                        <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>
                            <option value="耗材仓">耗材仓</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>仓库：</label>
                        <select id="warehouse" name="warehouse" class="form-control">
                            <option value="">请选择仓库</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>日期</label>
                        <input id="start" name="start" class="form-control" style="width:100px" value="@ViewBag.start" />至
                        <input id="end" name="end" class="form-control" style="width:100px" value="@ViewBag.end" />
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <input id="sku" name="sku" class="form-control" style="width:120px" placeholder="sku编码" />
                    </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>
                    </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{
    <style>
        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
}

@section scripts{
    <script type="text/javascript">
        var first = true;
        var tb;
        $(document).ready(function () {
            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 160));

            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });
            
            $('#warehousetype').change(function () {
                listwarehouse();
            })

            if ('@ViewBag.warehousetype' != '') {
                $('#warehousetype').val('@ViewBag.warehousetype');
                $('#warehousetype').trigger('change');

            }
            else {
                first = false;
                list();
            }
        })

        function list(t,w) {
            var columns = [
                { field: 'bailun_sku', title: 'Sku', width: '120' },
                {
                    field: 'sku_title_cn', title: 'sku名称', width: '180', formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.sku_title_cn + '">' + data.sku_title_cn + '</div>';
                    }
                },
                { field: 'warehouse_name', title: '仓库名称', width: '180', iscount: true },
                { field: 'start_stock_amount', title: '期初-在库金额', width: '130', iscount: true, sortable: true },
                { field: 'start_stock_count', title: '期初-在库数量', width: '130', iscount: true, sortable: true },
                { field: 'start_onway_amount', title: '期初-在途金额', width: '130', iscount: true, sortable: true },
                { field: 'start_onway_count', title: '期初-在途数量', width: '130', iscount: true, sortable: true },
                { field: 'end_stock_amount', title: '期末-在库金额', width: '130', iscount: true, sortable: true },
                { field: 'end_stock_count', title: '期末-在库数量', width: '130', iscount: true, sortable: true },
                { field: 'end_onway_amount', title: '期末-在途金额', width: '130', iscount: true, sortable: true },
                { field: 'end_onway_count', title: '期末-在途数量', width: '130', iscount: true, sortable: true },
                { field: 'diff_stock_amount', title: '差异-在库金额', width: '130', iscount: true, sortable: true },
                { field: 'diff_stock_count', title: '差异-在库数量', width: '130', iscount: true, sortable: true },
                { field: 'diff_onway_amount', title: '差异-在途金额', width: '130', iscount: true, sortable: true },
                { field: 'diff_onway_count', title: '差异-在途数量', width: '130', iscount: true, sortable: true },
                { field: 'amount', title: '总金额(差异)', width: '130', iscount: true, sortable: true },
                { field: 'count', title: '总数量(差异)', width: '130', iscount: true, sortable: true }
            ];

            var url = '@Url.Content("~/Reports/Finance/SkuPurchaseSellStockJson")' + '?' + $("#toolbar").serialize();

            if (tb == undefined) {
                tb = OnlyTable("roletable", columns, url, "", {
                    showfooter: true, showCount: true
                });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function listwarehouse() {
            var type = $('#warehousetype').val();

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

                        if (first) {
                            $('#warehouse').val('@ViewBag.warehouse');
                            list();
                            first = false;
                        }

                    }
                    
                }
            })
        }

    </script>

}

