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 Business_AddBranch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            txtBranch .Attributes.Add("onKeyPress", "return KeyPressFuncNotSpecialCharecter();");
            txtBranchName .Attributes.Add("onKeyPress", "return KeyPressFuncNotSpecialCharecter();");
            txtBranch.Attributes.Add("onKeyPress", "return KeyPressFuncIsWholeNumber();");
            this.pnl_confirmation.Visible = false;
            ExceptionUtility.LogException(0, "", "83", "Page_Load", "");
        }
        lblError.Text = "";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        string error = "";
        ExceptionUtility.LogException(0, "", "83.489", "btnSave_Click", "");
        bool saved = addbranch(ref error);
        if (saved)
        {
            lblError.Text = "Branch Added";
        }
        else
        {
            if (Helper.OracleError(error))
            {
                lblError.Text = "An error occured!";
                ExceptionUtility.LogException(0, "", "83.489", "btnSave_Click", error);
            }
            else
                lblError.Text = error;
        }
        
        pnlResult.Visible = true;
        pnlResult.Enabled = true;
        pnl_filters.Visible = true;
        pnlControls.Visible = false;
      
        loadBranch();
    }
    private void loadBranch()
    {
        string parameters = "";
        try
        {
            string currentMethod = "";
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "BANK_LKP_EDITOR.Get_BRANCHES";

           
            oracom.Parameters.Add("p_branch_cd", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;         
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_lang"].Value = "EN";
            parameters = "p_lang-EN:";
            if (txtBranchCode.Text.Trim().Length > 0)
            {
                parameters = parameters + "p_branch_cd-" + txtBranchCode .Text .Trim ();
                oracom.Parameters["p_branch_cd"].Value = txtBranchCode.Text.Trim();
            }
            else
                oracom.Parameters["p_branch_cd"].Value = OracleString.Null;

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

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

            oracon.Close();
            if (currentMethod.Contains("Button1_Click"))
                ExceptionUtility.LogException(0, "BANK_LKP_EDITOR.Get_BRANCHES","487.1", "loadBranch", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BANK_LKP_EDITOR.Get_BRANCHES","487.1", "loadBranch",parameters );
            GridView1.DataSource = null;
            GridView1.DataBind();
           // //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
    private bool addbranch(ref string error)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.add_branch";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;         
            oracom.Parameters.Add("p_branch_cd", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_name", 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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            parameters = "p_lang-EN:";
            oracom.Parameters["p_branch_cd"].Value = txtBranch.Text;
            if (txtBranch.Text.Length > 0)
                parameters = parameters + "p_branch_cd-" + txtBranch.Text;
            oracom.Parameters["p_branch_name"].Value = txtBranchName.Text;
            if(txtBranchName.Text .Length >0)
                parameters = parameters + ":p_branch_name-" + txtBranch.Text;
            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (error.Contains("Exception - ORA-00001: unique constraint (ASR.LKP_BRANCH_PK) violated"))
            {
                error = "Branch Code Exists";
                ExceptionUtility.LogException(9, "bank_lkp_editor.add_branch", "489.1", "addbranch", parameters );
            }
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "bank_lkp_editor.add_branch", "489.1", "addbranch",parameters );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "bank_lkp_editor.add_branch", "489.1", "addbranch",parameters );
                return false;
            }
        }
        catch (Exception ex)
        {
         
            ExceptionUtility.LogException(ex, "bank_lkp_editor.add_branch","489.1", "addbranch", parameters );
           // //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    private bool deletebranch(ref string error)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "BANK_LKP_EDITOR.delete_branch";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_cd", 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_branch_cd"].Value = ViewState["branch"];           
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            parameters = "p_branch_cd-" + ViewState["branch"] + ":p_lang-EN";
            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "BANK_LKP_EDITOR.delete_branch", "490.1", "deletebranch", parameters );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "BANK_LKP_EDITOR.delete_branch", "490.1", "deletebranch", parameters );
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BANK_LKP_EDITOR.delete_branch","490.1", "deletebranch", parameters);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "83.487", "Button1_Click", "");
            loadBranch ();
            pnlResult.Visible = true;
           
       
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         if (e.CommandName.Equals("d"))
        {
            this.pnl_confirmation.Visible = true;
            this.btnAdd.Visible = false;
            this.pnlControls.Visible = false;
            this.pnlResult.Visible = false;
            this.pnl_filters.Visible = false;
            
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            ViewState["branch"] = rowID;
            ExceptionUtility.LogException(0, "", "83.490", "GridView1_RowCommand.Delete", "");
        }
    }
    protected void btn_conf_ok_Click(object sender, EventArgs e)
    {
        string branchID = ViewState["branch"].ToString();
        ViewState["mode"] = "d";
        string error = "";      
        bool delete = false;
        delete = deletebranch(ref error);
        if (delete == true)
        {
            lblError.Text = "Branch Deleted";
        }
        else
        {
            if (Helper.OracleError(error))
            {
                lblError.Text = "An error occured!";
                ExceptionUtility.LogException(0, "", "", "btn_conf_ok_Click", error);
            }
            else
                lblError.Text = error;
        }

        this.pnlResult.Visible = true;
        this.pnl_confirmation.Visible = false;
        this.pnl_filters.Visible = true;
        this.btnAdd.Visible = true;
        loadBranch( );
    }
    protected void btn_conf_cancel_Click(object sender, EventArgs e)
    {
        this.pnl_confirmation.Visible = false;
        this.pnl_filters.Visible = true;
        this.pnlResult.Visible = true;
        this.btnAdd.Visible = true;
        ExceptionUtility.LogException(0, "", "", "btn_conf_cancel_Click", "");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ExceptionUtility.LogException(0, "", "", "GridView1_PageIndexChanging", "");
        loadBranch();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
      
        pnlControls.Visible = true;
        pnlResult.Enabled = false;
        ViewState["mode"] = "a";      
        txtBranch .Text = "";
        ExceptionUtility.LogException(0, "", "83.488", "btnAdd_Click", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
        ExceptionUtility.LogException(0, "", "83.488", "btnAdd_Click", "");
        loadBranch ();
    }
}