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_MerchantSettle : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rowAccount.Visible = false;
            rowMerchant.Visible = true;
            rowDates.Visible = true;

            dtFrom.date = getDayofMonth(1);
            dtTo.date = getDayofMonth(DateTime.Now.Day);

            if (Request.QueryString["clID"] != null)
            {
                txtClientNumber.Text = Request.QueryString["clID"];
                btnSearch_Click(sender, e);
            }
        }
        lblError.Text = "";
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (rdTerminalNo.Checked)
        {
            if (txtTerminalNo.Text.Length > 0 && dtFrom.date.Length > 0 && dtTo.date.Length > 0)
            {
                loadTerminals(txtTerminalNo.Text);
            }
            else
                lblError.Text = "Please Enter All Data...";
        }
        else if (rdMerchants.Checked)
        {
            if (txtClientNumber.Text.Length > 0)
                loadMerchantTerminals(txtClientNumber.Text);
            else
                lblError.Text = "Please Enter All Data...";
        }
        
    }

    protected void rdTerminalNo_CheckedChanged(object sender, EventArgs e)
    {
        pnlTerminals.Visible = false;
        rowAccount.Visible = true;
        rowMerchant.Visible = false;
        rowDates.Visible = true;
    }

    protected void rdMerchants_CheckedChanged(object sender, EventArgs e)
    {
        pnlTerminals.Visible = false;
        rowAccount.Visible = false;
        rowMerchant.Visible = true;
        rowDates.Visible = true;
    }

    //****************************** Terminals ******************************\\

    private void loadMerchantTerminals(string MerID)
    {
        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_merch_settl_viewer.get_client_terms";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_client_no", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_frm_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_to_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_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_client_no"].Value = MerID;
        oracom.Parameters["p_frm_date"].Value = dtFrom.date;
        oracom.Parameters["p_to_date"].Value = dtTo.date;

        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

        oracon.Close();

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

        pnlTerminals.Visible = true;
        pnlServices.Visible = false;
        pnlTxn.Visible = false;
    }

    private void loadTerminals(string termID)
    {
        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_merch_settl_viewer.get_term_period";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_frm_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_to_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_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_term_id"].Value = termID;
        oracom.Parameters["p_frm_date"].Value = dtFrom.date;
        oracom.Parameters["p_to_date"].Value = dtTo.date;

        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

        oracon.Close();

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

        string code = oracom.Parameters["p_res_code"].Value.ToString();
        string msg = oracom.Parameters["p_res_msg"].Value.ToString();

        if (!code.Equals("0"))
            lblError.Text = msg;

        pnlTerminals.Visible = true;
        pnlServices.Visible = false;
        pnlTxn.Visible = false;
    }

    protected void gvTerminals_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string terminalID = gvTerminals.DataKeys[index].Value.ToString();
            string date = gvTerminals.Rows[index].Cells[9].Text.ToString();
            string time = gvTerminals.Rows[index].Cells[10].Text.ToString();
            loadServices(terminalID, date, time);
            ViewState["terminalID"] = terminalID;
            ViewState["date"] = date;
            ViewState["time"] = time;
        }
        else if (e.CommandName.Equals("p"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string terminalID = gvTerminals.DataKeys[index].Value.ToString();
            string date = gvTerminals.Rows[index].Cells[9].Text.ToString();
            string time = gvTerminals.Rows[index].Cells[10].Text.ToString();

            string p1 = Session["uid"].ToString();
            string p2 = terminalID;
            string p3 = date;
            string p4 = time;
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?rpt=101&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "','Report')", true);
        }
    }

    protected void gvTerminals_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 9; i <= 10; i++)
                e.Row.Cells[i].Visible = false;
        }
    }

    //****************************** Service ******************************\\

    private void loadServices(string termID, string date, string time)
    {
        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_MERCH_SETTL_VIEWER.Get_Term_Services";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_time", 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_term_id"].Value = termID;
        oracom.Parameters["p_date"].Value = date;
        oracom.Parameters["p_time"].Value = time;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

        oracon.Close();

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

        pnlServices.Visible = true;
        pnlTxn.Visible = false;
    }

    private void loadServiceTxn( string ptsCode )
    {
        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_MERCH_SETTL_VIEWER.Get_Term_Service_Trx";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_pts_code", 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_pts_code"].Value = ptsCode;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

        oracon.Close();

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

        pnlTxn.Visible = true;
    }

    protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();
            ViewState["rowID"] = rowID;
            string status = gvServices.Rows[index].Cells[11].Text.ToString();
            if (!status.ToLower().Equals("loaded"))
                loadServiceTxn(rowID);
            else
                lblError.Text = "Can't view transactions for loaded services";
        }
        else if (e.CommandName.Equals("p"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();

            string p1 = Session["uid"].ToString();
            string p2 = rowID;
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?rpt=100&p1=" + p1 + "&p2=" + p2 + "','Report')", true);
        }
    }

    protected void gvServices_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvServices.PageIndex = e.NewPageIndex;
        loadServices(ViewState["terminalID"].ToString(), ViewState["date"].ToString(),
                        ViewState["time"].ToString());
    }

    //****************************** Transactions ******************************\\

    protected void gvTxn_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTxn.PageIndex = e.NewPageIndex;
        loadServiceTxn(ViewState["rowID"].ToString());
    }

    private string getDayofMonth(int pDay)
    {
        string day = "", month = "", year = "";
        if (pDay < 10)
            day = "0" + pDay.ToString();
        else
            day = pDay.ToString();
        if (DateTime.Now.Month < 10)
            month = "0" + DateTime.Now.Month.ToString();
        else
            month = DateTime.Now.Month.ToString();
        year = DateTime.Now.Year.ToString();
        string date = day + "-" + month + "-" + year;
        return date;
    }
}
