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

public partial class Business_ATMCashReconS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadValues(sender, e);
        }
    }
    private void loadValues(object sender, EventArgs e)
    {
        if (Request.QueryString["repl_date"] != null && Request.QueryString["atm_type"]!=null)
        {
            string d, m, y;
            d = Request.QueryString["repl_date"].Substring(4, 2);
            m = Request.QueryString["repl_date"].Substring(2, 2);
            y = Request.QueryString["repl_date"].Substring(0, 2);

            DatePicker1.date = d + "-" + m + "-" + "20" + y;  
            if(Request.QueryString["atm_type"]=="009")
                ddlRecordType.SelectedItem.Text = "Dispense";
            else
                ddlRecordType.SelectedItem.Text = "Deposit";

            Button1_Click(sender, e);
           
        }
       
    }
    private void loadResult()
    {
        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;
        else
            oracom.Parameters["p_repl_type"].Value = OracleString.Null;

        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);

        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    private void loadResult1()
    {
        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 (ddlRecordType.SelectedIndex > 0)
            oracom.Parameters["p_repl_type"].Value = ddlRecordType.SelectedValue;
        else
            oracom.Parameters["p_repl_type"].Value = OracleString.Null;

        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);

        oracon.Close();

        GridView3.DataSource = ds;
        GridView3.DataBind();
    }

    private void loadResult2()
    {
        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";

        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;
        else
            oracom.Parameters["p_repl_type"].Value = OracleString.Null;

        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);

        oracon.Close();

        gvGL.DataSource = ds;
        gvGL.DataBind();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        pnlResult.Visible = true;
        pnlBR.Visible = false;
        loadResult();
        loadResult1();
        loadResult2();
    }

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

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

    private void loadATMList(int flag, string status, 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;
        
            if (flag == 1)
                oracom.CommandText = "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Match";
            else if (flag == 2)
                oracom.CommandText = "ATM_SUMMARY_VIEWER.Get_ATM_List_by_Overage";
            else
            {
                switch (status)
                {
                    case "1":
                        oracom.CommandText = "ATM_SUMMARY_VIEWER.get_active_atm_list";
                        break;
                    case "2":
                        oracom.CommandText = "ATM_SUMMARY_VIEWER.get_replenished_atm_list";
                        break;
                    case "3":
                        oracom.CommandText = "ATM_SUMMARY_VIEWER.get_atm_EJ_Avl_GL_Miss";
                        break;
                    case "4":
                        oracom.CommandText = "ATM_SUMMARY_VIEWER.get_atm_EJ_Miss_GL_Avl";
                        break;
                    case "5":
                        oracom.CommandText = "ATM_SUMMARY_VIEWER.get_atm_gl_entry_approved";
                        break;

                }
            }

        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 (ddlRecordType.SelectedIndex > 0)
            oracom.Parameters["p_repl_type"].Value = ddlRecordType.SelectedValue;
        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;

        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());

        if (nop == -1 && ds.Tables[0].Rows.Count > 0)
            nop = int.Parse(Math.Ceiling((double.Parse(ds.Tables[0].Rows[0]["ROW_COUNT"].ToString()) / pageSize)).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());
            }
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        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;
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            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;
            string openingBalance = GridView2.Rows[index].Cells[8].Text;
            string closingBalance = GridView2.Rows[index].Cells[9].Text;
            string replCurrency=GridView2.Rows[index].Cells[12].Text;
           
            StringBuilder sb = new StringBuilder();

            if (!atmNo.Equals("&nbsp;"))
            {
                atmNo = atmNo.Substring(atmNo.Length - 4);
                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 + "&");



                if (ViewState["status"].Equals("4"))
                {
                    sb.Append("status=" + ViewState["status"] + "&");
                    if (!type.Equals("&nbsp;"))
                        sb.Append("type=" + type + "&");
                    if (!replCurrency.Equals("&nbsp;"))
                        sb.Append("replCurrency=" + replCurrency + "&");
                    if(!openingBalance .Equals("&nbsp;"))
                        sb.Append("openingBalance=" + openingBalance + "&");
                    if(!closingBalance .Equals("&nbsp;"))
                        sb.Append("closingBalance=" + closingBalance + "&");
                }
                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[10].Visible = false;
            e.Row.Cells[12].Visible = false;
            if (ViewState["flag"].ToString() == "3")
            {
                e.Row.Cells[8].Visible = true;
                e.Row.Cells[9].Visible = true;

                switch (ViewState["status"].ToString ())
                {
                    case "1":
                        e.Row.Cells[3].Visible = false;
                        e.Row.Cells[4].Visible = false;
                        e.Row.Cells[5].Visible = false;
                        e.Row.Cells[6].Visible = false;
                        e.Row.Cells[7].Visible = false;
                        e.Row.Cells[8].Visible = false;
                        e.Row.Cells[9].Visible = false;                  
                        e.Row.Cells[11].Visible = false;
                        break;
                    case "2":
                        e.Row.Cells[3].Visible = true;
                        e.Row.Cells[4].Visible = true;
                        e.Row.Cells[5].Visible = true;
                        e.Row.Cells[6].Visible = true;
                        e.Row.Cells[7].Visible = false;
                         e.Row.Cells[8].Visible = true;
                        e.Row.Cells[9].Visible = true;
                        e.Row.Cells[11].Visible = true;
                        break;
                    case "3":
                        e.Row.Cells[3].Visible = true;
                        e.Row.Cells[4].Visible = true;
                        e.Row.Cells[5].Visible = true;
                        e.Row.Cells[6].Visible = true;
                        e.Row.Cells[7].Visible = false;
                        e.Row.Cells[8].Visible = true;
                        e.Row.Cells[9].Visible = true;
                        e.Row.Cells[11].Visible = true;
                        break;
                    case "4":
                        e.Row.Cells[3].Visible = false;
                        e.Row.Cells[4].Visible = false;
                        e.Row.Cells[5].Visible = true;
                        e.Row.Cells[6].Visible = true;
                        e.Row.Cells[7].Visible = false;
                        e.Row.Cells[8].Visible = true;
                        e.Row.Cells[9].Visible = true;
                        e.Row.Cells[11].Visible = true;
                        break;
                    case "5":
                        e.Row.Cells[3].Visible = true;
                        e.Row.Cells[4].Visible = true;
                        e.Row.Cells[5].Visible = true;
                        e.Row.Cells[6].Visible = true;
                        e.Row.Cells[7].Visible = false;
                        e.Row.Cells[8].Visible = true;
                        e.Row.Cells[9].Visible = true;
                        e.Row.Cells[11].Visible = true;
                        break;

                }
                
            }
            else
            {
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[9].Visible = false;
                e.Row.Cells[10].Visible = false;
            }
        }
    }
    protected void ddlRecordType_SelectedIndexChanged(object sender, EventArgs e)
    {
        pnlResult.Visible = false;
    }
    protected void gvGL_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ViewState["status"] = "";
            lblATMList.Text = "";
            int index = int.Parse(e.CommandArgument.ToString());
            string status = gvGL.DataKeys[index].Value.ToString();
            lblATMList.Visible = true;
            lblATMList.Text = "";
            if (status.Equals("1"))
                lblATMList.Text = "Active ATM List";
            else if (status.Equals("2"))
                lblATMList.Text = "Replenished ATM List";
            else if (status.Equals("3"))
                lblATMList.Text = "EJ Availabe, GL Missing ATM List";
            else if (status.Equals("4"))
                lblATMList.Text = "EJ Missing, GL Available ATM List";
            else
                lblATMList.Text = "GL MANUAL ENTRY IS APPROVED ATM List";
            pnlBR.Visible = true;
            ViewState["flag"] = 3;
            ViewState["status"] = status;            
            loadATMList(3, status, Helper.gridSize(), 1);
            
            
        }
    }
    protected void gvGL_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvGL.Rows)
        {
            if (row.RowIndex == gvGL.SelectedIndex)
            {
                row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            }
            else
            {
                row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            }
        }
    }
}
