var EditDialog = function (parent) {
    this.Parent = parent;
    this.User = {};

    this.Setup = function () {
        $("#sex span").on("click", this.OnSexButtonClick.bind(this));
        $("#edit-sure-btn").on("click", this.OnSureButtonClick.bind(this));
        $("#edit-cancel-btn").on("click", this.HideDialog.bind(this));
        $("#edit-close").on("click", this.HideDialog.bind(this));
    };

    this.Show = function (data) {
        this.User = data;
        this.InitOrgList();
        $('#edit-dialog').show();
        $("#username").textbox('setValue', data.RealName);
        $("#account").textbox('setValue', data.LoginName);
        //$("#password").textbox('setValue', data.LoginPassword);
        parseInt($('#sex span').eq(0).attr('gender')) === data.Gender ? $('#sex span').eq(0).addClass('active') : $('#sex span').eq(0).removeClass('active');
        parseInt($('#sex span').eq(1).attr('gender')) === data.Gender ? $('#sex span').eq(1).addClass('active') : $('#sex span').eq(1).removeClass('active');
    };

    this.InitOrgList = function () {
        $.ajax({
            type: "POST",
            dataType: 'text',
            url: '/OrgManagement/Query',
            data: {
                page: 1,
                rows: 10000
            },
            success: function (result) {
                console.log(this.User.OrgId)
                $('#edit-dialog-org-list').combobox({
                    valueField: 'Id',
                    textField: 'Name',
                    editable: false,
                    panelHeight: 'auto',
                    data: JSON.parse(result).rows
                });
                
                $('#edit-dialog-org-list').combobox('setValue', this.User.OrgId);
            }.bind(this)
        });
    };

    this.OnSexButtonClick = function (event) {
        $('#sex span').removeClass("active");
        $(event.target).addClass("active");
    };

    this.OnSureButtonClick = function () {
        this.validation();
    };

    this.validation = function () {
        if ($("#username").textbox('getValue').trim() === '' || $("#username").textbox('getValue').trim === null) {
            alert('请输入姓名');
            return
        } else if ($('#edit-dialog-org-list').combobox('getValue') === '') {
            alert('请选择机构');
            return;
        } else if ($("#account").textbox('getValue').trim() === '' || $("#account").textbox('getValue').trim === null) {
            alert('请输入登录账户');
            return
        }
        //else if ($("#password").textbox('getValue').trim() === '' || $("#password").textbox('getValue').trim === null) {
        //    alert('请输入登录密码');
        //    return
        //}
        else if ($("#password").textbox('getValue').trim().length < 6) {
            alert('密码长度至少为6位');
            return
        } else {
            this.User.OrgId = $('#edit-dialog-org-list').combobox('getValue');
            this.User.Gender = parseInt($('#sex span.active').attr('gender'));
            this.User.RealName = $("#username").textbox('getValue');
            this.User.LoginName = $('#account').textbox('getValue');
            this.User.LoginPassword = $('#password').textbox('getValue');

            this.EditUser();
            this.HideDialog();
        }
    };

    this.HideDialog = function () {
        $('#edit-dialog').hide();

        this.clearInput();
    };

    this.EditUser = function () {
        $.ajax({
            type: "POST",
            dataType: 'text',
            url: '/UserManagement/Update',
            data: this.User,
            success: function () {
                this.Parent.ReLoadTableData($('#org-list').combobox('getValue'));
            }.bind(this)
        });
    };

    this.clearInput = function () {
        $('#edit-dialog-org-list').combobox('setValue', ''),
        $("#username").textbox('setValue', ''),
        $('#account').textbox('setValue', ''),
        //$('#password').textbox('setValue', ''),
        $('#sex span').eq(0).addClass("active");
    };
};