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

public partial class controls_admin_TopMenuManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RefreshGrid();
        }
    }

    private void RefreshGrid()
    {
        List<TopMenu> loGridData = TopMenuManagement.GetTopMenuItems(4);

        if (loGridData.Count > 0)
        {
            mainGrid.DataSource = loGridData;
            mainGrid.DataBind();
        }
        else
        {
            loGridData.Add(new TopMenu());
            mainGrid.DataSource = loGridData;
            mainGrid.DataBind();

            int liTotalColumns = mainGrid.Rows[0].Cells.Count;
            mainGrid.Rows[0].Cells.Clear();
            mainGrid.Rows[0].Cells.Add(new TableCell());
            mainGrid.Rows[0].Cells[0].ColumnSpan = liTotalColumns;
            mainGrid.Rows[0].Cells[0].Text = "No Records Found.";
        }
    }

    protected void MainGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        permissionIDCombo.DataSource = UserPermissionManagement.GetUserPermissions();
        permissionIDCombo.DataBind();
        permissionIDCombo.Items.Insert(0, new ListItem("Anonymous", "-1"));

        TopMenu menuItem = TopMenuManagement.GetTopMenuItems(4)[e.NewEditIndex + mainGrid.PageIndex * mainGrid.PageSize];

        ViewState["menuItemID"] = menuItem.ID;
        parentIDTextBox.Text = menuItem.ParentID.ToString();
        nameTextBox.Text = menuItem.Name;
        actionTypeCombo.SelectedValue = menuItem.ActionType;
        actionDataTextBox.Text = menuItem.ActionData;
        permissionIDCombo.SelectedValue = menuItem.PermissionID.ToString();

        mainMultiView.ActiveViewIndex = 1;
        e.Cancel = true;
    }

    protected void MainGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        TopMenuManagement.DeleteTopMenuItem(Convert.ToInt32(mainGrid.DataKeys[e.RowIndex].Values[0]));
        mainGrid.EditIndex = -1;
        RefreshGrid();
    }

    protected void MainGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RefreshGrid();
        mainGrid.PageIndex = e.NewPageIndex;
        mainGrid.DataBind();
    }

    protected void AddNewButton_Click(object sender, EventArgs e)
    {
        permissionIDCombo.DataSource = UserPermissionManagement.GetUserPermissions();
        permissionIDCombo.DataBind();
        permissionIDCombo.Items.Insert(0, new ListItem("Anonymous", "-1"));

        parentIDTextBox.Text = String.Empty;
        nameTextBox.Text = String.Empty;
        actionTypeCombo.SelectedIndex = 0;
        actionDataTextBox.Text = String.Empty;
        mainMultiView.ActiveViewIndex = 1;
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        mainMultiView.ActiveViewIndex = 0;
        int liMenuItemID = 0;
        int liParentID;
        string lsActionData;
        bool lbIsUpdating = false;

        if (ViewState["menuItemID"] != null)
        {
            liMenuItemID = Convert.ToInt32(ViewState["menuItemID"]);
            lbIsUpdating = true;
        }

        Int32.TryParse(parentIDTextBox.Text, out liParentID);

        if (actionTypeCombo.SelectedValue == "LI")
        {
            lsActionData = String.Format("~{0}", ControlsHelper.GetDecodedUrl(actionDataTextBox.Text));
        }
        else
        {
            lsActionData = actionDataTextBox.Text;
        }

        TopMenuManagement.SaveTopMenuItem(
            liMenuItemID,
            liParentID,
            nameTextBox.Text,
            actionTypeCombo.SelectedValue,
            lsActionData,
            Convert.ToInt32(permissionIDCombo.SelectedValue),
            lbIsUpdating);

        ViewState["menuItemID"] = null;
        RefreshGrid();
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        mainMultiView.ActiveViewIndex = 0;
    }
}
