{extend name="public/container"}
{block name="content"}
<div class="layui-fluid" style="background: #fff;margin-top: -10px;">
    <div class="layui-tab layui-tab-brief" lay-filter="tab">
        <ul class="layui-tab-title">
            <li lay-id="list" {eq name='type' value='1' }class="layui-this" {
            /eq} >
            <a href="{eq name='type' value='1'}javascript:;{else}{:Url('index',['type'=>1])}{/eq}">全部商品</a>
            </li>
            <li lay-id="list" {eq name='type' value='2' }class="layui-this" {
            /eq}>
            <a href="{eq name='type' value='2'}javascript:;{else}{:Url('index',['type'=>2])}{/eq}">审核中商品({$checking})</a>
            </li>
            <li lay-id="list" {eq name='type' value='3' }class="layui-this" {
            /eq}>
            <a href="{eq name='type' value='3'}javascript:;{else}{:Url('index',['type'=>3])}{/eq}">已通过商品({$checkok})</a>
            </li>
            <li lay-id="list" {eq name='type' value='4' }class="layui-this" {
            /eq}>
            <a href="{eq name='type' value='4'}javascript:;{else}{:Url('index',['type'=>4])}{/eq}">已驳回商品({$checkno})</a>
            </li>
        </ul>
    </div>
    <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">
                            <div class="layui-inline">
                                <label class="layui-form-label">所有分类</label>
                                <div class="layui-input-block">
                                    <select name="cate_id">
                                        <option value=" ">全部</option>
                                        {volist name='cate' id='vo'}
                                        <option value="{$vo.id}">{$vo.html}{$vo.cate_name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商品名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="store_name" class="layui-input"
                                           placeholder="请输入商品名称,关键字,编号">
                                    <input type="hidden" name="type" value="{$type}">
                                </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-primary layui-btn-sm export" 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="layui-btn-container">
                        {switch name='type'}
                        {case value="1"}
                        <a class="layui-btn layui-btn-sm" href="{:Url('create')}">添加商品</a>
                        {/case}
                        {case value="2"}
                        <a class="layui-btn layui-btn-sm" href="{:Url('create')}">添加商品</a>
                        {/case}
                        {case value="4"}
                        <button class="layui-btn layui-btn-sm" data-type="take_products">重新提交审核</button>
                        {/case}
                        {/switch}
                    </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="checkboxstatus">
                        {{# if(d.status == 2){ }}
                        驳回
                        {{# }else if(d.status == 1){ }}
                        通过
                        {{# } else { }}
                        审核中
                        {{# } }}
                    </script>
                    <!--收藏-->
                    <script type="text/html" id="like">
                        <span><i class="layui-icon layui-icon-praise"></i> {{d.like}}</span>
                    </script>
                    <!--点赞-->
                    <script type="text/html" id="collect">
                        <span><i class="layui-icon layui-icon-star"></i> {{d.collect}}</span>
                    </script>
                    <!--商品名称-->
                    <script type="text/html" id="store_name">
                        <h4>{{d.store_name}}</h4>
                        <p>价格:<font color="red">{{d.price}}</font></p>
                        {{# if(d.cate_name!=''){ }}
                        <p>分类:{{d.cate_name}}</p>
                        {{# } }}
                    </script>
                    <script type="text/html" id="allstock">
                        {{+d.stock+(+d.sales)}}
                    </script>
                    <!--操作-->
                    <script type="text/html" id="act">
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event='edit'>
                            编辑
                        </button>
                        {{# if(d.status != 1){ }}
                        <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">
                            {{# if(d.status == 0){ }}
                            {if condition="$user_store eq 0"}
                            <li>
                                <a href="javascript:void(0);" lay-event='check_ok'>
                                    <i class="fa fa-check"></i> 通过
                                </a>
                            </li>
                            <li>
                                <a href="javascript:void(0);" lay-event='check_no'>
                                    <i class="fa fa-close"></i> 驳回
                                </a>
                            </li>
                            {/if}
                            {{# } }}
                            {{# if(d.status == 2){ }}
                            <li>
                                <a href="javascript:void(0);" lay-event='check'>
                                    <i class="fa fa-trash"></i> 提交审核
                                </a>
                            </li>
                            {{# } }}
                        </ul>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="{__ADMIN_PATH}js/layuiList.js"></script>
<script>
    var type = <?=$type?>;
    //实例化form
    layList.form.render();
    //加载列表
    layList.tableList('List', "{:Url('product_ist',['type'=>$type])}", function () {
        var join = new Array();
        switch (parseInt(type)) {
            case 1:
            case 2:
            case 3:
                join = [
                    {field: 'id', title: 'ID', sort: true, event: 'id', width: '6%'},
                    {field: 'from_store_name', title: '所属门店', width: '6%'},
                    {field: 'image', title: '商品图片', templet: '#image', width: '10%'},
                    {field: 'store_name', title: '商品名称', templet: '#store_name'},
                    {field: 'ficti', title: '虚拟销量', edit: 'ficti', width: '8%'},
                    {field: 'sort', title: '排序', edit: 'sort', width: '6%'},
                    {field: 'status', title: '状态', templet: "#checkboxstatus", width: '8%'},
                    {field: 'right', title: '操作', align: 'center', toolbar: '#act', width: '14%'},
                ];
                break;
            case 4:
                join = [
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', sort: true, event: 'id', width: '6%'},
                    {field: 'from_store_name', title: '所属门店', width: '6%'},
                    {field: 'image', title: '商品图片', templet: '#image', width: '10%'},
                    {field: 'store_name', title: '商品名称', templet: '#store_name'},
                    {field: 'ficti', title: '虚拟销量', edit: 'ficti', width: '8%'},
                    {field: 'sort', title: '排序', edit: 'sort', width: '6%'},
                    {field: 'status', title: '状态', templet: "#checkboxstatus", width: '8%'},
                    {field: 'reason', title: '驳回原因', width: '10%'},
                    {field: 'right', title: '操作', align: 'center', toolbar: '#act', width: '14%'},
                ];
                break;
        }
        return join;
    })
    //excel下载
    layList.search('export', function (where) {
        where.excel = 1;
        location.href = layList.U({c: 'store.store_product_check', a: 'product_ist', q: where});
    })
    //下拉框
    $(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.tool(function (event, data, obj) {
        switch (event) {
            case 'check':
                var url = layList.U({c: 'store.store_product_check', a: 'check', q: {id: data.id}});
                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 'check_ok':
                var url = layList.U({c: 'store.store_product_check', a: 'check_ok', q: {id: data.id}});
                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 'check_no':
                $eb.createModalFrame(data.store_name + '-驳回原因', layList.U({a: 'check_no', q: {id: data.id}}), {h: 550, w: 720});
                break;
            case 'open_image':
                $eb.openImage(data.image);
                break;
            case 'edit':
                location.href = layList.U({a: 'create', q: {id: data.id}});
                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;
        }
    });
    //查询
    layList.search('search', function (where) {
        layList.reload(where, true);
    });
    //自定义方法
    var action = {
        take_products: function () {
            var ids = layList.getCheckData().getIds('id');
            if (ids.length) {
                layList.basePost(layList.Url({
                    c: 'store.store_product_check',
                    a: 'take_products'
                }), {ids: ids}, function (res) {
                    layList.msg(res.msg);
                    location.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}
