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

            string channelID = "4";
            loadJobs();
            if (!rdDGN.Checked)
               loadDates(channelID);
            
            ExceptionUtility.LogException(0, "", "", "Page_Load", "");
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
                rdDGN.Visible = false;
        }
        lblError.Text = "";
    }

    private void loadJobs()
    {
        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 = "pos_settelment_monitor.Get_Job_Status";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_job_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_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_job_type"].Value = "SETTL";

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

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

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_settelment_monitor.Get_Job_Status","", "loadJobs", "");
            GridView1.DataSource = null;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private bool startJob(ref string error)
    {
        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 = "pos_settelment_monitor.start_pos_settl_job";
            if (rdPOS.Checked)
                oracom.CommandText = "pos_settelment_monitor.start_pos_settl_job";
            else if(rdECOM.Checked)
                 oracom.CommandText = "pos_settelment_monitor.start_Ecom_Settl_Job";
            else if (rdDGN.Checked)
                oracom.CommandText = "pos_settelment_monitor.Gen_CS_DGN_POSSettl"; 
            else
                oracom.CommandText = "pos_settelment_monitor.Gen_Rpt_POSSettl";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_job_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_file_date", 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_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;


            //oracom.Parameters["p_job_name"].Value = "POS_SETTL";
            //parameters = "p_job_name-POS_SETTL:";

            if (rdPOS.Checked)
            {
                oracom.Parameters["p_job_name"].Value = "POS_SETTL";
                parameters = "p_job_name-POS_SETTL:";
            }
            else if (rdECOM.Checked)
            {
                oracom.Parameters["p_job_name"].Value = "Ecom_SETTL";
                parameters = "p_job_name-Ecom_SETTL:";
            }
            else if (rdDGN.Checked)
            {
                oracom.Parameters["p_job_name"].Value = "DGN_INWARD";
                parameters = "p_job_name-DGN_INWARD:";
            }
            else
            {
                oracom.Parameters["p_job_name"].Value = "GEN_RPT_POSSETTL";
                parameters = "p_job_name-GEN_RPT_POSSETTL:";
            }

            oracom.Parameters["p_file_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = parameters + "p_file_date-" + DatePicker1.date;
            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")
            {
                ExceptionUtility.LogException(0, "pos_settelment_monitor.start_pos_settl_job", "354.1", "startJob", parameters );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "pos_settelment_monitor.start_pos_settl_job", "354.1", "startJob", parameters );
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_settelment_monitor.start_pos_settl_job","354.1", "startJob", parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        if (DatePicker1.date.Length > 0)
        {
            string error = "";
            ExceptionUtility.LogException(0, "", "65.354", "btnStart_Click", "");
            bool saved = startJob(ref error);
            if (saved)
            {

                lblError.Text = "Process completed"; // "Job Began...";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An Error Occured!";
                    ExceptionUtility.LogException(9, "", "65.354", "btnStart_Click", error);
                }
                else
                    lblError.Text = error;
            }
            loadJobs();
        }
        else
        {
            ExceptionUtility.LogException(9, "", "", "btnStart_Click", "");
            lblError.Text = "You have to choose a date...";
        }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
      
        //if (rdDGN.Checked)
        //    loadDates("19");
        //else
        //    loadDates("4");
        loadJobs();
    }

    protected void rdDGNPOS_CheckedChanged(object sender, EventArgs e)
    {
        loadDates("19");
        ExceptionUtility.LogException(0, "", "", "rdDGNPOS_CheckedChanged", "");
    }

    protected void rdPOS_CheckedChanged(object sender, EventArgs e)
    {
        loadDates("4");
        ExceptionUtility.LogException(0, "", "", "rdDGNPOS_CheckedChanged", "");
    }

    private void loadDates(string Id)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.CommandText = "pos_settelment_monitor.get_ptlf_files";

            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;
            oracom.Parameters["p_rc_code"].Value = Id;
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
                lblDate.Text = ds.Tables[0].Rows[0][0].ToString();
            else
                lblError.Text = "No Available dates for settlement job";

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_settelment_monitor.get_ptlf_files","", "loadDates", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
}
