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_VPMerchants : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadMerStatus();
            loadReqTypes();
            ddlMerStatus.SelectedValue = "007";
            loadMerchants("", "", "", Helper.gridSize(), 1);            
            ddlMerStatus.Enabled = true;
            ddlSama.Enabled = false;
            ddlSupport.Enabled = false;
            ddlB24.Enabled = false;
        }
        lblError.Text = "";
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddMerchant.aspx");
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "poslookup_getter.get_mer_req_types";

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

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

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

        oracon.Close();
    }

    protected void btnSearchMerchants_Click(object sender, EventArgs e)
    {
        txtMerchantNumber.Text = txtMerchantNumber.Text.Trim();
        txtMAppNumber.Text = txtMAppNumber.Text.Trim();
        txtFltrAccNo.Text = txtFltrAccNo.Text.Trim();
        txtRetailerName.Text = txtRetailerName.Text.Trim();
        txtTerminalID.Text = txtTerminalID.Text.Trim();
        txtRetailerID.Text = txtRetailerID.Text.Trim();
        int temp = 0;
        try
        {

            if (txtTerminalPhone.Text.Length > 0)
            {
                temp = int.Parse(txtTerminalPhone.Text);
            }
            temp = 1;
        }
        catch (Exception ex)
        {
            lblError.Text = "please enter numeric values only in the field Terminal Phone";
        }
        if (temp == 1) loadMerchants(txtMerchantNumber.Text, txtMAppNumber.Text, "", Helper.gridSize(), 1);
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadMerchants(txtMerchantNumber.Text, txtMAppNumber.Text, "", Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "poslookup_getter.get_Mer_Pend_Status";

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

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

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

        oracon.Close();
    }

    private void loadMerchants(string merNo, string mAppNo, string status, int pageSize, int pageNumber)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "merchant_reg.get_merchant_info";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_merchant_no", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_client_number", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_account_number", OracleType.VarChar).Direction = ParameterDirection.Input;        
        
        oracom.Parameters.Add("p_retailer_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_retailer_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_phone_num", OracleType.VarChar).Direction = ParameterDirection.Input;
        
        oracom.Parameters.Add("p_sama_flag", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_operation_flag", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_b24_flag", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_vplus_flag", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_support_flag", OracleType.Number).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_modified", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_req_type", OracleType.Number).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_num", OracleType.Number).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.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

        if(merNo == "")
            oracom.Parameters["p_merchant_no"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_merchant_no"].Value = merNo;
        if (mAppNo == "")
            oracom.Parameters["p_client_number"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_client_number"].Value = mAppNo;

        oracom.Parameters["p_account_number"].Value = txtFltrAccNo.Text;
        oracom.Parameters["p_status"].Value = ddlMerStatus.SelectedValue;
        oracom.Parameters["p_retailer_id"].Value = txtRetailerID.Text;
        oracom.Parameters["p_retailer_name"].Value = txtRetailerName.Text.Replace("%", "");
        oracom.Parameters["p_terminal_id"].Value = txtTerminalID.Text;
        oracom.Parameters["p_term_phone_num"].Value = txtTerminalPhone.Text;

        if (ddlSama.SelectedIndex == 0)
            oracom.Parameters["p_sama_flag"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_sama_flag"].Value = ddlSama.SelectedValue;

        oracom.Parameters["p_operation_flag"].Value = OracleNumber.Null;

        if (ddlB24.SelectedIndex == 0)
            oracom.Parameters["p_b24_flag"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_b24_flag"].Value = ddlB24.SelectedValue;

        if (ddlVPlus.SelectedIndex == 0)
            oracom.Parameters["p_vplus_flag"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_vplus_flag"].Value = ddlVPlus.SelectedValue;

        if (ddlSupport.SelectedIndex == 0)
            oracom.Parameters["p_support_flag"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_support_flag"].Value = ddlSupport.SelectedValue;
        if (ddlModified.SelectedValue == "100")
            oracom.Parameters["p_modified"].Value = OracleNumber.Null;
        else
            oracom.Parameters["p_modified"].Value = ddlModified.SelectedValue;
        oracom.Parameters["p_req_type"].Value = ddlReqType.SelectedValue;
        oracom.Parameters["p_page_size"].Value = pageSize;
        oracom.Parameters["p_page_num"].Value = pageNumber;
        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);

        int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
        if (pageNumber == 1)
        {
            ddlPages.Items.Clear();
            for (int i = 1; i <= nop; i++)
            {
                ddlPages.Items.Add(i.ToString());
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("u"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string clientNo = GridView1.Rows[index].Cells[1].Text.ToString();
            Response.Redirect("UpdateMerchant.aspx?clID=" + clientNo);
        }
        else if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string clientNo = GridView1.Rows[index].Cells[1].Text.ToString();
            Response.Redirect("MerchantSettle.aspx?clID=" + clientNo);
        }
        else if (e.CommandName.Equals("a"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string clientNo = GridView1.Rows[index].Cells[1].Text.ToString();

            string error = "";
            //only approve if it's submitted...
            string Submitted = GridView1.Rows[index].Cells[5].Text.ToString();
            //if (Submitted.ToLower().Equals("submitted") || Submitted.ToLower().Equals("modified"))
            //{
                bool saved = Approve(rowID, ref error);
                if (saved)
                {
                    lblError.Text = "Merchant Approved";
                    ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                   "window.open('Comment.aspx?clID=" + clientNo + "&action=Operation Approve Merchant','MerchantComment','status=1,width=700,height=250')", true);
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "", "GridView1_RowCommand.Approve", error);
                    }
                    else
                        lblError.Text = error;
                }
            //}
            //else
            //{
            //    lblError.Text = "Only Merchants with the status 'Submitted' can be approved";
            //}
        }
        else if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string clientNo = GridView1.Rows[index].Cells[1].Text.ToString();

            string error = "";
            //only decline if it's submitted...
            string Submitted = GridView1.Rows[index].Cells[5].Text.ToString();
            //if (Submitted.ToLower().Equals("submitted"))
            //{
                bool saved = Decline(rowID, ref error);
                if (saved)
                {
                    lblError.Text = "Merchant Declined";
                    ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                   "window.open('Comment.aspx?clID=" + clientNo + "&action=Operation Declined Merchant','MerchantComment','status=1,width=700,height=250')", true);
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "", "GridView1_RowCommand.Decline", error);
                    }
                    else
                        lblError.Text = error;
                }
            //}
            //else
            //{
            //    lblError.Text = "Only Merchants with the status 'Submitted' can be declined";
            //}
        }
        else if (e.CommandName.Equals("r"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string clientNo = GridView1.Rows[index].Cells[1].Text.ToString();

            string error = "";
            bool saved = Reset(rowID, ref error);
            if (saved)
            {
                lblError.Text = "Merchant Reset Successful";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An Error Occured!";
                    ExceptionUtility.LogException(9, "", "", "GridView1_RowCommand.Reset", error);
                }
                else
                    lblError.Text = error;
            }
        }

        loadMerchants(txtMerchantNumber.Text, txtMAppNumber.Text, "", Helper.gridSize(), 1);
    }

    private bool Approve(string index, 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 = "merchant_reg.approve_vplus";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_mt_code", OracleType.Number).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_mt_code"].Value = index;

        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;
    }

    private bool Decline(string index, 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 = "merchant_reg.decline_vplus";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_mt_code", OracleType.Number).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_mt_code"].Value = index;

        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;
    }

    private bool Reset(string index, 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 = "merchant_reg.Reset_Merch_Decline";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_mt_code", OracleType.Number).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_mt_code"].Value = index;

        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;
    }

}
