using System;
using System.Web.UI.WebControls;
using digioz;

public partial class controls_admin_LoginManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Visible = false;
        lblMessage.Text = "";
        if (!IsPostBack)
        {
            Views.SetActiveView(List);
            BindLists();
            BindGrid();
        }
    }

    private void BindLists()
    {
        ddlParentId.DataSource = SettingsManager.GetSettingByName("LoginFilters").Children;
        ddlParentId.DataBind();
    }

    private void BindGrid()
    {
        Setting loLoginFilters = SettingsManager.GetSettingByName("LoginFilters");
        SettingList loSettings = new SettingList();
        foreach (Setting loSetting in loLoginFilters.Children)
        {
            loSettings.AddRange(loSetting.Children);
        }
        dgFilters.DataSource = loSettings;
        dgFilters.DataBind();
    }

    private void ResetDetail()
    {
        txtId.Value = "0";
        ddlParentId.SelectedValue = "0";
        txtName.Text = "";
        chkEnabled.Checked = false;
    }

    protected void OnMenuItemClick(object sender, MenuEventArgs e)
    {
        txtId.Value = "";
        switch (e.Item.Value)
        {
            case "Filters":
                BindGrid();
                Views.SetActiveView(List);
                break;
            case "Add":
                ResetDetail();
                Views.SetActiveView(Detail);
                break;
        }
    }

    protected void OnSave(object sender, EventArgs e)
    {
        Setting loSetting = new Setting(Convert.ToInt32(txtId.Value), Convert.ToInt32(ddlParentId.SelectedValue), txtName.Text.Trim(), chkEnabled.Checked);
        bool lbResult = SettingsManager.Save(loSetting);
        lblMessage.Visible = true;
        lblMessage.CssClass = lbResult ? "messageOk" : "messageError";
        lblMessage.Text = (lbResult
            ? string.Format("Login Filter '{0}' has been sucessfully updated...<br/>", Server.HtmlEncode(txtName.Text))
            : string.Format("Failed to update Login Filter '{0}'...<br/>", Server.HtmlEncode(txtName.Text)));
    }

    protected void OnCancel(object sender, EventArgs e)
    {
        ResetDetail();
        Views.SetActiveView(List);
        BindGrid();
    }

    protected void OnEditCommand(object source, DataGridCommandEventArgs e)
    {
        Views.SetActiveView(Detail);
        Setting loSetting = SettingsManager.GetSettingById(Convert.ToInt32(dgFilters.DataKeys[e.Item.ItemIndex]));
        txtId.Value = loSetting.Key.Id.ToString();
        ddlParentId.SelectedValue = loSetting.Key.ParentId.ToString();
        txtName.Text = loSetting.Name;
        chkEnabled.Checked = loSetting.Enabled;
    }

    protected void OnDeleteCommand(object source, DataGridCommandEventArgs e)
    {
        SettingsManager.Delete(Convert.ToInt32(dgFilters.DataKeys[e.Item.ItemIndex]));
        BindGrid();
    }

    protected void OnPageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgFilters.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
