$(function() {

    function initData(){

        $('.selectpicker').selectpicker({
            width:643
        });
        loadRole();
        loadDept();
    }

    //提交更改
    $('#btn_submit').click(function () {
        var btype = $("#myModalLabel").text();
        var arr= $("#re_deptId").val();
        var str = "";
        for(var i=0;i<arr.length;i++){
            if(i<arr.length-1) {
                str = str + arr[i]+",";
            }else{
                str = str + arr[i];
            }
        }
        var data  =  {
            userId : $("#re_userId").val(),
            userName:$("#re_userName").val(),
            userPwd:$("#re_userPwd").val(),
            userSex:$("#re_userSex").val(),
            userAge:$("#re_userAge").val(),
            userTel:$("#re_userTel").val(),
            userEmail:$("#re_userEmail").val(),
            userPosition:$("#re_userPosition").val(),
            deptId :str,
            roleId : $("#re_roleId").val(),
            effective : $("#re_effective").val(),
            remark : $("#re_remark").val()
        }
        $.ajax({
            type: "post",
            url: "/user/add",
            data:JSON.stringify(data),
            contentType:"application/json",
            success: function(data){
                if(data=='success'){
                    //跳转到登录页面
                    window.location.href = "/login";
                }
            }
        })
    })


    //加载角色
    function loadRole(){
        $.ajax({
            type: "GET",
            url: "/role/selectList",
            dataType: "json",
            success: function(data){
                var html = '';
                $.each(data, function(commentIndex, comment){
                    html += '<option value="'+comment['roleId']+'">' + comment['roleName']
                        + '</option>';
                });
                $('#re_roleId').append(html);
            }
        });
    }
    //加载科室
    function loadDept(){
        $.ajax({
            type: "GET",
            url: "/dept/selectList",
            dataType: "json",
            success: function(data){
                var html = '';
                $.each(data, function(commentIndex, comment){
                    html += '<option value="'+comment['dept_id']+'">' + comment['dept_name']
                        + '</option>';
                });
                $('#dept_id').append(html);
                $('#re_deptId').append(html);
                $('.selectpicker').selectpicker('val','1');
                $('.selectpicker').selectpicker('refresh');
            }
        });
    }

    initData();
});