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_MonitorRecon : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            loadJobs();
            loadChannels();
            loadChannelEvents();
            btnStart.Enabled = 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 = "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 = "R";

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

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

            oracon.Close();
        }
        catch (Exception ex)
        {
        }
    }

    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_Events_on_ReconPage"; 

            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_Recon_Job";


        oracom.Parameters.Add("p_current_uid", OracleType.Number).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_rc_code"].Value = ddlChannels.SelectedValue;
        oracom.Parameters["p_recon_date"].Value = DatePicker1.date;
        //oracom.Parameters["p_file_date"].Value = DatePicker1.date;
        if (chkAll.Checked)
        {
            oracom.Parameters["p_re_code"].Value = OracleString.Null;
            oracom.Parameters["p_all_events"].Value = "Y";
        }
        else
        {
            oracom.Parameters["p_re_code"].Value = ddlEvents.SelectedValue;
            oracom.Parameters["p_all_events"].Value = "N";
        }
        
        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 (DatePicker1.date.Length == 0)
        {
            lblError.Text = "please enter a date";
        }
        else
        {
            string error = "";
            bool saved = startJob(ref error);
            if (saved)
            {
                lblError.Text = "Job Started";
            }
            else
            {
                lblError.Text = error;
            }
            loadJobs();
        }
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadChannelEvents();
        btnStart.Enabled = false;
        if (ddlChannels.SelectedValue.Equals("2") || ddlChannels.SelectedValue.Equals("11"))
        {
            chkAll.Enabled = false;
        }
        else
            chkAll.Enabled = true;
        chkAll.Checked = false;
    }

    protected void ddlEvents_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlEvents.SelectedIndex > 0)
            btnStart.Enabled = true;
        else
            btnStart.Enabled = false;
        DatePicker1.date = "";
    }

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

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (chkAll.Checked)
        {
            ddlEvents.Enabled = false;
            if (ddlChannels.SelectedIndex > 0)
                btnStart.Enabled = true;
        }
        else
        {
            ddlEvents.Enabled = true;
            ddlEvents.SelectedIndex = 0;
            btnStart.Enabled = false;
        }
    }
}
