<title>货品表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsProducts/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="请输入商品名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="barcode" placeholder="请输入商品编码" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="sn" placeholder="请输入货品条码" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="spesDesc" placeholder="请输入货品规格" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsProducts-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>

            <div class="layui-inline layui-inline-6 float-right">
                <div class="layui-form-mid float-right text-right text-red">
                    <i class="layui-icon layui-icon-tips"></i>  库存支持点击数字进行直接编辑
                </div>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsProducts-tableBox" lay-filter="LAY-app-CoreCmsProducts-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsProducts-tableBox-bar">
    <a class="layui-btn layui-btn-xs" lay-event="detail">记录</a>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , view = layui.view;

                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsProducts-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsProducts-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsProducts-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsProducts/GetPageList',
                    method: 'POST',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-189',//面包屑142px,搜索框4行172,3行137,2行102,1行67
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            //{ type: "checkbox", fixed: "left" },
                            { field: 'id', title: '序列', width: 60, sort: false },
                            {
                                field: 'images', title: '图片', width: 50, sort: false,
                                templet: function (d) {
                                    if (d.images) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.images + '")><image style="max-width:28px;max-height:28px;" src="' + d.images + '"/></a>';
                                    } else {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + setter.noImagePicUrl + '")><image style="max-width:30px;max-height:30px;" src="' + setter.noImagePicUrl + '"/></a>';
                                    }
                                }
                            },
                            { field: 'name', title: '商品名称', sort: false },
                            { field: 'spesDesc', title: '规格值', sort: false },
                            { field: 'barcode', title: '商品条码', sort: false, width: 125 },
                            { field: 'sn', title: '货品编码', sort: false, width: 130 },
                            { field: 'price', title: '销售价', sort: false, width: 60 },
                            { field: 'costprice', title: '成本价', sort: false, width: 60 },
                            { field: 'mktprice', title: '市场价', sort: false, width: 60 },
                            //{ field: 'marketable', title: '是否上架', width: 95, templet: '#switch_marketable', sort: false, unresize: true },
                            { field: 'weight', title: '重量(千克)', sort: false, width: 80 },
                            { field: 'unit', title: '单位', sort: false, width: 60 },
                            { field: 'stock', title: '库存（单机编辑）', sort: false, width: 120, edit: 'text' },
                            {
                                field: 'freezeStock', title: '冻结库存', sort: false, width: 80, templet: function (data) {
                                    if (data.freezeStock > 0) {
                                        return "<button type=\"button\" class=\"layui-btn layui-btn-warm layui-btn-xs\">" + data.freezeStock + "</button>";
                                    } else {
                                        return data.freezeStock;
                                    }
                                }
                            },
                            //{ field: 'isDefalut', title: '是否默认货品', width: 95, templet: '#switch_isDefalut', sort: false, unresize: true },
                            //{ field: 'isDel', title: '是否删除', width: 95, templet: '#switch_isDel', sort: false, unresize: true },
                            { width: 62, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsProducts-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsProducts-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsProducts-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsProducts-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        }
                    });


                //监听单元格编辑
                table.on('edit(LAY-app-CoreCmsProducts-tableBox)', function (obj) {
                    var value = obj.value //得到修改后的值
                        , data = obj.data //得到所在行所有键值
                        , field = obj.field; //得到字段
                    console.log(obj.value);
                    if (!!!obj.value) {
                        layer.msg("不能为空");
                        return false;
                    }

                    if(obj.value < 0) {
                        layer.msg('库存不能为负数');
                        table.reload('LAY-app-CoreCmsProducts-tableBox');
                        return false;
                    }


                    if (field == 'stock') {
                        if (coreHelper.isNumber(obj.value)) {
                            layer.confirm('确定要将库存设置为 ' + obj.value + " 吗?", {
                                btn: ['确定', '取消']
                            }, function (index, layero) {
                                coreHelper.Post("Api/CoreCmsProducts/DoSetStock", { id: data.id, data: value }, function (e) {
                                    if (debug) { console.log(e); } //开启调试返回数据
                                    //table.reload('LAY-app-CoreCmsProducts-tableBox');
                                    layer.close(index); //再执行关闭
                                    layer.msg(e.msg);
                                });
                            });
                        } else {
                            layer.msg("库存必须为数字");
                            table.reload('LAY-app-CoreCmsProducts-tableBox');
                            return false;
                        }

                    } else if (field == 'freezeStock') {
                        if (coreHelper.isNumber(obj.value)) {
                            coreHelper.Post("Api/CoreCmsHouseCategory/DoSetFreezeStock", { id: data.id, data: value }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                //table.reload('LAY-app-CoreCmsProducts-tableBox');
                                layer.msg(e.msg);
                            });
                        } else {
                            layer.msg("冻结库存必须为数字");
                            return false;
                        }
                    }
                });



                //执行预览操作
                function doDetails(obj) {
                    coreHelper.Post("Api/CoreCmsProducts/GetDetails", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '查看记录',
                                area: ['1230px', '90%'],
                                id: 'LAY-popup-CoreCmsProducts-details',
                                success: function (layero, index) {
                                    view(this.id).render('stock/products/details', { data: e.data }).done(function () {
                                        form.render();
                                    });
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

            });
    };
</script>

<!--设置是否上架-->
<script type="text/html" id="switch_marketable">
    <input type="checkbox" name="switch_marketable" lay-skin="switch" lay-text="开启|关闭" disabled="disabled" lay-filter="switch_marketable" {{ d.marketable ? 'checked' : '' }}>
</script>

<!--设置是否默认货品-->
<script type="text/html" id="switch_isDefalut">
    <input type="checkbox" name="switch_isDefalut" lay-skin="switch" lay-text="开启|关闭" disabled="disabled" lay-filter="switch_isDefalut" {{ d.isDefalut ? 'checked' : '' }}>
</script>

<!--设置是否删除-->
<script type="text/html" id="switch_isDel">
    <input type="checkbox" name="switch_isDel" lay-skin="switch" lay-text="开启|关闭" disabled="disabled" lay-filter="switch_isDel" {{ d.isDel ? 'checked' : '' }}>
</script>

