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.Text;

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

    private void loadResult()
    {
        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 = "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Match";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_type", 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_repl_date"].Value = DatePicker1.date;

            if (ddlRecordType.SelectedIndex > 0)
            {
                oracom.Parameters["p_repl_type"].Value = ddlRecordType.SelectedValue;
                parameters = "p_repl_type-" + ddlRecordType.SelectedItem.Text;
            }
            else
                oracom.Parameters["p_repl_type"].Value = OracleString.Null;

            oracom.Parameters["p_lang"].Value = "EN";
            parameters = parameters + ":p_lang-EN:";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            ExceptionUtility.LogException(0, "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Match","34.1", "loadResult", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Match", "34.1", "loadResult", parameters);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult1()
    {
        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 = "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Overage";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_type", 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_repl_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = "p_repl_date-" + DatePicker1.date;
            if (ddlRecordType.SelectedIndex > 0)
            {
                parameters = parameters +":p_repl_type" + ddlRecordType.SelectedItem.Text;
                oracom.Parameters["p_repl_type"].Value = ddlRecordType.SelectedValue;
            }
            else
                oracom.Parameters["p_repl_type"].Value = OracleString.Null;

            oracom.Parameters["p_lang"].Value = "EN";
            parameters = parameters + ":p_lang-EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

            oracon.Close();

            GridView3.DataSource = ds;
            GridView3.DataBind();
            ExceptionUtility.LogException(0, "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Overage","34.2", "loadResult1", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_SUMMARY_VIEWER.Get_ATM_Summary_by_Overage","34.2", "loadResult1",parameters );
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        pnlResult.Visible = true;
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "4.34", "Button1_Click", "");
        loadResult();
        loadResult1();
        
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "4.35", "GridView1_RowCommand.View", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string status = GridView1.DataKeys[index].Value.ToString();
            pnlBR.Visible = true;
            loadATMList(1, status, Helper.gridSize(), 1);
            ViewState["status"] = status;
            ViewState["flag"] = 1;
        }
    }

    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "4.36", "GridView1_RowCommand.View", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string status = GridView3.DataKeys[index].Value.ToString();
            pnlBR.Visible = true;
            loadATMList(2, status, Helper.gridSize(), 1);
            ViewState["status"] = status;
            ViewState["flag"] = 2;
        }
    }

    private void loadATMList(int flag, string status, int pageSize, int pageNumber)
    {
        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;
            if (flag == 1)
                oracom.CommandText = "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Match";
            else
                oracom.CommandText = "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Overage";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            if (flag == 1)
                oracom.Parameters.Add("p_record_status", OracleType.VarChar).Direction = ParameterDirection.Input;
            else
                oracom.Parameters.Add("p_recon_status", 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_repl_date"].Value = DatePicker1.date;
            if(DatePicker1.date .Length >0)
                parameters = "p_repl_date-" + DatePicker1.date;
            if (ddlRecordType.SelectedIndex > 0)
            {
                oracom.Parameters["p_repl_type"].Value = ddlRecordType.SelectedValue;
                parameters = parameters + ":p_repl_type-" + ddlRecordType.SelectedItem.Text;
            }
            else
                oracom.Parameters["p_repl_type"].Value = OracleString.Null;
            if (flag == 1)
                oracom.Parameters["p_record_status"].Value = status;
            else
                oracom.Parameters["p_recon_status"].Value = status;
            parameters = parameters + ":p_recon_status-" + status+":p_lang-EN";
            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();

            GridView2.DataSource = ds;
            GridView2.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
            ExceptionUtility.LogException(0, "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Match,ATM_SUMMARY_VIEWER.Get_ATM_List_by_Overage", "35.1", "loadATMList", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Match,ATM_SUMMARY_VIEWER.Get_ATM_List_by_Overage", "35.1", "loadATMList", parameters);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
        string status = ViewState["status"].ToString();
        int flag = int.Parse(ViewState["flag"].ToString());
        loadATMList(flag, status, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlBR.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "4.131", "GridView2_RowCommand.View", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string atmNo = GridView2.Rows[index].Cells[1].Text;
            string type = GridView2.Rows[index].Cells[2].Text;
            string pReplDate = GridView2.Rows[index].Cells[3].Text;
            string pReplTime = GridView2.Rows[index].Cells[4].Text;
            string ReplDate = GridView2.Rows[index].Cells[5].Text;
            string ReplTime = GridView2.Rows[index].Cells[6].Text;
            StringBuilder sb = new StringBuilder();
            if (!atmNo.Equals("&nbsp;"))
                sb.Append("atmno=" + atmNo + "&");

            if (!pReplDate.Equals("&nbsp;"))
                sb.Append("pReplDate=" + pReplDate + "&");

            if (!pReplTime.Equals("&nbsp;"))
                sb.Append("pReplTime=" + pReplTime + "&");

            if (!ReplDate.Equals("&nbsp;"))
                sb.Append("ReplDate=" + ReplDate + "&");

            if (!ReplTime.Equals("&nbsp;"))
                sb.Append("ReplTime=" + ReplTime + "&");

            string queryString = sb.ToString();
            queryString = queryString.TrimEnd('&');

            if (type.Equals("Dispense"))
            {
                ScriptManager.RegisterClientScriptBlock(this,typeof(string), "new window",
                    "window.open('Repl_1.aspx?" + queryString + "','Replineshment')",true);
            }
            else if (type.Equals("Deposit"))
            {
                ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                    "window.open('Repl_2.aspx?" + queryString + "','Replineshment')", true);
            }
            else
                {
                ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                    "window.open('Repl_atm.aspx?" + queryString + "','Replineshment')", true);
            }
        }
    }

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