<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<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"/>
</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 pb10 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 class="layui-form" id="form1" action="" style="margin-top: 10px;">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">驳回原因</label>
                            <div class="layui-input-block">
                                <input type="text" name="rejectName" id="rejectName" placeholder="请输入驳回原因"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="merchantRejectDescTab" lay-filter="merchantRejectDescTab"></table>
        </div>
    </div>
</div>
<script type="text/html" id="edit">
    <form class="layui-form" id="form2" style="margin-top: 2rem;">
        <input name="id" id="id" hidden>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">驳回原因</label>
            <div class="layui-input-block">
                    <textarea class="layui-textarea" name="rejectName" id="rejectNameEdit"
                              autocomplete="off"></textarea>
                <%--                <input type="text" name="rejectName" id="rejectNameEdit" style="cursor: pointer" placeholder="请输入驳回原因"  autocomplete="off" class="layui-input">--%>
            </div>
        </div>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="sort" id="sort" style="cursor: pointer" placeholder="请输入排序"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="leftBar">
    <a class="layui-btn layui-btn-normal layui-btn-sm" 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>
</script>

<script type="text/html" id="toolBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>编辑
    </a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">
        <i class="layui-icon layui-icon-delete"></i>删除
    </a>
</script>

<script>
    var sort = '${sort}';
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var tableIns = table.render({
            elem: '#merchantRejectDescTab'
            , url: '${ctx}/merchantRejectDesc/page'
            , id: 'merchantRejectDesc'
            , 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 //解析数据列表
                };
            }
            , toolbar: '#leftBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter']
            , title: '商户审核驳回描述信息表'
            , cols: [[
                {type: 'checkbox', fixed: 'left', hidden: true}
                , {field: 'numbers', title: '序号', fixed: 'left', unresize: true, width: 80, type: 'numbers'}
                , {field: 'rejectName', title: '驳回原因', width: 900}
                , {field: 'sort', title: '排序', sort: true}
                , {
                    field: 'createTime',
                    title: '创建时间',
                    width: 160,
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {
                    field: 'updateTime',
                    title: '修改时间',
                    width: 160,
                    templet: "<div>{{layui.util.toDateString(d.updateTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作', width: 200, toolbar: '#toolBar'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
        });
        table.on('tool(merchantRejectDescTab)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'edit') { //编辑
                //do something
                toAdd(obj);
                //同步更新缓存对应的值
                // obj.update({
                //     rejectName: '123'
                //     ,sort: 'xxx'
                // });
            } else if (layEvent === 'del') { //编辑
                layer.confirm('真的删除么?', {icon: 3}, function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    del(data);
                    //向服务端发送删除指令
                });
            }
        });

        //监听头工具栏事件
        table.on('toolbar(merchantRejectDescTab)', function (obj) {
            switch (obj.event) {
                case 'add':
                    // 添加
                    toAdd();
                    break;
                case 'search':
                    var params = serialize($('#form1'));
                    table.reload('merchantRejectDesc', {
                        where: params
                        , page: {
                            curr: 1
                        }
                    });
                    break;
            }
        });

        function toAdd(obj) {
            var msg = '新增';
            if (obj !== undefined) {
                msg = '编辑';
            }
            var index = layer.open({
                type: 1
                , shade: 0.5
                , title: msg
                , maxmin: true
                , content: $("#edit").html()
                , area: ['400px', '400px']
                , btn: ['保存', '取消',]
                , shadeClose: true
                , btn1: function (index, layero) {
                    if (!$("#rejectNameEdit").val()) {
                        layer.msg("驳回原因不能为空！");
                        return;
                    }
                    if (!$("#sort").val()) {
                        layer.msg("排序不能为空!");
                        return;
                    }
                    layer.close(index);
                    toEdit(obj);
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
                , success: function () {
                    if (obj != undefined) {
                        $("#id").val(obj.data.id);
                        $("#rejectNameEdit").val(obj.data.rejectName);
                        $("#sort").val(obj.data.sort);
                    } else {
                        $("#sort").val(sort);
                    }
                }
            });
            form.render();
        }

        function toEdit(obj) {
            var data = $("#form2").serializeObject();
            $.ajax({
                url: '${ctx}/merchantRejectDesc/saveOrUpdate',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                        if (obj != undefined) {
                            obj.update({
                                rejectName: data.rejectName,
                                sort: data.sort
                            });
                            // $("[lay-event=edit]").hide();
                        } else {
                            tableIns.reload();
                            sort++;
                        }
                    } else {
                        layer.msg(res.data, {
                            icon: 2
                        });
                    }
                }
            })
        }

        function del(obj) {
            $.ajax({
                url: '${ctx}/merchantRejectDesc/del/' + obj.id,
                method: 'DELETE',
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                        sort++;
                    } else {
                        layer.msg(res.data, {
                            icon: 2
                        });
                    }
                }
            })
        }
    });

    jQuery.prototype.serializeObject = function () {
        var obj = new Object();
        $.each(this.serializeArray(), function (index, param) {
            if (!(param.name in obj)) {
                obj[param.name] = param.value;
            }
        });
        return obj;
    };
</script>
</body>
</html>
