layui.config({
    base: "/webconfig/layuiJs/"
}).define(['jquery', 'basic', 'table', 'utils'], function (exports) {
    var $ = layui.jquery;
    var table = layui.table;

    //列表页
    var mainTable = function () {
        //初始化表格
        var init = function (fn) {
            table.init('list',
                {
                    elem: '#list',
                    id: 'list',
                    toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    ,
                    defaultToolbar: [
                        'filter', 'exports', 'print',
                        { title: '筛选搜索开关', layEvent: 'LAYTABLE_TIPS', icon: 'layui-icon-search' }
                    ],
                    title: pagename,
                    even: true //开启隔行背景
                    ,
                    text: {
                        none: nodata //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                    },
                    done: fn
                });
        };

        //主列表加载，可反复调用进行刷新
        var load = function (page, rows) {
            if (!page) page = 1;
            if (!rows) rows = 15;
            AjaxGet(listapi,
                {
                    page: page,
                    limit: rows
                },
                function (result) {
                    if (!result.iserror) {
                        table.reload("list",
                            {
                                limit: rows,
                                data: result.data.list
                            });
                        layui.laypage.render({
                            elem: 'pager',
                            count: result.data.count,
                            curr: page,
                            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                            limit: rows,
                            limits: [10, 15, 20, 30, 50, 100, 500],
                            jump: function (pager, first) {
                                if (!first) {
                                    mainTable.reload(pager.curr, pager.limit);
                                }
                            }
                        });
                    }
                });
        }
        //load(1, 10);
        return {
            init: init,
            reload: load
        };
    }();

    //监听头部工具事件
    table.on('toolbar(list)',
        function(obj) {

        });



    //监听行单击事件
    table.on('row(list)', function (obj) {
        
    });

    //监听行双击事件
    table.on('rowDouble(list)', function (obj) {
        //obj 同上
    });

    //监听行工具事件
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var guid = data.Guid;
        var id = "";
        //console.log(data);
        //获取ID值
        var d = 0;
        var iid = data;
        for (e in iid) {
            if (d == 0) id = iid[e];//JSON数据，请将编号放在第一个位置
            d++;
        }

        //获取name值
        var name = "";
        var iname = data;//JSON数据，请将名字放在第二个位置
        var c = 0;
        for (i in iname) {
            if (c == 1) name = iname[i];
            c++;
        }
        if (obj.event === 'btn_del') {
            layer.confirm('真的删除' + name + '这条数据么?',
                function (index) {
                    AjaxPost(
                        delapi,
                        { guid: guid },
                        function (res) {
                            if (!res.iserror) {
                                layer.msg('成功删除1条记录!'+name);
                                obj.del();
                            }
                        }
                    );
                });
        } else if (obj.event === 'btn_edit') {
            parent.layer.open({
                type: 2,
                title: '编辑' + pagename + '【' +id+ '' + '】' + name,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: editurl + editparameter + guid,
                btn: '关闭返回',
                yes: function (index, layero) {
                    parent.layer.close(index);
                    loadtable();
                },
                cancel: function (index, layero) { loadtable(); }
            });
        } else if (obj.event === 'btn_view') {
            parent.layer.open({
                type: 2,
                title: '查看' + pagename + '【' + id + '】' + name,
                maxmin: true,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: viewurl + guid,
                btn: '关闭返回',
                yes: function (index, layero) { parent.layer.close(index); },
                cancel: function (index, layero) { }
            });

        } else if (obj.event === 'btn_examine') {
            parent.layer.open({
                type: 2,
                title: '审核' + pagename + '【' + id + '】' + name,
                maxmin: true,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: examineurl + guid,
                btn: '关闭返回',
                yes: function (index, layero) { parent.layer.close(index); },
                cancel: function (index, layero) { }
            });

        }

    });
    exports('layui_table_plus', mainTable);
});