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: {
                page: 1,
                rows: 10000
            },
            success: function (result) {
                $('#add-dialog-org-list').combobox({
                    valueField: 'Id',
                    textField: 'Name',
                    editable: false,
                    panelHeight: 'auto',
                    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.validation();
    };

    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(0);
                $('#org-list').combobox('setValue', 0);
            }.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-sex-select span').eq(1).removeClass('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')
        }
    };

    this.validation = function () {
        if ($("#name").textbox('getValue').trim() === '' || $("#name").textbox('getValue').trim === null) {
            alert('请输入姓名');
            return
        } else if ($('#add-dialog-org-list').combobox('getValue') === '') {
            alert('请选择机构');
            return;
        } else if ($("#loginAccount").textbox('getValue').trim() === '' || $("#loginAccount").textbox('getValue').trim === null) {
            alert('请输入登录账户');
            return
        } else if ($("#loginPassword").textbox('getValue').trim() === '' || $("#loginPassword").textbox('getValue').trim === null) {
            alert('请输入登录密码');
            return
        } else if ($("#loginPassword").textbox('getValue').trim().length < 6) {
            alert('密码长度至少为6位');
            return
        } else {
            this.AddUser();
            this.HideDialog();
        }
    };
};