var dept_selectinput;
var usertemp;
var role_selectinput;


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

    if (edittype == "add") {
        $("#dept_name").val("")
    }

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

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

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


    }




    dept_selectinput = new TreeSelect({
        element: '#user_dept_select',
        data: zdata,
        valueKey: "id",
        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_img_add").removeClass("hide");
    $("#user_img_pre").addClass("hide");

}

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": 1,
        "truename": usertruename,
        "roleids": userroles
    }

    if (userpassword != null && typeof (userpassword) != "undefined") {
        parm["password"] = userpassword;
    }
    if (usermobile != null && typeof (usermobile) != "undefined") {
        parm["mobile"] = usermobile;
        parm["phone"] = usermobile;
    }
    if (username != null && typeof (username) != "undefined") {
        parm["username"] = username;
    }
    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") {
        parm["id"] = usertemp.id;
        UserUpdate(parm, function (data) {
            layer.msg('修改成功！', {
                time: 2000, //20s后自动关闭
            }, function () {
                $('#addUserModal').modal('hide')
                updateUser(dept_selectinput.value)
            });
        }, function (error) {

        });

        return;
    }

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

    });


}