var dept_selectinput;
var usertemp;
var role_selectinput;


function loadUserData(nodedata) {
    var pnode = nodedata;
    resetform();


    var enable = true;

    if (edittype == "update") {
        $("#user_title").html("修改用户信息")
        enable = false;
    } else {
        enable = true;
    }

    if (edittype == "add") {
        $("#dept_name").val("")
        $("#user_title").html("添加用户")
    }

    if (edittype == "edit") {
        $("#user_title").html("修改用户")
    }

    if (role_selectinput == null) {
        role_selectinput = $('#user_role_select').comboTree({
            source: roles,
            isMultiple: true
        });
    }
    role_selectinput.unbind();
    role_selectinput.closeDropDownMenu();

    if (!enable) {
        role_selectinput.unbind();
        $("#user_role_select").attr("disabled", true);
    } else {
        $("#user_role_select").attr("disabled", false);
        role_selectinput.bindings();
    }

    $(".ComboTreeItemChlid").find("input").prop('checked', false);

    if (edittype == "edit" || edittype == "update") {
        usertemp = nodedata;
        pnode = [{ "id": usertemp.organid }]
        initUserInfo()
    }




    dept_selectinput = new TreeSelect({
        element: '#user_dept_select',
        data: zdata,
        valueKey: "id",
        enable: enable,
        placeholder: "选择部门",
        selectvalue: pnode.length > 0 ? pnode[0].id : null
    });

    $("#user_add_btn").off("click");
    $("#user_add_btn").on("click", function() {
        addUser();
    })

}

function resetform() {
    $("#user_name").val("");
    $("#user_true_name").val("");
    $("#user_workno").val("");
    $("#user_password").val("");
    $("#user_confirm_password").val("")
        // $("#user_birth").val("")
    $("#user_mobile").val("")
    $("#user_position").val("")
    $("#user_idno").val("")
    $("#user_email").val("")
    $("#user_role_select").val("")
    $("#user_img_add").removeClass("hide");
    $("#user_img_pre").addClass("hide");


    var html = "";
    for (var i = 0; i < job_array.length; i++) {
        html += "<option value='" + job_array[i].id + "'>" + job_array[i].name + "</option>"
    }
    $("#user_position").html(html);






}

function initUserInfo() {

    $("#user_name").val(usertemp.username);
    $("#user_true_name").val(usertemp.truename);
    $("#user_workno").val(usertemp.workno);
    $("#user_password").val(usertemp.password);
    $("#user_confirm_password").val(usertemp.password)
        // $("#user_birth").val(moment.unix(usertemp.birth / 1000).format("YYYY-MM-DD"))
    $("#user_mobile").val(usertemp.mobile)
    $("#user_position").val(usertemp.positionid)
    $("#user_idno").val(usertemp.idno)
    $("#user_email").val(usertemp.email)

    $("#user_img_pre2").html("")
    $("#user_img_add").removeClass("hide");
    $("#user_img_pre").addClass("hide");
    if (typeof(usertemp.pic) != "undefinde" && usertemp.pic != null) {
        $("#user_img_pre").removeClass("hide");
        $("#user_img_add").addClass("hide");
        var $section = $("#user_img_pre2");
        var $span = $("<span class='up-span'>");
        $span.appendTo($section);
        var $img0 = $("<img class='close-upimg'>").on("click", function(event) {
            event.preventDefault();
            event.stopPropagation();
            // $(".works-mask").show();
            // delParent = $(this).parent();

            $section.html("")
            $("#user_img_add").removeClass("hide");
            $("#user_img_pre").addClass("hide");

        });
        $img0.attr("src", "/js/lib/imageupload/img/a7.png").appendTo($section);
        var $img = $("<img class='up-img up-opcity' id='user_pic' style='height:200px;width: auto;'>");
        $img.attr("src", base_image_server_url + usertemp.pic);
        $img.appendTo($section);
        var $p = $("<p class='img-name-p'>");
        $p.html("image").appendTo($section);
        var $input = $("<input id='taglocation' name='taglocation' value='' type='hidden'>");
        $input.appendTo($section);
        var $input2 = $("<input id='tags' name='tags' value='' type='hidden'/>");
        $input2.appendTo($section);
        $(".up-section").removeClass("loading");
        $(".up-img").removeClass("up-opcity");
    }

    var attr = [];
    if (usertemp.roles.length > 0) {
        for (var j = 0; j < usertemp.roles.length; j++) {
            var ii = {};
            ii["id"] = usertemp.roles[j].id;
            ii["title"] = usertemp.roles[j].des;
            attr.push(ii);

            $("span[data-id='" + usertemp.roles[j].id + "']").find("input").prop('checked', true)
        }
    }



    role_selectinput._selectedItems = attr;
    role_selectinput.refreshInputVal();


}


function addUser() {

    var username = $("#user_name").val();
    var userworkno = $("#user_workno").val();
    var userpassword = $("#user_password").val();
    var userconfirmpassword = $("#user_confirm_password").val();
    var userpic = $("#user_pic").attr("path");
    var usermobile = $("#user_mobile").val();
    var useridno = $("#user_idno").val();
    var useremail = $("#user_email").val();
    // var userbirth = $("#user_birth").val();
    var userposition = $("#user_position").val();
    var usertruename = $("#user_true_name").val();
    var userroles = role_selectinput.getSelectedItemsId();
    if (userpassword != null && typeof(userpassword) != "undefined" && userpassword != userconfirmpassword) {
        layer.msg('密码不一致！', {
            time: 2000, //20s后自动关闭
        });
        return;
    }
    var parm = {
        "organid": dept_selectinput.value,
        "positionid": userposition,
        "truename": usertruename,
        "roleids": userroles
    }
    if (!parm.roleids) {
        layer.msg('用户角色不能为空！', {
            time: 2000, //20s后自动关闭
        });
        return;
    }
    if (userpassword != "" && userpassword != null && typeof(userpassword) != "undefined") {
        parm["password"] = userpassword;
    } else {
        // layer.msg('密码不能为空！', {
        //     time: 2000, //20s后自动关闭
        // });
        // return;
    }
    if (usermobile != null && typeof(usermobile) != "undefined") {
        parm["mobile"] = usermobile;
        parm["phone"] = usermobile;
    }
    if (username != "" && username != null && typeof(username) != "undefined") {
        parm["username"] = username;
    } else {
        layer.msg('账号不能为空！', {
            time: 2000, //20s后自动关闭
        });
        return;
    }
    if (userworkno != null && typeof(userworkno) != "undefined") {
        parm["workno"] = userworkno;
    }
    if (useridno != null && typeof(useridno) != "undefined") {
        parm["idno"] = useridno;
    }
    if (useremail != null && typeof(useremail) != "undefined") {
        parm["email"] = useridno;
    }
    if (userpic != null && typeof(userpic) != "undefined") {
        parm["pic"] = userpic;
    }




    if (edittype == "edit" || edittype == "update") {
        parm["id"] = usertemp.id;
        UserUpdate(parm, function(data) {
            layer.msg('修改成功！', {
                time: 2000, //20s后自动关闭
            }, function() {
                if (edittype == "edit") {
                    updateUser(dept_selectinput.value)
                    $('#addUserModal').modal('hide')
                }
                if (edittype == "update") {
                    $('#addUserModal').modal('hide')
                    GetUserInfo(function(data) {
                        var html = "<i class=\"icon icon-user\"></i>  " + data.truename + " " + data.organname + ",欢迎您"
                        $("#user").html(html);
                    }, function(error) {})
                }
            });
        }, function(error) {

        });

        return;
    }

    UserAdd(parm, function(data) {
        layer.msg('添加成功！', {
            time: 2000, //20s后自动关闭
        }, function(data) {
            $('#addUserModal').modal('hide');
            updateUser(dept_selectinput.value);
        });
    }, function(error) {
        layer.msg('添加失败！输入参数不正确！', {
            time: 2000, //20s后自动关闭
        });
    });


}