/**
用户管理
 */
layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    table.render({
        elem: '#LAY-manage'
        , url: '/api/user_search'
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { title: '操作', width: 150, align: 'center',  toolbar: '#table-toolbar' }
            , { field: 'user', title: '用户名', minWidth: 150 }// sort: true
            , { field: 'status', title: '类型',templet: '#statusTemp'}
        ]]
        , page: true
        , limit: 15
        , limits: [10, 15, 20, 25, 30, 50]
        , text: { none: '暂无数据!' }
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 0 //规定成功的状态码，默认：0
        }
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.count, //解析数据长度,区分大小写
                "data": res.data //解析数据列表,区分大小写
            };
        }
    });
    //监听搜索
    form.on('submit(LAY-front-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-manage', {
            where: field
        });
    });

    table.on('tool(LAY-manage)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除选中的用户?', function (index) {
                $.ajax(
                    {
                        type: "POST",
                        url: '/api/user_delete',
                        cache: false,//禁止缓存
                        data: {
                            'user': data.user
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            var res = XMLHttpRequest.responseJSON;
                            if (res != undefined)
                                layer.msg(res.message, { icon: 0, time: 2000 });
                        },
                        success: function (data) {
                            //obj.del();
                            layer.close(index);
                            layui.table.reload('LAY-manage'); //重载表格
                        }
                    });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑用户'
                , area: ['600px', '350px']
                , id: 'LAY-popup-user-add'
                , shadeClose: false
                , success: function (layero, index) {
                    view(this.id).render('system/userform', data).done(function () {
                        form.render(null, 'layuiadmin-form');
                        var container = $(view(this.id).container);
                        var _user = container.find('input[name="user"]');
                        var _pwd = container.find('input[name="pwd"]');
                        if (_user != undefined)
                            _user.val(data.user);
                        if (_pwd != undefined)
                            _pwd.val(data._pwd);
                        _user.attr("disabled", "disabled");
                        //监听提交
                        form.on('submit(LAY-front-submit)', function (data) {
                            var field = data.field; //获取提交的字段
                            if (field.pwd != field.repwd) {
                                layer.msg('确认密码不一致', { icon: 0, time: 2000 });
                                return;
                            }
                            field.pwd = md5(field.pwd);
                            $.ajax(
                                {
                                    type: "POST",
                                    url: '/api/user_save',
                                    cache: false,//禁止缓存
                                    data: field,
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        var res = XMLHttpRequest.responseJSON;
                                        if (res != undefined)
                                            layer.msg(res.message, { icon: 0, time: 2000 });
                                    },
                                    success: function (data) {
                                        layer.close(index); //执行关闭 
                                        layui.table.reload('LAY-manage'); //重载表格
                                    }
                                });

                        });
                    });
                }
            });
        }
    });

    exports('user', {})
});