{extend name="public/container"}
{block name="content"}
<div class="layui-fluid" style="background: #fff;margin-top: -10px;">

    <div class="layui-row layui-col-space15" id="app">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            {eq name="type" value="0"}
                            <div class="layui-inline">
                                <label class="layui-form-label">门店：</label>
                                <div class="layui-input-inline">
                                    <select name="store_id" lay-verify="store">
                                        <option value="0">全部</option>
                                        {volist name="store" id="v"}
                                        <option value="{$v.id}">{$v.name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">类型：</label>
                                <div class="layui-input-inline">
                                    <select name="is_warn" lay-verify="is_warn">
                                        <option value="0">库存</option>
                                        <option value="1">警告</option>
                                    </select>
                                </div>
                            </div>
                            {/eq}
                            <div class="layui-inline">
                                <label class="layui-form-label">商品名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="key" class="layui-input" placeholder="请输入商品名称,关键字,编号">

                                </div>
                            </div>
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit="search"
                                            lay-filter="search">
                                        <i class="layui-icon layui-icon-search"></i>搜索
                                    </button>
                                    <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit="export"
                                            lay-filter="export">
                                        <i class="fa fa-floppy-o" style="margin-right: 3px;"></i>导出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!--商品列表-->
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="alert alert-info" role="alert">
                        列表[虚拟销量],[库存],[排序]可进行快速修改,双击或者单击进入编辑模式,失去焦点可进行自动保存
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="layui-btn-container" lay-submit="addKc" lay-filter="addKc">

                        <button class="layui-btn layui-btn-sm">增减库存</button>

                    </div>
                    <table class="layui-hide" id="List" lay-filter="List"></table>
                    <!--图片-->
                    <script type="text/html" id="image">
                        <img style="cursor: pointer" lay-event="open_image" src="{{d.image}}">
                    </script>
                    <!--商品名称-->
                    <script type="text/html" id="store_name">
                        <h4>{{d.store_name}}</h4>
                        <p>价格:<font color="red">{{d.price}}</font></p>
                    </script>
                    <!--操作-->
                    <script type="text/html" id="act">
                        <button type="button" class="layui-btn layui-btn-xs" onclick="dropdown(this)">操作 <span
                                    class="caret"></span></button>
                        <ul class="layui-nav-child layui-anim layui-anim-upbit">
                            <li>
                                <a href="javascript:void(0);" lay-event='edit'>
                                    <i class="fa fa-trash"></i> 入库记录
                                </a>
                            </li>
                        </ul>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="{__ADMIN_PATH}js/layuiList.js"></script>
<script>

    //实例化form
    layList.form.render();
    //加载列表
    layList.tableList('List', "{:Url('product_ist')}", function () {
        var join = new Array();
        join = [
            {field: 'id', title: 'ID', sort: true, event: 'id', width: '6%'},
            {field: 'store', title: '门店', width: '10%'},
            {field: 'image', title: '商品图片', templet: '#image', width: '10%'},
            {field: 'store_name', title: '商品名称', templet: '#store_name'},
            {field: 'in_stock', title: '库存', width: '8%'},
            {field: 'repair_sales', title: '待补货', width: '8%'},
            {field: 'store_sales', title: '销量', width: '8%'},
            {field: 'status', title: '状态', templet: "#checkboxstatus", width: '8%'},
            {field: 'right', title: '操作', align: 'center', toolbar: '#act', width: '14%'},
        ];

        return join;
    })
    //excel下载
    layList.search('export', function (where) {
        where.excel = 1;
        location.href = layList.U({c: 'company.goods', a: 'product_ist', q: where});
    })
    //点击弹窗增加库存
    layList.search('addKc', function (where) {
        layer.prompt({title: '请输入条形码', formType: 0}, function (pass, index) {
            layer.close(index);
            $eb.axios.post('{:Url("store.Placeorder/getBarCode")}' + (pass ? '?bar_code=' + pass : '')).then(function (res) {
                const data = res.data.data;
                layer.open({
                    content: '确认是否为商品' + `${data.store_name}[${data.suk}]￥${data.price}`
                    , btn: ['确定', '取消'],
                    yes: function (index) {
                        layer.close(index);
                        $eb.createModalFrame('增减库存 - ' + data.store_name, layList.U({
                            a: 'edit_stock',
                            q: {bar_code: pass}
                        }), {h: 550, w: 720})
                        // layer.prompt({title: '请输入数量', formType: 0}, function (text, index) {
                        //     layer.close(index);
                        //     layList.baseGet(layList.Url({
                        //         c: 'company.goods',
                        //         a: 'save',
                        //         p: {bar_code: pass, in_stock: text}
                        //     }), function (res) {
                        //         layList.msg(res.msg, function () {
                        //             //layList.reload();
                        //         });
                        //     });
                        // });
                    }
                });
            }).catch(function (err) {
                console.log(err);
            })

        });


    })


    //下拉框
    $(document).click(function (e) {
        $('.layui-nav-child').hide();
    })

    function dropdown(that) {
        var oEvent = arguments.callee.caller.arguments[0] || event;
        oEvent.stopPropagation();
        var offset = $(that).offset();
        var top = offset.top - $(window).scrollTop();
        var index = $(that).parents('tr').data('index');
        $('.layui-nav-child').each(function (key) {
            if (key != index) {
                $(this).hide();
            }
        })
        if ($(document).height() < top + $(that).next('ul').height()) {
            $(that).next('ul').css({
                'padding': 10,
                'top': -($(that).parent('td').height() / 2 + $(that).height() + $(that).next('ul').height() / 2),
                'min-width': 'inherit',
                'position': 'absolute'
            }).toggle();
        } else {
            $(that).next('ul').css({
                'padding': 10,
                'top': $(that).parent('td').height() / 2 + $(that).height(),
                'min-width': 'inherit',
                'position': 'absolute'
            }).toggle();
        }
    }

    //快速编辑
    layList.edit(function (obj) {
        var id = obj.data.id, value = obj.value;
        switch (obj.field) {
            case 'price':
                action.set_product('price', id, value);
                break;
            case 'stock':
                action.set_product('stock', id, value);
                break;
            case 'sort':
                action.set_product('sort', id, value);
                break;
            case 'ficti':
                action.set_product('ficti', id, value);
                break;
        }
    });
    //上下加商品
    layList.switch('is_show', function (odj, value) {
        if (odj.elem.checked == true) {
            layList.baseGet(layList.Url({
                c: 'store.store_product',
                a: 'set_show',
                p: {is_show: 1, id: value}
            }), function (res) {
                layList.msg(res.msg, function () {
                    layList.reload();
                });
            });
        } else {
            layList.baseGet(layList.Url({
                c: 'store.store_product',
                a: 'set_show',
                p: {is_show: 0, id: value}
            }), function (res) {
                layList.msg(res.msg, function () {
                    layList.reload();
                });
            });
        }
    });
    //点击事件绑定
    layList.tool(function (event, data, obj) {
        switch (event) {
            case 'delstor':
                var url = layList.U({c: 'store.store_product', a: 'delete', q: {id: data.id}});
                if (data.is_del) var code = {title: "操作提示", text: "确定恢复商品操作吗？", type: 'info', confirm: '是的，恢复该商品'};
                else var code = {title: "操作提示", text: "确定将该商品移入回收站吗？", type: 'info', confirm: '是的，移入回收站'};
                $eb.$swal('delete', function () {
                    $eb.axios.get(url).then(function (res) {
                        if (res.status == 200 && res.data.code == 200) {
                            $eb.$swal('success', res.data.msg);
                            obj.del();
                            location.reload();
                        } else
                            return Promise.reject(res.data.msg || '删除失败')
                    }).catch(function (err) {
                        $eb.$swal('error', err);
                    });
                }, code)
                break;
            case 'open_image':
                $eb.openImage(data.image);
                break;
            case 'edit':
                $eb.createModalFrame(data.store_name, layList.U({
                    a: 'bill_view',
                    q: {product_id: data.product_id, store_id: data.store_id}
                }), {h: 600, w: 1000})
                break;
            case 'attr':
                $eb.createModalFrame(data.store_name + '-属性', layList.U({a: 'attr', q: {id: data.id}}), {
                    h: 600,
                    w: 800
                })
                break;
        }
    })
    //排序
    layList.sort(function (obj) {
        var type = obj.type;
        switch (obj.field) {
            case 'id':
                layList.reload({order: layList.order(type, 'id')}, true, null, obj);
                break;
            case 'sales':
                layList.reload({order: layList.order(type, 'sales')}, true, null, obj);
                break;
        }
    });
    //查询
    layList.search('search', function (where) {
        layList.reload(where, true);
    });
    //自定义方法
    var action = {
        set_product: function (field, id, value) {
            layList.baseGet(layList.Url({
                c: 'store.store_product',
                a: 'set_product',
                q: {field: field, id: id, value: value}
            }), function (res) {
                layList.msg(res.msg);
            });
        },
        show: function () {
            var ids = layList.getCheckData().getIds('id');
            if (ids.length) {
                layList.basePost(layList.Url({
                    c: 'store.store_product',
                    a: 'product_show'
                }), {ids: ids}, function (res) {
                    layList.msg(res.msg);
                    layList.reload();
                });
            } else {
                layList.msg('请选择要上架的商品');
            }
        }
    };
    //多选事件绑定
    $('.layui-btn-container').find('button').each(function () {
        var type = $(this).data('type');
        $(this).on('click', function () {
            action[type] && action[type]();
        })
    });
</script>
{/block}
