<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../common.jsp"/>

    <style>
        /*.layui-table-cell {*/
        /*    text-align: center;*/
        /*    height: auto;*/
        /*    white-space: normal;*/
        /*}*/

        .layui-table img {
            max-width: 300px
        }
    </style>
</head>
<body>
<div class="row animated fadeInRight" style="margin: 0;">
    <div class="wrapper wrapper-content mt5 right-content ml5 mr5">
        <div class="toolbar-panel bdd">
            <div class="toolbar-box">
                <div class="toolbar-head clearfix lgraybg pt5 pb5 pl10 bdbd">
                    <!-- 顶部按钮 -->
                    <div class="fl">
                        查询条件
                    </div>
                    <div class="tools fr">
                        <a href="javascript:void(0);" class="collapse dblock f14 dgray mr5">
                            <i class="fa-2x fa fa-angle-double-up"></i>
                        </a>
                    </div>
                </div>
            </div>

            <!--搜索条件 -->
            <div class="toolbar-body">
                <form id="form1" class="layui-form" action="" style="margin-top: 10px">
                    <div class="layui-form-item">
                        <!-- <span class="dlblock w100 textr">ID：</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
                        <input type="text" placeholder="请输入内容" id="searchId">
                        </span>  -->

                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userId" placeholder="请输入用户ID" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">一级分类</label>
                            <div class="layui-input-inline">
                                <select name="typeFirst" lay-filter="typeFirst">
                                    <option value="">请选择一级分类</option>
                                    <c:if test="${typeFirstList != null}">
                                        <c:forEach items="${typeFirstList}" var="item">
                                            <option value="${item.value}">${item.name}</option>
                                        </c:forEach>
                                    </c:if>
                                </select>
                            </div>
                            <label class="layui-form-label">二级分类</label>
                            <div class="layui-input-inline">
                                <select name="typeSecond" lay-filter="typeSecond">
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <table class="layui-hide" id="demo" lay-filter="test"></table>
    </div>
</div>

<script id="typeSecond0" type="text/html">
    <c:if test="${typeSecondList0 != null}">
        <option value="">选择二级分类</option>
        <c:forEach var="item" items="${typeSecondList0}">
            <option value="${item.value}">${item.name}</option>
        </c:forEach>
    </c:if>
</script>

<script id="typeSecond1" type="text/html">
    <c:if test="${typeSecondList1 != null}">
        <option value="">选择二级分类</option>
        <c:forEach var="item" items="${typeSecondList1}">
            <option value="${item.value}">${item.name}</option>
        </c:forEach>
    </c:if>
</script>

<script id="barDemo" type="text/html">
    <a lay-event="edit" class="layui-btn layui-btn-xs">
        <i class="layui-icon layui-icon-edit"></i>
        编辑
    </a>

    <a lay-event="del" class="layui-btn layui-btn-xs layui-btn-danger">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </a>
</script>


<script type="text/html" id="imgTemp">
    <img src="{{d.icon}}" width="50px" height="50px" alt=""/>
</script>

<script type="text/html" id="toolBar">
    <a class="layui-btn layui-btn-sm " href="javascript:void(0);" lay-event="search">
        <i class="layui-icon layui-icon-search"></i>
        查询
    </a>
    <a class="layui-btn layui-btn-sm" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        添加
    </a>
    <a class="layui-btn layui-btn-sm layui-btn-danger">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </a>
</script>

<script>
    var laypage, layer, table, form;

    layui.use(['laypage', 'layer', 'mDropdown', 'table', 'upload', 'element', 'form'], function () {
        laypage = layui.laypage;
        layer = layui.layer; //弹层
        table = layui.table; //表格
        dropdown = layui.mDropdown;
        form = layui.form;

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            , id: 'idTest'
            , url: '${ctx}/notice/page' //数据接口
            , autoSort: false
            , where: {
                column: 'createDt'
                , asc: false
            }
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //默数据列表的字段名称，认：data
                , statusCode: 200
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , title: 'app首页菜单表'
            , page: true //开启分页
            , toolbar: '#toolBar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: ['filter']
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: '', title: '序号', type: 'numbers'}
                , {field: 'userId', title: '用户id', width: 80, sort: false}
                , {
                    field: 'userName', title: '用户名', width: 80, align: "center", templet: function (r) {
                        if (r.userId == -1) {
                            return '全员'
                        } else {
                            return r.userName
                        }
                    }
                }
                , {
                    field: 'typeFirst', title: '类别', minWidth: 100, align: "center", templet: function (r) {
                        var content;
                        if (r.typeFirst == 0) {
                            content = "分润通知"
                        } else {
                            content = "系统通知"
                        }

                        if (r.typeSecond == 0) {
                            content += " - 身份认证"
                        }
                        if (r.typeSecond == 1) {
                            content += " - 商家认证"
                        }
                        if (r.typeSecond == 2) {
                            content += " - 店员退款审核"
                        }
                        if (r.typeSecond == 4) {
                            content += " - 邀请注册"
                        }
                        if (r.typeSecond == 5) {
                            content += " - 银行卡认证"
                        }
                        if (r.typeSecond == 6) {
                            content += " - 商户分润"
                        }
                        if (r.typeSecond == 99) {
                            content += " - 其他"
                        }
                        if (r.typeSecond == 7) {
                            content += " - 升级收益"
                        }
                        if (r.typeSecond == 8) {
                            content += " - 商户资料修改"
                        }
                        if (r.typeSecond == 9) {
                            content += " - 结算卡修改"
                        }
                        if (r.typeSecond == 10) {
                            content += " - 商户违规"
                        }
                        if (r.typeSecond == 11) {
                            content += " - 商户异常"
                        }
                        if (r.typeSecond == 12) {
                            content += " - 商户冻结"
                        }

                        return content
                    }
                }
                , {field: 'title', title: '标题', minWidth: 150}
                , {field: 'desc', title: '简介', minWidth: 150}
                , {field: 'url', title: '内容链接', minWidth: 150}
                , {field: 'conrtent', title: '通知内容', minWidth: 150}
                , {
                    field: 'createDt',
                    title: '创建时间',
                    width: 160,
                    sort: true,
                    hide: false,
                    templet: "<div>{{layui.util.toDateString(d.createDt, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {field: 'noticeCode', hide: true, title: '通知编号', minWidth: 100}
                , {field: 'orderNumber', hide: true, title: '订单号', minWidth: 100}
                , {fixed: 'right', title: '操作', minWidth: 85, align: 'center', toolbar: '#barDemo'}
            ]]
            , done: function (res) {
            }
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'search':
                    search(obj);
                    break;
                case 'add':
                    toAdd()
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        toEdit(checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        del(data)
                    }
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
            } else if (layEvent === 'del') {
                del(data)
            } else if (layEvent === 'edit') {
                //layer.msg('编辑操作');
                toEdit(data.id);
            } else if (layEvent === 'bandage') {
                bandage(data.id);
            }
        });

        //监听排序事件
        table.on('sort(test)', function (obj) { //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            console.log(obj.field); //当前排序的字段名
            console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
            console.log(this); //当前排序的 th 对象

            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('idTest', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                , where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    column: obj.field //排序字段
                    , asc: obj.type === 'asc' //排序方式
                }
            });

            layer.msg('服务端排序。order by ' + obj.field + ' ' + obj.type);
        });

        form.on('select(typeFirst)', function(data) {
            console.log("data.value : " + data.value);
            var html;
            if (data.value === "0") {
                html = document.getElementById("typeSecond0").innerHTML;
            } else if (data.value === "1") {
                html = document.getElementById("typeSecond1").innerHTML;
            } else {
                html = "";
            }

            var typeSecond = $('select[name="typeSecond"]');
            typeSecond.empty();
            typeSecond.append(html);
            form.render('select');
        });
    });

    function toEdit(id) {
        var url = ctx + '/notice/detail/' + id;
        var index = layer.open({
            type: 2
            , title: "通知详情"
            , shade: 0.5
            , maxmin: true
            , content: url
            , area: ['753px', '700px']
        });
    }

    function toAdd() {
        var url = ctx + '/notice/detail';
        var index = layer.open({
            type: 2
            , title: "通知详情"
            , shade: 0.5
            , maxmin: true
            , content: url
            , area: ['753px', '700px']
            , end: function () {
                time: 1000
            }
        });
    }

    function bandage(id) {
        var url = '${ctx}/appMenu/bandage/' + id;
        var index = layer.open({
            type: 2
            , title: "用户角色"
            , shade: 0.5
            , maxmin: true
            , content: url
            , area: ['400px', '300px']
        });
    }

    function del(obj) {
        var url = '${ctx}/notice/del/' + obj.id;
        layer.confirm('确认删除', {
            yes: function () {
                $.ajax({
                    url: url,
                    type: 'DELETE',
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.message);
                            table.reload('idTest');
                        } else {
                            layer.msg(res.message, {
                                icon: 5
                            });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败", {
                            icon: 5
                        });
                    }
                })
            }
        });
    }

    function search(obj) {
        var params = serialize($('#form1'));
        params.column = obj.config.where.column;
        params.asc = obj.config.where.asc;
        table.reload('idTest', {
            page: {
                curr: 1
            },
            initSort: obj,
            where: params
        });
    }
</script>
</body>

</html>