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 merchantMain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ExceptionUtility.LogException(0, "", "78", "Page_Load", "");
            txtFeePercent.Attributes.Add("onKeyPress", "return KeyPressFuncIsNumericWithDot();");
            loadMerchants(txtRetailerID.Text, Helper.gridSize(), 1);
            loadServices();

            this.pnl_del_service_confirmation.Visible = false;
            this.pnl_del_terminal_confirmation.Visible = false;
            this.pnl_del_Merchant_confirmation.Visible = false;
            
        }

        this.lblError.Text = "";
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtRetailerID.Text = txtRetailerID.Text.Trim();
        ExceptionUtility.LogException(0, "", "78.448", "Button1_Click", "");
        loadMerchants(txtRetailerID.Text, Helper.gridSize(), 1);

        pnlResult.Enabled = true;
        pnlControls.Visible = false;
        pnlTerminals.Visible = false;
        pnlControlTerminal.Visible = false;
        pnlServices.Visible = false;
        pnlControlServices.Visible = false;
        
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtRetailerID.Text = txtRetailerID.Text.Trim();
        loadMerchants(txtRetailerID.Text, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            pnlControls.Visible = true;
            ViewState["rowid"] = rowID;
            ViewState["mode"] = "u";

            txtRetailer.Text = GridView1.Rows[index].Cells[0].Text.ToString();
            txtMerchantName.Text = GridView1.Rows[index].Cells[1].Text.ToString();
            //txtAccNo.Text = GridView1.Rows[index].Cells[2].Text.ToString();
            txtAccNo.Visible = false;
            txtAccNo.Text = null;

            pnlTerminals.Visible = false;
            pnlControlTerminal.Visible = false;
            pnlServices.Visible = false;
            pnlControlServices.Visible = false;
            ExceptionUtility.LogException(0, "", "78.449", "GridView1_RowCommand.update", "");
        }
        else if (e.CommandName.Equals("vt"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            pnlTerminals.Visible = true;
            ViewState["rowid"] = rowID;
            ViewState["mode"] = "vt";

            loadTerminals(rowID);

            pnlControls.Visible = false;
            pnlControlTerminal.Visible = false;
            pnlServices.Visible = false;
            pnlControlServices.Visible = false;
            ExceptionUtility.LogException(0, "", "78.450", "GridView1_RowCommand.ViewTerminals", "");
        }
        else if (e.CommandName.Equals("d"))
        {
            //Delete Merchant
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            ViewState["rowid"] = rowID;
            ViewState["mode"] = "d";

            ViewState["selectedMerchID"] = rowID;
            this.pnl_del_Merchant_confirmation.Visible = true;
            ExceptionUtility.LogException(0, "", "78.451", "GridView1_RowCommand.delete", "");
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string mode = ViewState["mode"].ToString();
        string error = "";

        if (mode.Equals("a"))
        {
            ExceptionUtility.LogException(0, "", "78.453", "btnSave_Click", "");
            bool saved = add_merchant(ref error);
            if (saved)
            {
                lblError.Text = "Merchant saved";
                
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Reset Failed";
                    ExceptionUtility.LogException(9, "", "", "btnSave_Click", error);
                }
                else
                    lblError.Text = error;
            }
        }
        else if (mode.Equals("u"))
        {
            string rowID = ViewState["rowid"].ToString();
            ExceptionUtility.LogException(0, "", "78.453", "btnSave_Click", "");
            bool saved = update_merchant(rowID, ref error);
            if (saved)
            {
                lblError.Text = "Merchant saved";
               
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Reset Failed";
                    ExceptionUtility.LogException(9, "", "78.453", "btnSave_Click", error);
                }
                else
                    lblError.Text = error;
               
            }
        }

        //loading the grid again
        loadMerchants(txtRetailerID.Text, Helper.gridSize(), 1);
        pnlControls.Visible = false;


    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = true;
        //pnlResult.Enabled = false;
        ViewState["mode"] = "a";

        txtAccNo.Text = "";
        txtMerchantName.Text = "";
        txtRetailer.Text = "";
        //pnlResult.Enabled = false;
        pnlTerminals.Visible = false;
        pnlControlTerminal.Visible = false;
        pnlServices.Visible = false;
        pnlControlServices.Visible = false;
        ExceptionUtility.LogException(0, "", "78.452", "btnAdd_Click", "");
    }

    private void loadMerchants(string retailer_id, 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 = "merchant_reg_main.get_merchants";


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



            oracom.Parameters["p_retailer_id"].Value = txtRetailerID.Text;
            if (txtRetailerID.Text.Length > 0)
                parameters = ":p_retailer_id" + txtRetailerID.Text+":p_lang-EN";
            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, "merchant_reg_main.get_merchants", "1.1", "loadMerchants", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.get_merchants","1.1", "loadMerchants", parameters  );
            GridView1.DataSource = null;
            GridView1.DataBind();
        }

    }

    private bool add_merchant(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 = "merchant_reg_main.add_merchant";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_retailer_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_retailer_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_account_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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.Parameters["p_retailer_name"].Value = txtMerchantName.Text;
            oracom.Parameters["p_retailer_id"].Value = txtRetailer.Text;
            oracom.Parameters["p_account_number"].Value = txtAccNo.Text;

            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, "merchant_reg_main.add_merchant", "453.1", "add_merchant", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.add_merchant", "453.1", "add_merchant", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(0, "merchant_reg_main.add_merchant", "453.1", "add_merchant", "");
            return false;
        }
    }

    private bool update_merchant(string ID, 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 = "merchant_reg_main.update_merchant";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_client_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_retailer_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_retailer_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_account_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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.Parameters["p_client_number"].Value = ID;
            oracom.Parameters["p_retailer_name"].Value = txtMerchantName.Text;
            oracom.Parameters["p_retailer_id"].Value = txtRetailer.Text;
            //oracom.Parameters["p_account_number"].Value = txtAccNo.Text;
            oracom.Parameters["p_account_number"].Value = OracleString.Null;

            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, "merchant_reg_main.update_merchant","453.1", "update_merchant", "ID: " + ID+", p_retailer_name: "+txtMerchantName .Text +", p_retailer_id: "+txtRetailer .Text );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.update_merchant", "453.1", "update_merchant", "ID: " + ID + ", p_retailer_name: " + txtMerchantName.Text + ", p_retailer_id: " + txtRetailer.Text);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex , "merchant_reg_main.update_merchant", "453.1", "update_merchant", "ID: " + ID + ", p_retailer_name: " + txtMerchantName.Text + ", p_retailer_id: " + txtRetailer.Text);
            return false;
        }
    }
    private bool update_service(string ID, 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 = "merchant_reg_main.update_Service";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_client_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_service_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fee_percent", 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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_client_num"].Value = int.Parse (ViewState["rowid"].ToString());
            oracom.Parameters["p_service_id"].Value = ID;
            oracom.Parameters["p_fee_percent"].Value = txtFeePercent .Text ;
           

            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, "merchant_reg_main.update_service", "506.1", "update_service", " p_client_num: " + int.Parse(ViewState["rowid"].ToString()) + ", p_service_id: " + ID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.update_service", "506.1", "update_service", " p_client_num: " + int.Parse(ViewState["rowid"].ToString()) + ", p_service_id: " + ID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.update_merchant", "506.1", "update_merchant", "p_client_num: " + int.Parse(ViewState["rowid"].ToString()) + ", p_service_id: " + ID);
            return false;
        }
    }
    //Terminals
    private void loadTerminals(string clientNumber)
    {
        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 = "merchant_reg_main.get_TERMINALs";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_client_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.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;



            oracom.Parameters["p_client_number"].Value = clientNumber;
            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();

            gvTerminals.DataSource = ds;
            gvTerminals.DataBind();
            ExceptionUtility.LogException(0, "merchant_reg_main.get_TERMINALs", "78.450", "loadTerminals", "clientNumber: " + clientNumber);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.get_TERMINALs", "78.450", "loadTerminals", "clientNumber: " + clientNumber);
            gvTerminals.DataSource = null;
            gvTerminals.DataBind();
        }
    }

    private bool addTerminal(string clientNumber, 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 = "merchant_reg_main.add_Terminal";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_client_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_Terminal_id", 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();

            oracom.Parameters["p_client_number"].Value = clientNumber;
            oracom.Parameters["p_Terminal_id"].Value = txtTerminalID.Text;

            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, "merchant_reg_main.add_Terminal", "457.1", "addTerminal", "clientNumber: " + clientNumber+", p_terminal_id: "+txtTerminalID .Text );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(0, "merchant_reg_main.add_Terminal", "457.1", "addTerminal", "clientNumber: " + clientNumber + ", p_terminal_id: " + txtTerminalID.Text);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex , "merchant_reg_main.add_Terminal", "457.1", "addTerminal", "clientNumber: " + clientNumber + ", p_terminal_id: " + txtTerminalID.Text);
            return false;
        }
    }

    private bool deleteTerminal(string terminalID, 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 = "merchant_reg_main.delete_Terminal";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_Terminal_id", 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();

            oracom.Parameters["p_Terminal_id"].Value = terminalID;

            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, "merchant_reg_main.delete_Terminal", "78.454", "deleteTerminal", "terminalID: " + terminalID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(0, "merchant_reg_main.delete_Terminal", "78.454", "deleteTerminal", "terminalID: " + terminalID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.delete_Terminal", "78.454", "deleteTerminal", "terminalID: " + terminalID);
            return false;
        }
    }

    protected void btnCancelTerminal_Click(object sender, EventArgs e)
    {
        pnlControlTerminal.Visible = false;
        pnlResult.Enabled = true;
        ExceptionUtility.LogException(0, "", "", "btnCancelTerminal_Click", "");
    }

    protected void btnSaveTerminal_Click(object sender, EventArgs e)
    {
        string error = "";
        string rowID = ViewState["rowid"].ToString();
        ExceptionUtility.LogException(0, "", "78.457", "btnSaveTerminal_Click", "");
        bool saved = addTerminal(rowID, ref error);
        if (saved)
        {
            
            lblError.Text = "Terminal saved";
        }
        else
        {
            if (Helper.OracleError(error))
            {
                lblError.Text = "Save Failed";
                ExceptionUtility.LogException(9, "", "78.453", "btnSave_Click", error);
            }
            else
                lblError.Text = error;
             
        }


        //loading the grid again
        loadTerminals(rowID);
        pnlControlTerminal.Visible = false;
    }

    protected void btnAddTerminal_Click(object sender, EventArgs e)
    {
        pnlControlTerminal.Visible = true;
        //pnlResult.Enabled = false;
        txtTerminalID.Text = "";

        pnlServices.Visible = false;
        pnlControlServices.Visible = false;
        ExceptionUtility.LogException(0, "", "78.456", "btnAddTerminal_Click", "");
    }

    protected void gvTerminals_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("d"))
        {
            //string error = "";
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvTerminals.DataKeys[index].Value.ToString();

            ViewState["selectedTerminalID"] = rowID;

            this.pnl_del_terminal_confirmation.Visible = true;
            this.pnlResult.Visible = false;
            this.pnl_filters.Visible = false;
            this.pnlTerminals.Visible = false;
            this.pnlServices.Visible = false;
            ExceptionUtility.LogException(0, "", "78.454", "gvTerminals_RowCommand.delete", "");
            //deleteTerminal(rowID, ref error);
            //loadTerminals(ViewState["rowid"].ToString());

            //pnlControlTerminal.Visible = false;
            //pnlServices.Visible = false;
            //pnlControlServices.Visible = false;
        }
        else if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvTerminals.DataKeys[index].Value.ToString();
            pnlServices.Visible = true;
            ViewState["termid"] = rowID;
            ExceptionUtility.LogException(0, "", "78.455", "gvTerminals_RowCommand.View", "");
            loadServices(int.Parse(ViewState["rowid"].ToString()));

            pnlControlTerminal.Visible = false;
            
            pnlControlServices.Visible = false;
            
        }
    }

    //services
    private void loadServices()
    {
        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 = "poslookup_getter.Get_MerServices";

            oracom.Parameters.Add("p_ServiceName", OracleType.Cursor).Direction = ParameterDirection.Output;

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

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

            oracon.Close();
            //ExceptionUtility.LogException(0, "poslookup_getter.Get_MerServices","", "loadServices", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "poslookup_getter.Get_MerServices","", "loadServices", "");
            
        }
    }

    private void loadServices(int clientNumber)
    {
        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 = "merchant_reg_main.get_services";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_client_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["p_client_num"].Value = clientNumber;
            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();

            gvServices.DataSource = ds;
            gvServices.DataBind();
            ExceptionUtility.LogException(0, "merchant_reg_main.get_services", "455.1", "loadServices", "clientNumber: " + clientNumber);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.get_services", "455.1", "loadServices", "clientNumber: " + clientNumber);
            gvServices.DataSource = null ;
            gvServices.DataBind();
        }
    }

    private bool addService(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 = "merchant_reg_main.ADD_Service";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_client_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_service_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_acct_no", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_fee_percent", 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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            parameters = "p_lang-EN:p_client_num-" + ViewState["rowid"] + ":";
            oracom.Parameters["p_client_num"].Value = ViewState["rowid"].ToString();
            oracom.Parameters["p_service_id"].Value = ddlServices.SelectedValue;
            if (ddlServices.SelectedIndex > 0)
                parameters = parameters + "p_service_id-" + ddlServices.SelectedItem.Text;
            oracom.Parameters["p_acct_no"].Value = txtAccountNoService.Text;
            if (txtAccountNoService.Text.Length > 0)
                parameters = parameters + "p_acct_no-" + txtAccountNoService.Text + ":";
            oracom.Parameters["p_fee_percent"].Value = txtFeePercent .Text;
            if (txtFeePercent.Text.Length > 0)
                parameters = parameters + "p_fee_percent-" + txtFeePercent.Text + ":";
            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, "merchant_reg_main.ADD_Service", "460.1", "addService", parameters  );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.ADD_Service", "460.1", "addService", parameters );
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex , "merchant_reg_main.ADD_Service", "460.1", "addService", parameters );
            return false;
        }
    }

    private bool deleteService(string serviceID, int clientNumber,  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 = "merchant_reg_main.delete_Service";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_client_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_service_id", 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();

            oracom.Parameters["p_client_num"].Value = clientNumber;
            oracom.Parameters["p_service_id"].Value = serviceID;

            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, "merchant_reg_main.delete_Service", "78.458", "deleteService", "serviceID: " + serviceID + ", clientNumber: " + clientNumber);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.delete_Service", "78.458", "deleteService", "serviceID: " + serviceID + ", clientNumber: " + clientNumber);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex , "merchant_reg_main.delete_Service", "78.458", "deleteService", "serviceID: " + serviceID + ", clientNumber: " + clientNumber);
            return false;
        }
    }

    protected void btnCancelService_Click(object sender, EventArgs e)
    {
        pnlControlServices.Visible = false;
        txtFeePercent.Text = "";
        txtAccountNoService.Text = "";
        ExceptionUtility.LogException(0, "", "", "btnCancelService_Click", "");
    }

    protected void btnSaveService_Click(object sender, EventArgs e)
    {
        string mode = ViewState["mode"].ToString();
        string error = "";
        string rowID = ViewState["termid"].ToString();
        ExceptionUtility.LogException(0, "", "78.460", "btnSaveService_Click", "");
        if (mode.Equals ("a"))
        {
            bool saved = addService(ref error);
            if (saved)
            {

                lblError.Text = "Service saved";
            }
            else
            {
                
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Save Failed";
                    ExceptionUtility.LogException(9, "", "78.460", "btnSaveService_Click", error);
                }
                else
                    lblError.Text = error;
            }
        }
        else if (mode.Equals("u"))
        {
            string serviceID = ViewState["rowID"].ToString();
            ExceptionUtility.LogException(0, "", "78.453", "btnSave_Click", "");
            bool saved = update_service(serviceID, ref error);
            if (saved)
            {
                lblError.Text = "Service saved";

            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Save Failed";
                    ExceptionUtility.LogException(9, "", "78.453", "btnSave_Click", error);
                }
                else
                    lblError.Text = error;
                
            }
        }
        //loading the grid again
        loadServices(int.Parse(ViewState["rowid"].ToString()));
        pnlControlServices.Visible = false;
    }

    protected void btnAddService_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "a";
        txtAccountNoService.ReadOnly = false;
        pnlControlServices.Visible = true;
        //pnlResult.Enabled = false;
        txtAccountNoService.Text = "";
        txtFeePercent.Text = "";
        ExceptionUtility.LogException(0, "", "78.459", "btnAddService_Click", "");
    }

    protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("d"))
        {
            //string error = "";
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();

            ViewState["selectedServiceID"] = rowID;

            this.pnl_del_service_confirmation.Visible = true;
            this.pnlResult.Visible = false;
            this.pnl_filters.Visible = false;
            this.pnlTerminals.Visible = false;
            this.pnlServices.Visible = false;

            //deleteService(rowID, int.Parse(ViewState["rowid"].ToString()), ref error);
            //loadServices(int.Parse(ViewState["rowid"].ToString()));
            ExceptionUtility.LogException(0, "", "78.458", "gvServices_RowCommand.delete", "");
        }
        if(e.CommandName .Equals ("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();
            ViewState["rowID"] = rowID;
            ViewState["mode"] = "u";
            pnlControlServices.Visible = true;
            txtAccountNoService.ReadOnly = true;
            ddlServices .SelectedItem .Text  = gvServices.Rows[index].Cells[0].Text.ToString();
            txtAccountNoService.Text = gvServices.Rows[index].Cells[1].Text.ToString();
            txtFeePercent.Text = gvServices.Rows[index].Cells[2].Text.ToString().Replace("&nbsp;","");
            ExceptionUtility.LogException(0, "", "78.506", "gvServices_RowCommand.update", "");
        }
    }
    
    protected void btn_del_service_conf_ok_Click(object sender, EventArgs e)
    {
        string error = "";

        string rowID = ViewState["selectedServiceID"].ToString();
        ExceptionUtility.LogException(0, "", "", "btn_del_service_conf_ok_Click", "");
        deleteService(rowID, int.Parse(ViewState["rowid"].ToString()), ref error);
        loadServices(int.Parse(ViewState["rowid"].ToString()));

        this.pnl_del_service_confirmation.Visible = false;
        this.pnlResult.Visible = true;
        this.pnl_filters.Visible = true;
        this.pnlTerminals.Visible = true;
        this.pnlServices.Visible = true;
        
    }
    
    protected void btn_del_service_conf_cancel_Click(object sender, EventArgs e)
    {
        this.pnl_del_service_confirmation.Visible = false;
        this.pnlResult.Visible = true;
        this.pnl_filters.Visible = true;
        this.pnlTerminals.Visible = true;
        this.pnlServices.Visible = true;
        ExceptionUtility.LogException(0, "", "", "btn_del_service_conf_cancel_Click", "");
    }
    
    protected void btn_del_terminal_conf_cancel_Click(object sender, EventArgs e)
    {
        this.pnl_del_terminal_confirmation.Visible = false;
        this.pnlResult.Visible = true;
        this.pnl_filters.Visible = true;
        this.pnlTerminals.Visible = true;
        ExceptionUtility.LogException(0, "", "", "btn_del_terminal_conf_cancel_Click", "");
    }
    
    protected void btn_del_terminal_conf_ok_Click(object sender, EventArgs e)
    {
        string error = "";

        string rowID = ViewState["selectedTerminalID"].ToString();

        deleteTerminal(rowID, ref error);
        loadTerminals(ViewState["rowid"].ToString());

        this.pnl_del_terminal_confirmation.Visible = false;
        this.pnlResult.Visible = true;
        this.pnl_filters.Visible = true;
        this.pnlTerminals.Visible = true;
        ExceptionUtility.LogException(0, "", "", "btn_del_terminal_conf_ok_Click", "");
    }


    /*IJR 11-1-2012 
     Delete Merchant Data*/
    private bool DeleteMerchant(string clientNumber, 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 = "merchant_reg_main.delete_Merchant";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_client_num", 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();

            oracom.Parameters["p_client_num"].Value = clientNumber;
            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, "merchant_reg_main.delete_Merchant", "78.451", "DeleteMerchant", "clientNumber: " + clientNumber);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "merchant_reg_main.delete_Merchant", "78.451", "DeleteMerchant", "clientNumber: " + clientNumber);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "merchant_reg_main.delete_Merchant", "78.451", "DeleteMerchant", "clientNumber: " + clientNumber);
            return false;
        }
    }



    protected void btn_del_Merch_conf_cancel_Click(object sender, EventArgs e)
    {
        pnl_del_Merchant_confirmation.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btn_del_Merch_conf_cancel_Click", "");
    }
    protected void btn_del_Merch_conf_ok_Click(object sender, EventArgs e)
    {
        string error = "";
        string rowID = ViewState["selectedMerchID"].ToString();
        DeleteMerchant(rowID, ref error);
        lblError.Text = error;
        loadMerchants(txtRetailerID.Text, Helper.gridSize(), 1);
        loadServices();
        this.pnl_del_Merchant_confirmation.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btn_del_Merch_conf_ok_Click", "");
    }
}
