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();
            loadJobs();
            loadJobNames();
            loadChannels();
            loadChannelEvents();
        }
        lblError.Text = "";
    }

    private void loadJobs()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "APPLICATION_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 = "D";

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

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

        oracon.Close();
       // Label2.Text += "1 ";
    }

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

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

        oracom.Parameters["p_job_type"].Value = "D";

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

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

        ddlJobs.Items.Insert(0, "------ Job Name ------");
        oracon.Close();
    }

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

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


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

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

        ddlChannels.Items.Insert(0, "------ Channels ------");
        oracon.Close();
    }

    private void loadChannelEvents()
    {
        ddlEvents.Items.Clear();
        if (ddlChannels.SelectedIndex > 0)
        {
            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_Recon_Job_Channel_Events";

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

            oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            oracon.Close();
            ddlEvents.DataSource = ds;
            ddlEvents.DataBind();
        }
        ddlEvents.Items.Insert(0, "------ Events ------");

    }

    private bool startJob(ref string error)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "APPLICATION_MONITOR.Start_Revert_Job";

        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_rc_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_re_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_recon_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_all_events", 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["p_job_name"].Value = ddlJobs.SelectedValue;
        oracom.Parameters["p_recon_date"].Value = DatePicker1.date;
        oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue;
        oracom.Parameters["p_re_code"].Value = ddlEvents.SelectedValue;
        oracom.Parameters["p_all_events"].Value = OracleString.Null;

        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")
            return true;
        else
            return false;
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        if (ddlJobs.SelectedIndex == 0)
        {
            lblError.Text = "please choose a job";
        }
        else if (ddlChannels.SelectedIndex == 0)
        {
            lblError.Text = "please choose a channel";
        }
        else if (ddlEvents.SelectedIndex == 0)
        {
            lblError.Text = "please choose an event";
        }
        else if (DatePicker1.date.Length == 0)
        {
            lblError.Text = "please enter a date";
        }
        else
        {
            string error = "";
            bool saved = startJob(ref error);
            if (saved)
            {
                lblError.Text = "Job Reverting Began...";
            }
            else
            {
                lblError.Text = error;
            }
            loadJobs();
        }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        loadJobs();
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadChannelEvents();
    }
}
