var AddDialog = function (parent) {
    this.Parent = parent;

    this.Setup = function () {
        this.InitOrgList();

        $("#add-dialog-sex-select span").on("click", this.OnSexButtonClick.bind(this));
        $("#sure-btn").on("click", this.OnSureButtonClick.bind(this));
        $("#cancel-btn").on("click", this.HideDialog.bind(this));
        $("#close").on("click", this.HideDialog.bind(this));
    };

    this.Show = function () {
        $('#add-dialog').show();
    };

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

    this.OnSexButtonClick = function (event) {
        $('#add-dialog-sex-select span').removeClass("active");
        $(event.target).addClass("active");
    };
    
    this.OnSureButtonClick = function () {
        this.AddUser();
        this.HideDialog();
    };

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

        this.clearInput();
    };

    this.AddUser = function () {
        $.ajax({
            type: "POST",
            dataType: 'text',
            url: '/UserManagement/Add',
            data: this.getUserParams(),
            success: function () {
                this.Parent.ReLoadTableData();
            }.bind(this)
        });
    };

    this.clearInput = function () {
        $("#name").textbox('setValue', '');
        $('#loginAccount').textbox('setValue', '');
        $('#loginPassword').textbox('setValue', '');
        $('#add-dialog-sex-select span').eq(0).addClass("active");
        $('#add-dialog-org-list').combobox('setValue', '');
    };

    this.getUserParams = function () {
        return {
            OrgId: $('#add-dialog-org-list').combobox('getValue'),
            Gender: parseInt($('#add-dialog-sex-select span.active').attr('gender')),
            RealName: $("#name").textbox('getValue'),
            LoginName: $('#loginAccount').textbox('getValue'),
            LoginPassword: $('#loginPassword').textbox('getValue')
        }
    };
};