using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;

public partial class Business_ManageServiceProviders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            pnlControls.Visible = false;
            pnlGrid.Visible = false;

            lblError.Text = "";
            pnlGrid.Visible = true;
            pnlControls.Visible = false;
            getPrepaidInfo(Helper.gridSize(), 1);
           
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        pnlGrid.Visible = true;
        pnlControls.Visible = false;
        getPrepaidInfo(Helper.gridSize(), 1);
    }
    private void getPrepaidInfo(int pageSize, int pageNumber)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "POS_SRV_PRVDR_EDITOR.Get_Providers";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            //oracom.Parameters["p_atm_s_code"].Value = OracleNumber.Null;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            if (ex.Message.Equals("ORA-24338: statement handle not executed\n"))
            {
                //this user id already exists...
                //setMessage("No Status available");
                //lblError.Text = "No Branch Available";
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string v_code = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            //pnlSearch.Visible = false;
            pnlGrid.Enabled = false;

            ViewState["v_code"] = v_code;
            //put the data in the controls
            if (GridView1.Rows[index].Cells[1].Text.Contains("&nbsp;"))
                txtVendorName.Text = "";
            else
                txtVendorName.Text = GridView1.Rows[index].Cells[1].Text;
            if (GridView1.Rows[index].Cells[2].Text.Contains("&nbsp;"))
                txtVendorEmail.Text = "";
            else
                txtVendorEmail.Text = GridView1.Rows[index].Cells[2].Text;

            txtVendorName.ReadOnly = true;

        }

    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Cells[4].Visible = false;

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            int page = 1;
            if (ddlPages.SelectedIndex > 0)
                page = int.Parse (ddlPages.SelectedValue);
            if (mode.Equals("a"))
            {
               
                bool saved = addPrepaid( ref error);
                if (saved)
                {
                    lblError.Text = "Provider email Saved";                 
                    //getPrepaidInfo(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
                    getPrepaidInfo(Helper.gridSize(), page);
                    
                }
                else
                {

                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "Save Failed";
                        ExceptionUtility.LogException(9, "", "", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                int v_code = Convert.ToInt16(ViewState["v_code"]);


                bool saved = updatePrepaid(ref error);
                if (saved)
                {
                    lblError.Text = "Provider email Saved";
                    //getPrepaidInfo(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
                    getPrepaidInfo(Helper.gridSize(), page);
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "Save Failed";
                        ExceptionUtility.LogException(9, "", "", "btnSave_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }

            ViewState["mode"] = null;
            ViewState["v_code"] = null;
            pnlControls.Visible = false;
            pnlGrid.Visible = true;
            pnlGrid.Enabled = true;
            //pnlSearch.Visible = true;
            clearControls();
        }
        catch (Exception ex)
        {
            if (ex.Message.Equals("ORA-00001: unique constraint (NCB.SEC_USER_UK) violated\n"))
            {
                //this user id already exists...
                setMessage("This Prepaid Card already exists");
            }
        }

    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        getPrepaidInfo(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "a";
        txtVendorName.ReadOnly = false;
        txtVendorEmail.ReadOnly = false;
        pnlControls.Visible = true;
        pnlGrid.Enabled = false;

        clearControls();
    }
    private bool addPrepaid(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 = "POS_SRV_PRVDR_EDITOR.Add_Provider";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_prvdr_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_prvdr_email", 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_prvdr_name"].Value = txtVendorName.Text.Trim();
        oracom.Parameters["p_prvdr_email"].Value = txtVendorEmail.Text.Trim();
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

    private bool updatePrepaid(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 = "POS_SRV_PRVDR_EDITOR.Update_Provider";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_prvdr_cd", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_prvdr_email", 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_prvdr_cd"].Value = ViewState["v_code"];
        oracom.Parameters["p_prvdr_email"].Value = txtVendorEmail.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")
            return true;
        else
            return false;
    }

    private void clearControls()
    {
        txtVendorEmail.Text = "";
        txtVendorName.Text = "";
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = null;
        ViewState["cardProductType"] = null;
        //pnlSearch.Visible = true;
        pnlControls.Visible = false;
        pnlGrid.Enabled = true;
        lblError.Text = "";
        clearControls();
    }
}