using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.OracleClient;

public partial class Business_ATM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadBranches();
            loadATMTypes();
        }
    }

    private void loadBranches()
    {
        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 = "LOOKUP_GETTER.Get_Branches";

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

            oracom.Parameters["p_lang"].Value = "EN";

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

            ddlChannels.DataSource = ds;
            ddlChannels.DataBind();
            DropDownList1.DataSource = ds;
            DropDownList1.DataBind();

            ddlChannels.Items.Insert(0, "------ Branches ------");
            oracon.Close();
            ExceptionUtility.LogException(0, "LOOKUP_GETTER.Get_Branches","", "loadBranches", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Branches","", "loadBranches", "");
        }
    }

    private void loadATMTypes()
    {
        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 = "LOOKUP_GETTER.Get_ATM_Types";

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

            oracom.Parameters["p_lang"].Value = "EN";

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

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

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


            ddlType.Items.Insert(0, "------ Types ------");
            oracon.Close();
            ExceptionUtility.LogException(0, "LOOKUP_GETTER.Get_ATM_Types","", "loadATMTypes", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_ATM_Types","", "loadATMTypes", "");
        }
    }

    private void loadResult(string branchID, string terminalID, string type, int pageSize, int pageNumber)
    {
        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 = "ATM_INFO_EDITOR.Get_ATM_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_atm_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_type", 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_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.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

            if (branchID.Length != 0)
            {
                parameters = ":p_branch_code-" + branchID;
                oracom.Parameters["p_branch_code"].Value = branchID;
            }
            else
                oracom.Parameters["p_branch_code"].Value = OracleString.Null;
            if (terminalID.Length != 0)
            {
                parameters = parameters + ":p_atm_number-" + terminalID;
                oracom.Parameters["p_atm_number"].Value = terminalID;
            }
            else
                oracom.Parameters["p_atm_number"].Value = OracleString.Null;
            if (type.Length != 0)
            {
                parameters = parameters + ":p_atm_type-" + type;
                oracom.Parameters["p_atm_type"].Value = type;
            }
            else
                oracom.Parameters["p_atm_type"].Value = OracleString.Null;
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            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);

            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());
                }
            }
            ExceptionUtility.LogException(0, "ATM_INFO_EDITOR.Get_ATM_Info","", "loadResult", parameters );//"branchID: " + branchID + ", terminalID: " + terminalID + ", type: " + type + ", pageSize: " + pageSize + ", pageNumber: " + pageNumber);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_INFO_EDITOR.Get_ATM_Info","", "loadResult", parameters );//"branchID: "+branchID +", terminalID: "+terminalID +", type: "+type+", pageSize: "+pageSize +", pageNumber: "+pageNumber );
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        getATMS();
    }

    private void getATMS()
    {
        string channelID = "", EventID = "", type = "";
        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        EventID = txtATMnumber.Text;
        if (ddlType.SelectedIndex > 0)
            type = ddlType.SelectedValue;
        pnlResult.Visible = true;
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
        loadResult(channelID, EventID, type, Helper.gridSize(), 1);
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        string channelID = "", EventID = "", type = "";
        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        EventID = txtATMnumber.Text;
        if (ddlType.SelectedIndex > 0)
            type = ddlType.SelectedValue;
        loadResult(channelID, EventID, type, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DropDownList1.Enabled = true;
        DropDownList3.Enabled = true;
        txtTerminalID.Enabled = true;

        pnlControls.Visible = true;
        pnlResult.Enabled = false;
        ViewState["mode"] = "a";
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string terminalID = GridView1.Rows[index].Cells[2].Text.ToString();
            string branchID = GridView1.Rows[index].Cells[0].Text.ToString();
            string typeID = GridView1.Rows[index].Cells[3].Text.ToString();

            string overage = GridView1.Rows[index].Cells[5].Text.ToString();
            string shortage = GridView1.Rows[index].Cells[6].Text.ToString();
            string cash = GridView1.Rows[index].Cells[7].Text.ToString();

            string overageDep = GridView1.Rows[index].Cells[8].Text.ToString();
            string shortageDep = GridView1.Rows[index].Cells[9].Text.ToString();
            string cashDep = GridView1.Rows[index].Cells[10].Text.ToString();

            string mUser = GridView1.Rows[index].Cells[11].Text.ToString();
            string mDate = GridView1.Rows[index].Cells[12].Text.ToString();

            DropDownList1.SelectedValue = branchID;
            txtTerminalID.Text = terminalID;
            txtTerminalID.Enabled = false;
            DropDownList3.SelectedValue = typeID;

            if (!overage.Equals("&nbsp;"))
                TextBox1.Text = overage;
            else
                TextBox1.Text = "";
            if (!shortage.Equals("&nbsp;"))
                TextBox2.Text = shortage;
            else
                TextBox2.Text = "";
            if (!cash.Equals("&nbsp;"))
                TextBox3.Text = cash;
            else
                TextBox3.Text = "";

            if (!overageDep.Equals("&nbsp;"))
                TextBox4.Text = overageDep;
            else
                TextBox4.Text = "";
            if (!shortageDep.Equals("&nbsp;"))
                TextBox5.Text = shortageDep;
            else
                TextBox5.Text = "";
            if (!cashDep.Equals("&nbsp;"))
                TextBox6.Text = cashDep;
            else
                TextBox6.Text = "";

            if (!mUser.Equals("&nbsp;"))
                lblUser.Text = mUser;
            else
                lblUser.Text = "";
            if (!mDate.Equals("&nbsp;"))
                lblDate.Text = mDate;
            else
                lblDate.Text = "";

            pnlControls.Visible = true;
            pnlResult.Enabled = false;

            ViewState["mode"] = "u";
            ViewState["rowid"] = rowID;
        }
        else if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool saved = deleteATM(rowID, ref error);
            if (saved)
            {
                lblError.Text = "ATM deleted";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Delete Failed";
                    ExceptionUtility.LogException(9, "", "", "GridView1_RowCommand.Delete", error);
                }
                else
                    lblError.Text = error;
            }
            getATMS();
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                bool saved = addATM(ref error);
                if (saved)
                {
                    lblError.Text = "ATM data Saved";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "Save Failed";
                        ExceptionUtility.LogException(9, "", "", "btnSave", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["rowid"].ToString();
                bool saved = updateATM(Uid, ref error);
                if (saved)
                {
                    lblError.Text = "ATM data Saved";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "Save Failed";
                        ExceptionUtility.LogException(9, "", "", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            getATMS();
            ViewState["mode"] = null;
            ViewState["rowid"] = null;
            pnlControls.Visible = false;
            pnlResult.Enabled = true;
        }
        catch (Exception ex)
        {
        }
    }

    private bool addATM(ref string error)
    {
        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 = "ATM_INFO_EDITOR.Add_ATM";

            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_terminal_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_overage_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_shortage_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_cash_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_overage_dep_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_shortage_dep_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_cash_dep_acct", 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 = DropDownList1.SelectedValue;
            oracom.Parameters["p_terminal_id"].Value = txtTerminalID.Text;
            oracom.Parameters["p_atm_number"].Value = txtTerminalID.Text.Substring(txtTerminalID.Text.Length - 4);
            oracom.Parameters["p_atm_type"].Value = DropDownList3.SelectedValue;

            oracom.Parameters["p_overage_acct"].Value = TextBox1.Text;
            oracom.Parameters["p_shortage_acct"].Value = TextBox2.Text;
            oracom.Parameters["p_cash_acct"].Value = TextBox3.Text;

            oracom.Parameters["p_overage_dep_acct"].Value = TextBox4.Text;
            oracom.Parameters["p_shortage_dep_acct"].Value = TextBox5.Text;
            oracom.Parameters["p_cash_dep_acct"].Value = TextBox6.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")
            {
                ExceptionUtility.LogException(0, "ATM_INFO_EDITOR.Add_ATM", "","addATM", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "ATM_INFO_EDITOR.Add_ATM","", "addATM", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_INFO_EDITOR.Add_ATM","", "addATM", "");
            return false;
        }
    }

    private bool updateATM(string rowID, ref string error)
    {
        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 = "ATM_INFO_EDITOR.update_atm_info";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_overage_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_shortage_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_cash_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_overage_dep_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_shortage_dep_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_cash_dep_acct", 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_atm_number"].Value = rowID;
            oracom.Parameters["p_branch_code"].Value = DropDownList1.SelectedValue;
            oracom.Parameters["p_atm_type"].Value = DropDownList3.SelectedValue;

            oracom.Parameters["p_overage_acct"].Value = TextBox1.Text;
            oracom.Parameters["p_shortage_acct"].Value = TextBox2.Text;
            oracom.Parameters["p_cash_acct"].Value = TextBox3.Text;

            oracom.Parameters["p_overage_dep_acct"].Value = TextBox4.Text;
            oracom.Parameters["p_shortage_dep_acct"].Value = TextBox5.Text;
            oracom.Parameters["p_cash_dep_acct"].Value = TextBox6.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")
            {
                ExceptionUtility.LogException(0, "ATM_INFO_EDITOR.Add_ATM","", "updateATM", "rowID: " + rowID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "ATM_INFO_EDITOR.Add_ATM","", "updateATM", "rowID: " + rowID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_INFO_EDITOR.Add_ATM","", "updateATM", "rowID: "+rowID );
            return false;
        }
    }

    private bool deleteATM(string atmID, ref string error)
    {
        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 = "ATM_INFO_EDITOR.Delete_ATM";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_number", 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_atm_number"].Value = atmID;

            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")
            {
                ExceptionUtility.LogException(0, "ATM_INFO_EDITOR.Delete_ATM","", "deleteATM", "atmID: " + atmID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "ATM_INFO_EDITOR.Delete_ATM","", "deleteATM", "atmID: " + atmID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_INFO_EDITOR.Delete_ATM","", "deleteATM", "atmID: "+atmID );
            return false;
        }
    }

    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[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
        }
    }
}
