using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Diagnostics;

public partial class sysparams : System.Web.UI.Page
{
    
  

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            
            getParams();
        }
        lblError.Text = "";

    }

    private void getParams()
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "system_param_editor.get_parametes";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;


        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    private bool deleteParameter(string paramID, ref string error)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "system_param_editor.delete_parameter";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        oracom.Parameters["p_param_id"].Value = paramID;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

    private bool addParameter(ref string error)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "system_param_editor.add_parameter";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_value_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_value_lkp", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        oracom.Parameters["p_param_name"].Value = txtName.Text;
        oracom.Parameters["p_param_value_type"].Value = ddlRecordType.SelectedValue;
        oracom.Parameters["p_param_value_lkp"].Value = txtDesc.Text;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

    private bool updateParameter(string paramID, ref string error)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "system_param_editor.update_parameter_def";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_value_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_value_lkp", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        oracom.Parameters["p_param_id"].Value = paramID;
        oracom.Parameters["p_param_name"].Value = txtName.Text;
        oracom.Parameters["p_param_value_type"].Value = ddlRecordType.SelectedValue;
        oracom.Parameters["p_param_value_lkp"].Value = txtDesc.Text;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

    private bool updateParameterValue(string paramID, string value, ref string error)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "system_param_editor.edit_parameter";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_param_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_new_value", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        oracom.Parameters["p_param_id"].Value = paramID;
        oracom.Parameters["p_new_value"].Value = value;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return 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();
            string error = "";
            bool temp = deleteParameter(Uid, ref error);
            if (temp)
                lblError.Text = "Parameter Deleted";
            else
                lblError.Text = error;
            getParams();
        }
        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
            txtName.Text = GridView1.Rows[index].Cells[0].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[4].Text.Replace("&nbsp;", "");
            ddlRecordType.SelectedValue = GridView1.Rows[index].Cells[4].Text;
            Notifications.showModal("modal-xl", this);
        }
        else if (e.CommandName.Equals("p"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            //pnlCOntrols2.Visible = true;
            //pnlGrid.Enabled = false;

            ViewState["uid"] = Uid;
            //get the lookup type
            string type = GridView1.Rows[index].Cells[3].Text;
            ViewState["type"] = type;

            //show control with a similar type
            if (type.Equals("0"))
            {
                txtValue.Visible = true;
                ddlValue.Visible = false;
                chkBox.Visible = false;

                txtValue.Text = GridView1.Rows[index].Cells[1].Text;
            }
            else if (type.Equals("1"))
            {
                txtValue.Visible = false;
                ddlValue.Visible = false;
                chkBox.Visible = true;

                string chkValue = GridView1.Rows[index].Cells[1].Text;
                if (chkValue.Equals("Y"))
                    chkBox.Checked = true;
            }
            else if (type.Equals("2"))
            {
                txtValue.Visible = false;
                ddlValue.Visible = true;
                chkBox.Visible = false;

                //create items in ddl
                string list = GridView1.Rows[index].Cells[4].Text;
                string[] valueList = list.Split(';');
                ddlValue.Items.Clear();
                foreach (string s in valueList)
                {
                    ddlValue.Items.Add(new ListItem(s, s));
                }
                try
                {
                    ddlValue.SelectedValue = GridView1.Rows[index].Cells[1].Text;
                }
                catch (Exception ex)
                {
                }
            }






            txtName.Text = GridView1.Rows[index].Cells[0].Text;
            txtDesc.Text = GridView1.Rows[index].Cells[4].Text.Replace("&nbsp;", "");
            ddlRecordType.SelectedValue = GridView1.Rows[index].Cells[3].Text;

            Notifications.showModal("modal-xl1", this);
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "a";
        //pnlControls.Visible = true;
        //pnlGrid.Enabled = false;
        clearControls();
        Notifications.showModal("modal-xl", this);
    }

    

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                bool saved = addParameter(ref error);
                if (saved)
                {
                    lblError.Text = "Parameter Saved";
                    Notifications.showMessage("Parameter Saved", this, Notifications.successMessage, true);
                }
                else
                {
                    lblError.Text = error;
                    Notifications.showMessage(error, this, Notifications.errorMessage, true);
                }
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["uid"].ToString();
                string status = "";
                bool saved = updateParameter(Uid, ref error);
                if (saved)
                {
                    lblError.Text = "Parameter Saved";
                    Notifications.showMessage("Parameter Saved", this, Notifications.successMessage, true);
                }
                else
                {
                    lblError.Text = error;
                    Notifications.showMessage(error, this, Notifications.errorMessage, true);
                }
            }
            getParams();
            ViewState["mode"] = null;
            ViewState["uid"] = null;
            //pnlControls.Visible = false;
            //pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {

        }
    }

    

    protected void btnSaveValue_Click(object sender, EventArgs e)
    {
        try
        {
            string type = ViewState["type"].ToString();
            string error = "", value = "";
            if (type.Equals("0"))
                value = txtValue.Text;
            else if (type.Equals("1"))
            {
                if (chkBox.Checked)
                    value = "Y";
                else
                    value = "N";
            }
            else if (type.Equals("2"))
                value = ddlValue.SelectedValue;


            string Uid = ViewState["uid"].ToString();
            bool saved = updateParameterValue(Uid, value, ref error);
            if (saved)
            {
                lblError.Text = "Parameter Saved";
                Notifications.showMessage("Parameter Saved", this, Notifications.successMessage, true);
            }
            else
            {
                lblError.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, true);
            }

            getParams();
            ViewState["type"] = null;
            ViewState["uid"] = null;
            //pnlCOntrols2.Visible = false;
            //pnlGrid.Enabled = true;
            clearControls();
        }
        catch (Exception ex)
        {

        }
    }

    private void clearControls()
    {
        txtName.Text = "";
        txtDesc.Text = "";
    }

    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;
            e.Row.Cells[4].Visible = false;
        }
    }

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

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getParams();
    }

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            GridView1.UseAccessibleHeader = true;
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }
    
}