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;
using System.Diagnostics;

public partial class Business_EJHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ExceptionUtility.LogException(0, "", "8", "Page_Load", "");
        }
    }

    private void loadResult(string ATMno, int pageSize, int pageNumber)
    {
        try
        {
            string currentMethod = "";
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "EJ_RUN_HISTORY_VIEWER.Get_EJ_Runs";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", 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_atm_num"].Value = ATMno;
            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());
                }
            }
            if (currentMethod.Contains("Button1_Click"))
                ExceptionUtility.LogException(0, "EJ_RUN_HISTORY_VIEWER.Get_EJ_Runs", "44.14", "loadResult", "ATMno- " + ATMno+":p_lang-EN");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "EJ_RUN_HISTORY_VIEWER.Get_EJ_Runs", "44.14", "loadResult", "ATMno- " + ATMno + ":p_lang-EN");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "8.44", "Button1_Click", "");
        loadResult(txtATM.Text, Helper.gridSize(), 1);
        pnlResult.Visible = true;

    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "8.44", "ddlPages_SelectedIndexChanged", ddlPages .SelectedItem .Text );
        loadResult(txtATM.Text, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            ExceptionUtility.LogException(0, "", "8.45", "GridView1_RowCommand.Edit", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string Terminal = GridView1.DataKeys[index].Value.ToString();
            string lastcutoff = GridView1.Rows[index].Cells[1].Text;
            string lastrepl = GridView1.Rows[index].Cells[2].Text;
            pnlBR.Visible = true;
            pnlBR.Enabled = true;
            txtTerminal.Text = Terminal;
            txtCutOff.Text = lastcutoff;
            txtRepl.Text = lastrepl;
            TextBox1.Text = lastrepl;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string error = "";
        ExceptionUtility.LogException(0, "", "8.413", "btnSave_Click", "");
        bool saved = saveBR(ref error);
        if (saved)
        {
            
            lblError.Text = "Transaction saved";
        }
        else
        {
            if (Helper.OracleError(error ))
            {
                lblError.Text = "An Error Occured!";
                ExceptionUtility.LogException(9, "", "8.413", "btnSave_Click", error);
            }
            else
                lblError.Text = error ;
           
        }
        pnlBR.Visible = false;
    }

    private bool saveBR(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 = "EJ_RUN_HISTORY_VIEWER.Update_EJ_Repl_date";

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

            oracom.Parameters.Add("p_lastrun_cutoff", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lastrun_replenishment", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_repl_date", OracleType.DateTime).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_term_id"].Value = txtTerminal.Text;
            if (txtTerminal.Text.Length > 0)
                parameters = "p_term_id-" + txtTerminal.Text;
            oracom.Parameters["p_lastrun_cutoff"].Value = txtCutOff.Text;
            if (txtCutOff.Text.Length > 0)
                parameters = parameters + ":p_lastrun_cutoff-" + txtCutOff.Text;
            oracom.Parameters["p_lastrun_replenishment"].Value = TextBox1.Text;
            if (TextBox1.Text.Length > 0)
                parameters = parameters + ":p_lastrun_replenishment-" + TextBox1.Text;
            oracom.Parameters["p_new_repl_date"].Value = txtRepl.Text;
            if (txtRepl.Text.Length > 0)
                parameters = parameters + ":p_new_repl_date-" + txtRepl.Text;
            oracom.Parameters["p_lang"].Value = "EN";
            parameters = parameters + ":p_lang-EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "EJ_RUN_HISTORY_VIEWER.Update_EJ_Repl_date", "413.1", "saveBR", parameters );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "EJ_RUN_HISTORY_VIEWER.Update_EJ_Repl_date", "413.1", "saveBR", parameters);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "EJ_RUN_HISTORY_VIEWER.Update_EJ_Repl_date", "413.1", "saveBR", parameters);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

   
}
