using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Net;

public partial class GenerateSGL : System.Web.UI.Page
{



    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //HttpContext.Current.Session["mode"] = "l";
            //Session["uid"] = "1";
            if (Request["sid"] != null)
            {
                txtSearchDate.Text = Request["sid"].Replace('-', '/');
                btnSearch1_Click(sender, e);
            }
            ExceptionUtility.LogException(0, "", "55", "Page_Load", "");

        }


    }

    private void loadFile()
    {
        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 = "gl_processor.Generate_BDC_GL";

            oracom.Parameters.Add("P_Settl_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_File_Name", 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_GL_Output", OracleType.Clob).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_Fl_output", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["P_Settl_Date"].Value = Helper.getDate(txtSearchDate.Text);
            oracom.Parameters["P_File_Name"].Value = "BDC_"+Helper.getDate(txtSearchDate.Text);

            oracom.Parameters["p_lang"].Value = "EN";

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            string error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                OracleLob myLob = (OracleLob)oracom.Parameters["p_GL_Output"].Value;
                string content = (String)myLob.Value;

                string filename = "BDC_ " + Helper.getDate(txtSearchDate.Text) + ".txt";
                StreamWriter sw = new StreamWriter(Server.MapPath("..\\Download\\" + filename));
                sw.Write(content);
                sw.Close();

                oracon.Close();
                HyperLink1.Text = filename;
                HyperLink1.NavigateUrl = "..\\Download\\" + filename;
                btnDownload.Visible = true;
                lblError.Text = error;

                pnlGrid.Visible = true;
                GridView1.DataSource = ds;
                GridView1.DataBind();

                GridView2.DataSource = null;
                GridView2.DataBind();

            }
            else
            {
                btnDownload.Visible = false;
                lblError.Text = error;

                pnlGrid.Visible = false;
            }

            
            // Label2.Text += "1 ";

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "gl_processor.Generate_BDC_GL", "", "GenerateBDCFile", "");

            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadFileDLL()
    {
        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 = "gl_processor.Generate_BDC_GL";

            oracom.Parameters.Add("P_Settl_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_File_Name", 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_GL_Output", OracleType.Clob).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_Fl_output", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["P_Settl_Date"].Value = Helper.getDate(txtSearchDate.Text);
            oracom.Parameters["P_File_Name"].Value = "BDC_" + Helper.getDate(txtSearchDate.Text);

            oracom.Parameters["p_lang"].Value = "EN";

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            string error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                OracleLob myLob = (OracleLob)oracom.Parameters["p_GL_Output"].Value;
                string content = (String)myLob.Value;

                string filename = "BDC_ " + Helper.getDate(txtSearchDate.Text) + ".txt";
                StreamWriter sw = new StreamWriter(Server.MapPath("..\\Download\\" + filename));
                sw.Write(content);
                sw.Close();

                oracon.Close();
                HyperLink1.Text = filename;
                HyperLink1.NavigateUrl = "..\\Download\\" + filename;
                btnDownload.Visible = true;
                lblError.Text = error;

                pnlGrid.Visible = true;
                GridView1.DataSource = ds;
                GridView1.DataBind();

                GridView2.DataSource = null;
                GridView2.DataBind();

            }
            else
            {
                btnDownload.Visible = false;
                lblError.Text = error;

                pnlGrid.Visible = false;
            }


            // Label2.Text += "1 ";

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "gl_processor.Generate_BDC_GL", "", "GenerateBDCFile", "");

            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        if (txtSearchDate.Text.Length > 0)
        {
            loadFile();
            
        }
        else
        {
            lblError.Text = "Please select settlement date";
        }

    }

    protected void btnSearch1_Click(object sender, EventArgs e)
    {

        if (txtSearchDate.Text.Length > 0)
        {
            searchFile();

        }
        else
        {
            lblError.Text = "Please select settlement date";
        }

    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {

        

        //Path of the File to be downloaded.
        string filePath = Server.MapPath(HyperLink1.NavigateUrl);

        //Content Type and Header.
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + HyperLink1.Text);

        //Writing the File to Response Stream.
        Response.WriteFile(filePath);

        //Flushing the Response.
        Response.Flush();
        Response.End();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {

            if (e.CommandName.Equals("v"))
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string Pid = GridView1.DataKeys[index].Value.ToString();
                loadTrans(Pid);
                
                ViewState["pid"] = Pid;

                //lblLegend.Text = pageName;
                ExceptionUtility.LogException(0, "", "54.282", "GridView1_Rowcommand.view", "");
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "", "", "GridView1_Rowcommand", "");

        }
    }

    private void loadTrans(string fileNumber)
    {
        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 = "gl_processor.Display_BDCSettl_Txns";

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

            oracom.Parameters.Add("P_Resp", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

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


            oracom.Parameters["P_Settl_Date"].Value = Helper.getDate(txtSearchDate.Text);
            oracom.Parameters["P_BDC_FileNo"].Value = int.Parse(fileNumber);

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

            string a = oracom.Parameters["P_Resp"].Value.ToString();



            lblError.Text = a;

            pnlDetails.Visible = true;
            GridView2.DataSource = ds;
            GridView2.DataBind();




            // Label2.Text += "1 ";

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "gl_processor.Generate_BDC_GL", "", "GenerateBDCFile", "");

            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void searchFile()
    {
        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 = "gl_processor.Get_TalyTxn_Recs";

            oracom.Parameters.Add("P_File_Date", 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_ResMsg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

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


            oracom.Parameters["P_File_Date"].Value = Helper.getDate(txtSearchDate.Text);

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

            string a = oracom.Parameters["P_Res_Code"].Value.ToString();
            string error = oracom.Parameters["p_ResMsg"].Value.ToString();
            if (a == "0")
            {
                pnlGrid.Visible = true;
                btnDownload.Visible = false;
                GridView1.DataSource = ds;
                GridView1.DataBind();
                GridView2.DataSource = null;
                GridView2.DataBind();

            }
            else
            {
                btnDownload.Visible = false;
                lblError.Text = error;
                GridView2.DataSource = null;
                GridView2.DataBind();
                pnlGrid.Visible = false;
            }


            // Label2.Text += "1 ";

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "gl_processor.Generate_BDC_GL", "", "GenerateBDCFile", "");

            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
}



    