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 Business_EJHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

    private void loadResult(string ATMno, 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 = "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());
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        loadResult(txtATM.Text, Helper.gridSize(), 1);
        pnlResult.Visible = true;
    }

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

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            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;
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string error = "";

        bool saved = saveBR(ref error);
        if (saved)
        {
            lblError.ForeColor = System.Drawing.Color.DarkGreen;
            lblError.Text = "Transaction saved";
        }
        else
        {
            lblError.Text = error;
        }
        pnlBR.Visible = false;
    }

    private bool saveBR(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 = "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;
        oracom.Parameters["p_lastrun_cutoff"].Value = txtCutOff.Text;
        oracom.Parameters["p_lastrun_replenishment"].Value = TextBox1.Text;
        oracom.Parameters["p_new_repl_date"].Value = txtRepl.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;
    }

   
}
