<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=agent-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <style type="text/css">
        .seach_items {
            float: right;
            margin-left: 10px;
        }
    </style>
    <style type="text/css">
        .LAY-btns .layui-nav {
            padding-left: 0;
            padding-right: 10px;
            top: -4px;
            margin: 0 10px;
            border: 0;
            background-color: #009688;
        }

        .LAY-btns .layui-nav .layui-nav-item {
            line-height: 40px;
        }

        .LAY-btns .layui-nav .layui-nav-child {
            top: 34px;
        }

        .LAY-btns .layui-nav .layui-nav-bar {
            display: none;
        }

        .LAY-btns .layui-nav .layui-nav-child dd.layui-this a {
            color: #333;
            background-color: #fff;
        }

        .LAY-btns .layui-nav .layui-nav-child dd.layui-this a:hover {
            background-color: #f2f2f2;
            color: #000;
        }

        .tableContent {
            width: 100%;
            display: flex;
            justify-content: space-between;
            flex-direction: row;
        }
    </style>

</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <!--商品-->
                    <div class="LAY-btns" style="margin-bottom: 10px;">
                        <div style="float: left">
                            <button class="layui-btn" id="commodity_add" data-permission="commodity.add_commodity"><i
                                    class="layui-icon layui-icon-add-circle"></i>添加
                            </button>
                        </div>
                        <form class="layui-form" lay-filter="query-form-element">
                            <div class="seach_items">
                                <button class="layui-btn" lay-submit lay-filter="query-form-element"><i
                                        class="layui-icon layui-icon-search"></i>查询
                                </button>
                            </div>
                            <div class="seach_items">
                                <input type="text" name="name" autocomplete="off" class="layui-input"
                                       placeholder="名称"/>
                            </div>
                        </form>
                        <div style="clear: both;"></div>
                    </div>
                    <table class="layui-hide" id="commodity_datagrid" lay-filter="commodity-operate"></table>

                    <script type="text/html" id="commodity-operate-bar">
                        <div class="layui-btn-group">
                            <a class="layui-btn layui-btn-xs" lay-event="commodity_edit" data-permission="commodity.add_commodity"
                            >修改</a>
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="mainImage">
    {{# if(d.main_image){ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_main">查看</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_main">上传</a>
    {{# } }}
</script>
<script type="text/html" id="listImage">
    {{# if(d.list_image){ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_list">查看</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_list">上传</a>
    {{# } }}
</script>
<script type="text/html" id="detailImage">
    {{# if(d.detail_image){ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_detail">查看({{ d.detail_image }})</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="upload_detail">上传</a>
    {{# } }}
</script>

<script src="../../layuiadmin/layui/layui.js?t=1"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'upload'], function () {
        var $ = layui.$;
        var table = layui.table
            , form = layui.form
            , upload = layui.upload
            , admin = layui.admin;

        table.render({
            elem: '#commodity_datagrid'
            , url: '/commodity/'
            , cols: [[
                {field: 'name', title: '商品名称', width: 150}
                , {field: 'introduce', title: '简介', width: 200}
                , {field: 'category_text', title: '分类', width: 200}
                , {field: 'price', title: '销售价', width: 100}
                , {field: 'discount', title: '折扣', width: 100}
                , {field: 'stock', title: '库存', width: 100}
                , {field: 'publish_date', title: '出版日期', width: 120}
                , {field: 'main_image', title: '主图', templet: '#mainImage', width: 80}
                , {field: 'list_image', title: '列表图', templet: '#listImage', width: 80}
                , {field: 'detail_image', title: '详情图', templet: '#detailImage', width: 90}
                , {field: 'status_text', title: '状态', width: 100}
                , {field: 'create_user_text', title: '添加人', width: 100}
                , {field: 'create_time_f', title: '添加时间', width: 150}
                , {field: 'notes', title: '备注', width: 200}
                , {width: 80, align: 'center', fixed: 'right', toolbar: '#commodity-operate-bar'}
            ]]
            , page: true
            , height: 'full-108'
            , done: function () {
                layui.index.removeNoPermButtons();
            }
        });

        //监听工具条
        table.on('tool(commodity-operate)', function (obj) {
            var data = obj.data;
            table.editdata = data;
            if (obj.event === 'commodity_edit') {
                layer.open({
                    type: 2,
                    title: '修改',
                    shadeClose: false,
                    area: ['40%', '80%'],
                    btn: ['保存', '取消'],
                    yes: function (index, dom) {
                        layui.onSubmitChild = function (res) {
                            layer.close(index);
                            table.reload('commodity_datagrid', {});
                        };
                        layui.submitChild();
                    },
                    btn2: function (index, layero) {
                        layer.close(index);//关闭当前按钮
                    },
                    content: 'edit.html?id=' + data.id
                });
            } else if (obj.event === 'upload_main' || obj.event === 'upload_list') {
                layer.open({
                    type: 2,
                    title: obj.event === 'upload_main' ? '上传主图片' : '上传列表图片',
                    shadeClose: false,
                    area: ['40%', '80%'],
                    btn: ['保存', '取消'],
                    yes: function (index, dom) {
                        layui.onSubmitChild = function (res) {
                            layer.close(index);
                            table.reload('commodity_datagrid', {});
                        };
                        layui.submitChild();
                    },
                    btn2: function (index, layero) {
                        layer.close(index);//关闭当前按钮
                        table.reload('commodity_datagrid', {});
                    },
                    content: 'uploadImage.html?id=' + data.id + '&type=' + obj.event
                });
            } else if ( obj.event === 'upload_detail') {
                layer.open({
                    type: 2,
                    title: '上传详情图片',
                    shadeClose: false,
                    area: ['80%', '80%'],
                    btn: ['保存', '取消'],
                    yes: function (index, dom) {
                        layui.onSubmitChild = function (res) {
                            layer.close(index);
                            table.reload('commodity_datagrid', {});
                        };
                        layui.submitChild();
                    },
                    btn2: function (index, layero) {
                        layer.close(index);//关闭当前按钮
                        table.reload('commodity_datagrid', {});
                    },
                    content: 'uploadDetailImg.html?id=' + data.id + '&type=' + obj.event
                });
            }
        });

        form.on('submit(query-form-element)', function (data) {
            table.reload('commodity_datagrid', {
                where: data.field
                , page: {curr: 1}
            });
            layer.closeAll();
            return false
        });

        $('#commodity_add').on('click', function () {
            layer.open({
                type: 2,
                title: '添加',
                area: ['40%', '80%'],
                btn: ['保存', '取消'],
                yes: function (index, dom) {
                    layui.onSubmitChild = function (res) {
                        layer.close(index);
                        table.reload('commodity_datagrid', {});
                    };
                    layui.submitChild();
                },
                btn2: function (index, layero) {
                    layer.close(index);//关闭当前按钮
                },
                content: 'edit.html'
            });
        });
    });

</script>
</body>
</html>
