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

    protected void btnExecute_Click(object sender, EventArgs e)
    {
        string percent = "", error = "";
        
        Execute(ddlDates.SelectedValue, ref error);
        lblError.Text = error;

        getStatus(ddlDates.SelectedValue, ref percent);
        lblProgressBar.Text = percent.ToString();

        Timer1.Enabled = true;
    }

    private void loadDates()
    {
        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_ptlf_files";

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

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

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

        oracon.Close();
    }

    private void getStatus(string date, ref string percent)
    {
        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.display_percent";

        oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tot_num", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_calc_num", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_percent", OracleType.Number).Direction = ParameterDirection.Output;

        oracom.Parameters["p_date"].Value = date;

        oracom.ExecuteNonQuery();
        oracon.Close();
        percent = oracom.Parameters["p_percent"].Value.ToString();
    }

    private void Execute(string date, 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 = "pos_settelment_monitor.execute_next_pos";

        oracom.ExecuteNonQuery();
        oracon.Close();
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        string percent = "";
        getStatus(ddlDates.SelectedValue, ref percent);
        lblProgressBar.Text = percent.ToString();
    }
}
