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 UpdateVisaBilling : System.Web.UI.Page
{
    public string pid, response;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlUpdate.Visible = false;
            loadChannel();
           
        }
        lblError.Text = "";
    }

    private void loadChannel()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "CS_SETTL_INFO_EDITOR.Get_VB_CHNL_NAME";

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

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

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

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

        oracon.Close();
    }

    private void Get_VB_ChannelKeys( 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.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "CS_SETTL_INFO_EDITOR.Get_VB_CHNL_KEYS";

        oracom.Parameters.Add("p_CHANNEL_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_KEYS", 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["p_CHANNEL_NAME"].Value = ddlchname.SelectedValue;
        oracom.Parameters["p_CHANNEL_KEYS"].Value = txtchkeys.Text;

        oracom.Parameters["p_page_size"].Value = pageSize;
        oracom.Parameters["p_page_num"].Value = pageNumber;
        oracom.Parameters["p_lang"].Value = "EN";
        
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        int  nop = 0;
        if(ds.Tables[0].Rows.Count>1)
        nop = int.Parse(Math.Ceiling((double.Parse(ds.Tables[0].Rows[0]["ROW_COUNT"].ToString()) / pageSize)).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 void Get_VB_ChannelKeys_ID()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "CS_SETTL_INFO_EDITOR.GET_VB_CHNL_KEYS_ID";

        oracom.Parameters.Add("p_VB_CHNL_CODE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_KEYS", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_TYPE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_ID", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("P_RSP", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

        oracom.Parameters["p_VB_CHNL_CODE"].Value = txtHiddenChannelCode.Text;
        oracom.Parameters["p_CHANNEL_NAME"].Value = txtHiddenchname.Text;
        oracom.Parameters["p_CHANNEL_KEYS"].Value = txtHiddenchkeys.Text;
        oracom.Parameters["p_CHANNEL_TYPE"].Value = txtHiddenBillType.Text;

        oracom.ExecuteNonQuery();
        oracon.Close();

        pid = oracom.Parameters["P_ID"].Value.ToString();
        response = oracom.Parameters["P_RSP"].Value.ToString();
    }

    private bool Update_VB_ChannelKeys(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 = "CS_SETTL_INFO_EDITOR.Update_VB_CHNL_KEYS";

        oracom.Parameters.Add("P_ID", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_VB_CHNL_CODE", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_NAME", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_KEYS", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_CHANNEL_TYPE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_current_uid", 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;

        this.Get_VB_ChannelKeys_ID();
        if (response == "Successful")
        {
            oracom.Parameters["P_ID"].Value = pid;
        }
        else
        {
            oracom.Parameters["P_ID"].Value = "";
            lblError.Text = response;
        }

        oracom.Parameters["p_VB_CHNL_CODE"].Value = txtUpdateChannelCode.Text;
        oracom.Parameters["p_CHANNEL_NAME"].Value = ddlUpdatechname.SelectedValue;
        oracom.Parameters["p_CHANNEL_KEYS"].Value = txtUpdatechkeys.Text;
        oracom.Parameters["p_CHANNEL_TYPE"].Value = txtUpdateBillType.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;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         if (e.CommandName.Equals("u"))
        {
            pnlGrid.Enabled = false;
            pnlUpdate.Visible = true;

            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlUpdate.Visible = true;
            pnlGrid.Enabled = false;

            //put the data in the controls
            txtHiddenChannelCode.Text = Uid;
            txtHiddenBillType.Text = GridView1.Rows[index].Cells[1].Text;
            txtHiddenchname.Text = GridView1.Rows[index].Cells[2].Text;
            txtHiddenchkeys.Text = GridView1.Rows[index].Cells[3].Text;

            txtUpdateChannelCode.Text = Uid;
            txtUpdateBillType.Text = GridView1.Rows[index].Cells[1].Text;
            ddlUpdatechname.SelectedValue = GridView1.Rows[index].Cells[2].Text;
            txtUpdatechkeys.Text = GridView1.Rows[index].Cells[3].Text;
        }
    }
   
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlUpdate.Visible = false;
        pnlGrid.Enabled = true;
        clearControls();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ExceptionUtility.LogEvent_Errors(0, "", "btnSave_Click", "Start");
       
            string error = "";
          
            bool saved = Update_VB_ChannelKeys(ref error);
            if (saved)
            {
                lblError.ForeColor = System.Drawing.Color.DarkGreen;
                lblError.Text = "Successfully Updated";
            }
            else
            {
                lblError.Text = error;
            }

            Get_VB_ChannelKeys(Helper.gridSize(), 1);
         
            pnlUpdate.Visible = false;
            pnlGrid.Enabled = true;
            clearControls();

            ExceptionUtility.LogEvent_Errors(0, "", "btnSave_Click", "End");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex);
            lblError.Text = ex.Message;
        }
    }

    private void clearControls()
    {
        txtUpdateChannelCode.Text = "";
        txtUpdateBillType.Text = "";
        txtUpdatechkeys.Text = "";

        this.loadChannel();
    }

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

    private void setMessage(string message)
    {
        ClientScript.RegisterClientScriptBlock(typeof(Button), "hello", "alert('" + message + "');", true);
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        Get_VB_ChannelKeys(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
    
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogEvent_Errors(0, "", "ButtonSearch_Click", "Start");
        
        Get_VB_ChannelKeys(Helper.gridSize(), 1);

        ExceptionUtility.LogEvent_Errors(0, "", "ButtonSearch_Click", "End");
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        Get_VB_ChannelKeys(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
}
