using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Pages : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        
        if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            SqlDataSource1.DeleteParameters["original_PID"].DefaultValue = Uid;
            SqlDataSource1.Delete();
            GridView1.DataBind();
        }
        else if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            pnlGrid.Enabled = false;

            ViewState["uid"] = Uid;
            //put the data in the controls
            txtPageName.Text = GridView1.Rows[index].Cells[0].Text;
            txtURL.Text = GridView1.Rows[index].Cells[2].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[1].Text;
            txtModule.Text = GridView1.Rows[index].Cells[4].Text;
            int status = int.Parse(GridView1.Rows[index].Cells[3].Text);
            if (status == 1)
                rdActive.Checked = true;
            else
                rdInActive.Checked = true;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "a";
        pnlControls.Visible = true;
        pnlGrid.Enabled = false;
        clearControls();
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = null;
        ViewState["uid"] = null;
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        clearControls();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();

            if (mode.Equals("a"))
            {
                SqlDataSource1.InsertParameters["NAME_EN"].DefaultValue = txtPageName.Text;
                SqlDataSource1.InsertParameters["DESCRIPTION"].DefaultValue = txtDesc.Text;
                SqlDataSource1.InsertParameters["URL"].DefaultValue = txtURL.Text;
                SqlDataSource1.InsertParameters["MID"].DefaultValue = txtModule.Text;
                if (rdActive.Checked)
                    SqlDataSource1.InsertParameters["STATUS"].DefaultValue = "1";
                else
                    SqlDataSource1.InsertParameters["STATUS"].DefaultValue = "0";

                SqlDataSource1.Insert();
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["uid"].ToString();
                SqlDataSource1.UpdateParameters["original_PID"].DefaultValue = Uid;
                SqlDataSource1.UpdateParameters["NAME_EN"].DefaultValue = txtPageName.Text;
                SqlDataSource1.UpdateParameters["DESCRIPTION"].DefaultValue = txtDesc.Text;
                SqlDataSource1.UpdateParameters["URL"].DefaultValue = txtURL.Text;
                SqlDataSource1.UpdateParameters["MID"].DefaultValue = txtModule.Text;
                if (rdActive.Checked)
                    SqlDataSource1.UpdateParameters["STATUS"].DefaultValue = "1";
                else
                    SqlDataSource1.UpdateParameters["STATUS"].DefaultValue = "0";

                SqlDataSource1.Update();
            }
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            pnlControls.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();
            //setMessage("The page is saved successfully.");
        }
        catch (Exception ex)
        {
            if(ex.Message.Equals("ORA-00001: unique constraint (NCB.SEC_USER_UK) violated\n"))
            {
                //this user id already exists...
            }
        }
    }

    #region Business Functions

    private void clearControls()
    {
        txtPageName.Text = "";
        txtDesc.Text = "";
        txtModule.Text = "";
        txtURL.Text = "";
    }

    #endregion

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Visible = false;
        }
    }

    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
}
