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;

public partial class Business_ManageBranch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            pnlControls.Visible = false;
            pnlGrid.Visible = false;
        }
        //pnlGrid.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        pnlGrid.Visible = true;
        pnlControls.Visible = false;
        getBranch(Helper.gridSize(), 1);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        //getBranch(txtBranchCode .Text ,txtAccountNo.Text );
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string branchCode = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool temp = false;

            temp = deleteBranch(branchCode, ref error);
            if (temp)
            {
                lblError.ForeColor = System.Drawing.Color.DarkGreen;
                lblError.Text = "Branch Deleted";
            }  
            else
            {
                lblError.Text = error;
            }

           // getBranch(branchCode ,"");
            getBranch(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        }
        else if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string branchCode = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            pnlSearch.Visible = false;
            pnlGrid.Enabled = false;

            ViewState["branchCode"] = branchCode;
            //put the data in the controls
            txtBranchCode1.Text = GridView1.Rows[index].Cells[0].Text;
            txtEnBranch.Text = GridView1.Rows[index].Cells[1].Text;
           
            if (GridView1.Rows[index].Cells[2].Text == "&nbsp;")
                txtARBranch.Text = "";
            else
                txtARBranch.Text = GridView1.Rows[index].Cells[2].Text;
            //if (GridView1.Rows[index].Cells[3].Text == "&nbsp;")
            //    txtAccountNo.Text = "";
            //else 
            //    txtAccountNumber.Text = GridView1.Rows[index].Cells[3].Text;
            txtBranchCode1.ReadOnly = true;
           
          

        }


    }
    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 getBranch(int pageSize, int pageNumber)
    {
        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 = "BRANCH_INFO_EDITOR.get_branch_info";

            //oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_account_no", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).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_total_pages", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            if (txtBranchCode .Text .Length != 0)
                oracom.Parameters["p_branch_code"].Value = txtBranchCode.Text .Trim ();
            else
                oracom.Parameters["p_branch_code"].Value = OracleString.Null;           
            oracom.Parameters["p_lang"].Value = "EN";            
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            if (ex.Message.Equals("ORA-24338: statement handle not executed\n"))
            {
                //this user id already exists...
                //setMessage("No Status available");
                //lblError.Text = "No Branch Available";
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        getBranch (Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
    private bool deleteBranch(string branchCode, 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 = "BRANCH_INFO_EDITOR.delete_branch_info";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_branch_code", 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_code"].Value = branchCode;
        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 addBranch(string BranchCode,string enName, string arName, 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 = "BRANCH_INFO_EDITOR.add_branch_info";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_en_branch", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_ar_branch", OracleType.NVarChar).Direction = ParameterDirection.Input;
        //oracom.Parameters.Add("p_account_no", 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_code"].Value = BranchCode ;
        oracom.Parameters["p_en_branch"].Value = enName;
        oracom.Parameters["p_ar_branch"].Value = arName;
        //oracom.Parameters["p_account_no"].Value = accountNo;
        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 updateBranch(int branchCode, string enName, string arName,  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 = "BRANCH_INFO_EDITOR.Update_Branch_Info";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_en_branch", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_ar_branch", OracleType.NVarChar ).Direction = ParameterDirection.Input;
        //oracom.Parameters.Add("p_account_no", 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_code"].Value = branchCode;
        oracom.Parameters["p_en_branch"].Value = enName ;
        oracom.Parameters["p_ar_branch"].Value = arName ;
        //oracom.Parameters["p_account_no"].Value = accountNo ;
        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 btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                
                bool saved = addBranch(txtBranchCode1.Text ,txtEnBranch.Text  ,txtARBranch.Text , ref error);
                if (saved)
                {
                    lblError.ForeColor = System.Drawing.Color.DarkGreen;
                    lblError.Text = "Branch Saved";
                    getBranch(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
                }
                else
                {
                    
                    lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                int branchCode = Convert.ToInt16(ViewState["branchCode"]);


                bool saved = updateBranch(branchCode ,txtEnBranch .Text ,txtARBranch.Text , ref error);
                if (saved)
                {
                    lblError.ForeColor = System.Drawing.Color.DarkGreen;
                    lblError.Text = "Branch Saved";
                   // getBranch(txtBranchCode1 .Text  , txtAccountNumber.Text);
                    getBranch(Helper .gridSize (),int.Parse (ddlPages .SelectedValue));
                }
                else
                {
                    lblError.Text = error;
                }
            }
            
            ViewState["mode"] = null;
            ViewState["statusCode"] = null;
            pnlControls.Visible = false;
            pnlGrid.Visible = true;
            pnlGrid.Enabled = true;
            pnlSearch.Visible = true;
            clearControls();
        }
        catch (Exception ex)
        {
            if (ex.Message.Equals("ORA-00001: unique constraint (NCB.SEC_USER_UK) violated\n"))
            {
                //this user id already exists...
                setMessage("This Branch already exists");
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = null;
        ViewState["statusCode"] = null;
        pnlSearch.Visible = true;
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        lblError.Text = "";
        clearControls();
    }
    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "a";
        txtBranchCode1.ReadOnly = false;
        pnlControls.Visible = true;
        pnlGrid.Enabled = false;
       
        clearControls();
    }
    private void clearControls()
    {
        txtBranchCode1.Text = "";
        txtEnBranch.Text = "";
        txtARBranch .Text = "";
        //txtAccountNumber.Text = "";
    }
}