define (['doT', 'text!temp/user_template_tpl.html','ckfinder','ckfinderStart', 'css!style/home.css',"datetimepicker",'pagination','bootstrapJs'], function (doT, template) {
    var user = {
        pageNo: 1, /*第几页*/
        pageSize: 10, /*每页显示多少条*/

        urls: '',
        init: function () {
            //初始化dot
            $ ("body").append (template);
            user.getList ();
            user.event ();
            //时间控件初始化
         /*   $('#datetimepicker').datetimepicker({
                format: 'yyyy-MM-dd',
                language: 'zh-CN',
                pickTime: false
            }).on('changeDate',function(){
                $(this).datetimepicker('hide');
            });*/
        },
        event: function () {
            $ (".Bannertu").click (function () {
                BrowseServer ('cover_image');
            });
            $("#search").click(function(){
                user.getList(1);
            });

            $("#reset").click(function () {
                document.getElementById("form_search").reset();
            });

            $("#add_user").click(function () {
                user.add_user();
            });

            $("#close").click(function () {
                document.getElementById("add_user_form").reset();
            });

            $("#confirm_delete").click(function(){
                var params = {};
                params.id     = $ ("#delete_id").val ();
                params.status = 1;
                if(!params.id || params.id == null){
                    alert ("要删除的id不能为空");
                    return false;
                }
                user.delete_user(params);
            });


            $("#edit_user").click(function () {
                var params = {};
                params.id  = $ ("#edit_id").val ();
                params.type = 'edit';
                params.user_phone = $("#edit_user_form input[name='phone']").val();
                params.pwd = $("#edit_user_form input[name='password']").val();               
                user.edit_user(params);
            });
        },
        getList: function (pageNo) {
            user.pageNo   = pageNo;
            var params    = {};
            params.name   = $("input[name='name']").val();
            params.phone  = $("input[name='phone']").val();
            params.invite = $("input[name='invite']").val();
            params.pageNo = user.pageNo;
            params.pageSize = user.pageSize;

            $.ajax ({
                url: '/index/usersList',
                type: 'GET',
                async: true,
                data: params,
                dataType: 'json',
                success: function (data) {
                    var temp = document.getElementById ('user_list_tpl').innerHTML;
                    var doTtmpl = doT.template (temp);
                    $ ("#users_list").html (doTtmpl (data.data.list));

                    /*分页代码*/
                    $ ("#pagediv").pagination ({
                        length: data.data.total,
                        current: pageNo,
                        every: user.pageSize,
                        onClick: function (el) {
                            user.getList (el.num.current);
                        }
                    });
                }
            });
        },
        add_user : function () {
            var params = {};
            params.realname    = $("#add_user_form input[name='realname']").val();
            params.user_phone  = $("#add_user_form input[name='user_phone']").val();
            params.pwd = $("#add_user_form input[name='pwd']").val();
            params.type = 'add';
            if (params.realname == '') {
                alert('姓名不能为空');
                $("input[name='realname']").focus();
                return ;
            }
            if (params.user_phone == '') {
                alert('手机号不能为空');
                $("input[name='user_phone']").focus();
                return ;
            }
            if (params.pwd == '') {
                alert('密码不能为空');
                $("input[name='pwd']").focus();
                return ;
            }

            $.ajax ({
                url: '/index/user_add',
                type: 'POST',
                async: true,
                data: params,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 200) {
                        $ ("#modal_add_user").modal ('hide');
                        alert(data.msg);
                    } else {
                        alert(data.msg);
                    }
                }
            });
        },
        edit_user : function (params) {
            $.ajax ({
                url: '/index/user_add',
                type: 'POST',
                async: true,
                data: params,
                dataType: 'json',
                success: function (data) {
                    $ ("#modal-delete").modal ('hide');
                    if (data.code == 200) {
                        $ ("#modal-do").modal ('hide');
                        alert(data.msg);
                    } else {
                        alert(data.msg);
                    }
                    user.getList (user.pageNo);
                }
            });
        },
        delete_user : function(params) {
            $.ajax ({
                url: '/index/del_user',
                type: 'POST',
                async: true,
                data: params,
                dataType: 'json',
                success: function (data) {
                    $ ("#modal-delete").modal ('hide');
                    if (data.code == "101") {
                        alert (data.msg);
                        return false;
                    }
                    user.getList (user.pageNo);
                }
            });
        }
    }
    return user;
});

function delete_user (obj) {
    $ ("#delete_id").val ($ (obj).attr ("data-id"));
}

function edit(obj) {
    var params = {}
    params.id = $(obj).attr ("data-id")
    $.ajax ({
        url: '/index/usersList',
        type: 'get',
        async: true,
        data: params,
        dataType: 'json',
        success: function (data) {
            $("#edit_user_form input[name='phone']").val(data.data.list[0].user_phone);
            $("#edit_user_form input[name='password']").val(data.data.list[0].user_pswd);
            $("#invite_name").html(data.data.list[0].realname);
            $("#invite_phone").html(data.data.list[0].phone);
        }
    });

    $ ("#edit_id").val ($ (obj).attr ("data-id"));
}