using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
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.Diagnostics;
using System.IO;

public partial class VATRejected : System.Web.UI.Page
{
    public int page_id = 0;
    public string table_name = "";
    string url = "";
    static int intCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        //table_name = "business_rules";
        //url = HttpContext.Current.Request.Url.AbsolutePath;
        //string[] urlArr = url.Split('/');
        //url = urlArr[2] + "/" + urlArr[3];
        //page_id = loadPageIDs(url);
        //if (!IsPostBack)
        //{
        //    SecurityAgent.AuthenticateArchive();
            
        //    ExceptionUtility.LogException(0, "", "5", "Page_Load", "");
        //}
    }
    private int loadPageIDs(string URL)
    {
        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 = "lookup_getter.get_pages";

            oracom.Parameters.Add("p_url", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pages", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_url"].Value = URL;

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

            oracon.Close();
            return int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.get_pages","", "loadPageIDs", "URL: " + URL);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
    

    private void loadFileNames()
    {
        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 = "Generate_VAT_Invoice.GETVAT_Rej_FileDet";

            oracom.Parameters.Add("P_FromDate", OracleType.VarChar, 250).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_ToDate", OracleType.VarChar, 250).Direction = ParameterDirection.Input;

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

            oracom.Parameters["P_FromDate"].Value = dtPostDateFrom.date;
            oracom.Parameters["P_ToDate"].Value = dtPostDateTo.date;
            
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ddlFiles.DataSource = ds;
            ddlFiles.DataBind();
            ddlFiles.Items.Insert(0, "------ Files ------");

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "Generate_VAT_Invoice.Get_GWFileName", "", "loadFileNames", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    

    private void loadResult(string fileNumber)
    {
        string parameters = "";
        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 = "Generate_VAT_Invoice.GET_VAT_RejTxns";

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

            //if (channelID.Length != 0)
            //{
            //    parameters = "p_rc_code-" + channelID;
            //    oracom.Parameters["p_rc_code"].Value = channelID;
            //}
            //else
            //    oracom.Parameters["p_rc_code"].Value = OracleNumber.Null;
            //if (eventID.Length != 0)
            //{
            //    parameters = parameters + "p_re_code-" + eventID;
            //    oracom.Parameters["p_re_code"].Value = eventID;
            //}
            //else
            //    oracom.Parameters["p_re_code"].Value = OracleNumber.Null;

            oracom.Parameters["P_Resp_FileNumber"].Value = fileNumber;
            

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

            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            
            if (currentMethod.Contains("Button1_Click"))
                ExceptionUtility.LogException(0, "BUSINESS_RULES_EDITOR.Get_Business_Rules","132.1", "loadResult", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BUSINESS_RULES_EDITOR.Get_Business_Rules","132.1", "loadResult", parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //lblError.Text = "";
        if (ddlFiles.SelectedIndex <1 && txtRespFileNumber.Text.Length == 0)
        {
            lblError.Text = "Please select a file or enter file number";
        }
        else
        {
            pnlResult.Visible = true;
            //HyperLink1.Visible = false;
            pnlBR.Visible = false;
            ExceptionUtility.LogException(0, "", "5.132", "Button1_Click", "");//channelID: " + channelID + ", eventID: " + eventID + ", pageSize: " + pageSize + ", pageNumber: " + pageNumber);
            string FileNumber = "";
            if (txtRespFileNumber.Text.Length != 0)
            {
                FileNumber = txtRespFileNumber.Text;
                ddlFiles.Items.Clear();
                dtPostDateFrom.date = "";
                dtPostDateTo.date = "";
            }
            else
                FileNumber = ddlFiles.SelectedValue;
            loadResult(FileNumber);
            lblError.Text = "";
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string channelID = "", EventID = "";
        //if (ddlChannels.SelectedIndex > 0)
        //    channelID = ddlChannels.SelectedValue;
        //if (ddlEvents.SelectedIndex > 0)
        //    EventID = ddlEvents.SelectedValue;
        //pnlBR.Visible = false;
        //ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
        //loadResult(channelID, EventID, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("r"))
        {
            intCount = 0;
            ExceptionUtility.LogException(0, "", "5.8", "GridView1_RowCommand.Edit", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string narrative = GridView1.Rows[index].Cells[3].Text.ToString();
            string status = GridView1.Rows[index].Cells[8].Text.ToString();
            if (status.ToLower().Equals("rejected"))
            {
                approve(rowID, narrative);
                Button1_Click(sender, e);
            }
            else
                lblError.Text = "Can only Reporcess Rejected Transactions";
        }
        
        
    }

    private void approve(string trnCode, string narrative)
    {
        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 = "Generate_VAT_Invoice.Reprocess_RejRec";

            oracom.Parameters.Add("P_Current_Id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_Txn_Code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_Vat_Inv_Inward", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_Txn_Resp", OracleType.VarChar).Direction = ParameterDirection.Input;

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


            oracom.Parameters["P_Current_Id"].Value = Session["uid"].ToString();
            oracom.Parameters["P_Txn_Code"].Value = trnCode;

            if (txtRespFileNumber.Text.Length > 0)
                oracom.Parameters["P_Vat_Inv_Inward"].Value = txtRespFileNumber.Text;
            else
                oracom.Parameters["P_Vat_Inv_Inward"].Value = ddlFiles.SelectedValue;
            oracom.Parameters["P_Txn_Resp"].Value = narrative;

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["P_Res_Msg"].Value.ToString();
            lblError.Text = a;


            // Label2.Text += "1 ";

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

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

   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        
    }


    protected void btnGetFiles_Click(object sender, EventArgs e)
    {
        loadFileNames();
        ExceptionUtility.LogException(0, "", "", "btnGetFiles_Click", "");//rowID: " + rowID);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string FileNumber = "";
        if (txtRespFileNumber.Text.Length != 0)
            FileNumber = txtRespFileNumber.Text;
        else
            FileNumber = ddlFiles.SelectedValue;
        loadResult(FileNumber); 
    }
}
