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)
        {
            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();
        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));
    }

    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;
        }
        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;
        }
        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;
        }
    }

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

        if (mode.Equals("a"))
        {
            bool saved = add_merchant(ref error);
            if (saved)
            {
                lblError.Text = "Merchant saved";
            }
            else
            {
                lblError.Text = error;
            }
        }
        else if (mode.Equals("u"))
        {
            string rowID = ViewState["rowid"].ToString();
            bool saved = update_merchant(rowID, ref error);
            if (saved)
            {
                lblError.Text = "Merchant saved";
            }
            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;
    }

    private void loadMerchants(string retailer_id, int pageSize, int pageNumber)
    {
        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;
        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());
            }
        }
    }

    private bool add_merchant(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 = "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")
            return true;
        else
        return false;
    }

    private bool update_merchant(string ID, 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 = "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")
            return true;
        else
        return false;
    }

    //Terminals
    private void loadTerminals(string clientNumber)
    {
        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();
    }

    private bool addTerminal(string clientNumber, 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 = "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")
            return true;
        else
            return false;
    }

    private bool deleteTerminal(string terminalID, 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 = "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")
            return true;
        else
            return false;
    }

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

    protected void btnSaveTerminal_Click(object sender, EventArgs e)
    {
        string error = "";
        string rowID = ViewState["rowid"].ToString();
        bool saved = addTerminal(rowID, ref error);
        if (saved)
        {
            lblError.Text = "Terminal saved";
        }
        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;
    }

    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;

            //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;

            loadServices(int.Parse(ViewState["rowid"].ToString()));

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

    //services
    private void loadServices()
    {
        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();
    }

    private void loadServices(int clientNumber)
    {
        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();
    }

    private bool addService(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 = "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_merchant_fee", 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 = ViewState["rowid"].ToString();
        oracom.Parameters["p_service_id"].Value = ddlServices.SelectedValue;
        oracom.Parameters["p_acct_no"].Value = txtAccountNoService.Text;
        oracom.Parameters["p_merchant_fee"].Value = Convert.ToDecimal(txtMerchantFee.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")
            return true;
        else
            return false;
    }

    private bool deleteService(string serviceID, int clientNumber,  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 = "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")
            return true;
        else
            return false;
    }

    protected void btnCancelService_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        txtMerchantFee.Text = "";
        txtAccountNoService.Text = "";
    }

    protected void btnSaveService_Click(object sender, EventArgs e)
    {
        string error = "";
        string rowID = ViewState["termid"].ToString();
        bool saved = addService(ref error);
        if (saved)
        {
            lblError.Text = "Service saved";
        }
        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)
    {
        pnlControlServices.Visible = true;
        //pnlResult.Enabled = false;
        txtAccountNoService.Text = "";
        txtMerchantFee.Text = "";
    }

    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()));
        }
    }
    
    protected void btn_del_service_conf_ok_Click(object sender, EventArgs e)
    {
        string error = "";

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

        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;
    }
    
    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;
    }
    
    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;
    }


    /*IJR 11-1-2012 
     Delete Merchant Data*/
    private bool DeleteMerchant(string clientNumber, 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 = "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")
            return true;
        else
            return false;
    }



    protected void btn_del_Merch_conf_cancel_Click(object sender, EventArgs e)
    {
        pnl_del_Merchant_confirmation.Visible = false;
    }
    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;
    }
}
