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;
using System.Threading;
using System.Diagnostics;

public partial class Business_ATM : System.Web.UI.Page
{
    public int page_id = 0;
    public string table_name = "";
    public string url = "";
    string accountLength = "";
    static int intCount = 0;
    //private bool btn_clicked = false;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        //url = HttpContext.Current.Request.Url.AbsolutePath;
        //string[] urlArr = url.Split('/');
        //url = urlArr[2] + "/" + urlArr[3];
        //page_id = loadPageIDs(url);

        if (!IsPostBack)
        {
            
            txtaddMerchantName.Attributes.Add("onKeyPress", "return KeyPressFuncNotSpecialCharecter();");
            txtAddMerchantAcct.Attributes.Add("onKeyPress", "return KeyPressFuncNotSpecialCharecter();");
            txtNoOfTerminals.Attributes.Add("onKeyPress", "return KeyPressFuncIsWholeNumber();");
   
        
            loadBranches();
            GetTerminalOrderData();

            ExceptionUtility.LogException(0, "", "3", "Page_Load", "");
        }

      
        this.lblError.Text = "";
       
     }


   

    private void loadBranches()
    {
        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 = "LOOKUP_GETTER.Get_Branches";

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

            oracom.Parameters["p_lang"].Value = "EN";

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

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


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

            ddlBranch.Items.Insert(0, "------ Branches ------");
            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Branches", "", "loadBranches", "p_lang-EN");
            // //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }


   
    private void loadResultGrid(string branchID, string MerchantName, string Status, int pageSize, int pageNumber)
    {
        string parameters = "";
        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 = "Terminal_Order.Get_Terminal_Order_Info";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_Merchant_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_Status", OracleType.VarChar).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 (branchID.Length != 0)
            {
                parameters = "p_branch_code-" + branchID;
                oracom.Parameters["p_branch_code"].Value = branchID;
            }
            else
                oracom.Parameters["p_branch_code"].Value = OracleString.Null;
            if (MerchantName.Length != 0)
            {
                parameters = ":p_Merchant_name-" + MerchantName;
                oracom.Parameters["p_Merchant_name"].Value = MerchantName;
            }
            else
                oracom.Parameters["p_Merchant_name"].Value = OracleString.Null;
            if (Status.Length != 0)
            {
                parameters = ":p_Status-" + Status;
                oracom.Parameters["p_Status"].Value = Status;
            }
            else
                oracom.Parameters["p_Status"].Value = OracleString.Null;

            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());
                }
            }
            ExceptionUtility.LogException(0, "ATM_INFO_EDITOR.Get_ATM_Info", "37.1.1", "loadResultGrid", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_INFO_EDITOR.Get_ATM_Info", "37.1.1", "loadResultGrid", parameters);
        }

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        
        ExceptionUtility.LogException(0, "", "3.37", "Button1_Click", "");
        GetTerminalOrderData();

    }



    private void GetTerminalOrderData()
    {
        try
        {
            pnlResult.Visible = true;
            pnl_add_Controls.Visible = false;
            pnlApprove.Visible = false;

            pnlResult.Enabled = true;

            string Branch="", Status="", MerchantName = "";

            if (ddlBranch.SelectedIndex==0)
                Branch = "";
            else
                Branch = ddlBranch.SelectedValue;
          
            Status = ddlStatus.SelectedValue;
            MerchantName = txtMerchantName.Text;


            loadResultGrid(Branch, MerchantName, Status, Helper.gridSize(), 1);

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "", "37.1", "GetTerminalOrderData", "");
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTerminalOrderData();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {


        pnl_add_Controls.Visible = true;
        pnlApprove.Visible = false;
        pnlResult.Visible = false;
        txtAddMerchantAcct.Text = "";
        txtaddMerchantName.Text = "";
        txtNoOfTerminals.Text = "";
        txtRelationOfficerID.Text = "";
        txtRelationOfficerName.Text = "";
        DatePicker1.date = "";

        loadBranches();

        ExceptionUtility.LogException(0, "", "3.320", "btnAdd_Click", "");
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        
        if (e.CommandName.Equals("e"))
        {
            ExceptionUtility.LogException(0, "", "3.32", "GridView1_RowCommand.Update", "");

            this.pnlApprove.Visible=true;
            this.pnl_add_Controls.Visible = false;
            
            
            int index = int.Parse(e.CommandArgument.ToString());
            string p_Record_id = GridView1.DataKeys[index].Value.ToString();

            string status=GridView1.Rows[index].Cells[11].Text;

            if (status == "Entered")
            {
                btnUpdate.Visible = true;
                ddlUpdateStatus.Visible = true;
                LabelStatus.Visible = true;
                
                
            }
            else
            {
                btnUpdate.Visible = false;
                ddlUpdateStatus.Visible = false;
                LabelStatus.Visible = false;
            }
            txtRemarks.Text = GridView1.Rows[index].Cells[12].Text.Trim();

            Session["p_Record_id"] = p_Record_id;
        }
        else if( e.CommandName.Equals("r"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string p1 = GridView1.DataKeys[index].Value.ToString();
           
             ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                    "window.open('../Reports/Default.aspx?rpt=870&p1=" + p1 + "','Report')", true);
                  

        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnl_add_Controls.Visible = false;
        pnlResult.Enabled = true;
      
        
        ExceptionUtility.LogException(0, "", "3.130", "btnCancel_Click", "");
        GetTerminalOrderData();
    }

    public static string DataRowToString(DataRow dr)
    {
        return dr["column_name"].ToString();
    }        
    
    private int loadPageIDs(string URL)
    {
        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 = "lookup_getter.get_pages";

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


            oracom.Parameters["p_url"].Value = URL;

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

            oracon.Close();
            //ExceptionUtility.LogException(0, "lookup_getter.get_pages", "", "loadPageIDs", "URL- " + URL);
            return int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.get_pages", "", "loadPageIDs", "URL- " + URL);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {


            e.Row.Cells[3].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[14].Visible = false;
            //e.Row.Cells[20].Visible = false;
        }

    }

 

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string error = "";
        addTermnialOrder(ref error);
        lblError.Text = error;
        if (error == "Successful")
            GetTerminalOrderData();
    }

    private bool addTermnialOrder(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 = "Terminal_Order.Add_Terminal_Order";

        oracom.Parameters.Add("p_current_uid", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_merchant_name", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_account_no", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_no_of_terminals", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Aggrement_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_branch_ID", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Rel_Office_ID", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Rel_Office_Name", 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_current_uid"].Value = Session["uid"].ToString(); 
        oracom.Parameters["p_merchant_name"].Value = txtaddMerchantName.Text;
        oracom.Parameters["p_account_no"].Value = txtAddMerchantAcct.Text;
        oracom.Parameters["p_no_of_terminals"].Value = Convert.ToInt16(txtNoOfTerminals.Text);
        oracom.Parameters["p_Aggrement_Date"].Value = DatePicker1.date;

        oracom.Parameters["p_terminal_type"].Value = ddlTerminalType.SelectedValue;
  
        oracom.Parameters["p_branch_ID"].Value = ddladdBranchID.SelectedValue;
        oracom.Parameters["p_Rel_Office_ID"].Value = txtRelationOfficerID.Text;
        oracom.Parameters["p_Rel_Office_Name"].Value = txtRelationOfficerName.Text;

        oracom.Parameters["p_lang"].Value = "EN";
      
        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 btnUpdate_Click(object sender, EventArgs e)
    {
        string error = "";

        if (ddlUpdateStatus.SelectedValue == "Rejected" && txtRemarks.Text == "")
            error = "Please Enter the Reason in Remarks";
        else if (UpdateTermnialOrder(ref error))
            GetTerminalOrderData();

        lblError.Text = error;
    }
    private bool UpdateTermnialOrder(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 = "Terminal_Order.Update_Terminal_Order";

        oracom.Parameters.Add("p_current_uid", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Record_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Remarks", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_STATUS", 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_current_uid"].Value = Session["uid"].ToString();
        oracom.Parameters["p_Record_id"].Value = Convert.ToInt16(Session["p_Record_id"] as string );
        oracom.Parameters["p_Remarks"].Value = txtRemarks.Text;
        oracom.Parameters["p_STATUS"].Value = ddlUpdateStatus.SelectedValue;
  
        oracom.Parameters["p_lang"].Value = "EN";

        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 btnCancelUpdate_Click(object sender, EventArgs e)
    {
        pnlApprove.Visible = false;
    }
}
