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.IO;

public partial class POS_DeviceRep : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadFiles();
            loadReaons();
            loadTerminalStatus();
            ddlStatus.SelectedValue = "001";
            loadVendorList();
            loadTerminalType(ddlVendor.SelectedValue);
            loadTerminalTypeF(ddlVendorF.SelectedValue);
            loadTerminalTypeUp(ddlVendorUp.SelectedValue);
            btnSearch_Click(sender, e);            
        }
    }

    private void loadReaons()
    {
        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_charge_reasons";

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

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

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

        oracon.Close();
    }

    private void loadTerminalStatus()
    {
        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_terminal_status";

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

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

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

        ddlStatus.Items.Insert(0, "-------All-------");

        oracon.Close();
    }

    private void loadTerminalType(string vendor)
    {
        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_Terminal_Types";

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

        oracom.Parameters["p_vendor_code"].Value = vendor;

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

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

        oracon.Close();
    }

    private void loadTerminalTypeF(string vendor)
    {
        if (ddlVendorF.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 = "poslookup_getter.Get_Terminal_Types";

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

            oracom.Parameters["p_vendor_code"].Value = vendor;

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

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

            oracon.Close();
        }
        ddlTerTypeF.Items.Insert(0, "-------- ALL --------");
    }

    private void loadTerminalTypeUp(string vendor)
    {
        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_Terminal_Types";

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

        oracom.Parameters["p_vendor_code"].Value = vendor;

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

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

        oracon.Close();
    }

    private void loadConnectionType(string terminalType)
    {
        if (String.IsNullOrWhiteSpace(terminalType))
        {
            lblConType.Text = "";
            return;
        }
        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_Term_Type_Conn";

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

        oracom.Parameters["p_term_type"].Value = terminalType;
    

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

        oracon.Close();

        if (ds.Tables[0].Rows.Count >= 1)
            lblConType.Text = ds.Tables[0].Rows[0]["connection_name"].ToString();
        else
            lblConType.Text = "";
    }

    private void loadVendorList()
    {
        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_Vendors_List";

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

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

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

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

        ddlVendorF.DataSource = ds;
        ddlVendorF.DataBind();
        ddlVendorF.Items.Insert(0, "-------- ALL --------");

        oracon.Close();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pnlResult.Visible = true;
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
        loadResult(Helper.gridSize(), 1);
    }

    private void loadResult(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 = "DEVICE_REPOSITORY_EDITOR.Get_Device_List";


        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_no", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_vendor_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_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(ddlVendorF.SelectedIndex > 0)
            oracom.Parameters["p_vendor_code"].Value = ddlVendorF.SelectedValue;
        else
            oracom.Parameters["p_vendor_code"].Value = OracleNumber.Null;
        if(ddlTerTypeF.SelectedIndex > 0)
            oracom.Parameters["p_term_type"].Value = ddlTerTypeF.SelectedValue;
        else
            oracom.Parameters["p_term_type"].Value = OracleNumber.Null;

        if (txtMerchantNo.Text.Length > 0)
            oracom.Parameters["p_merchant_no"].Value = txtMerchantNo.Text;
        else
            oracom.Parameters["p_merchant_no"].Value = OracleNumber.Null;
        if (txtClientNo.Text.Length > 0)
            oracom.Parameters["p_client_no"].Value = txtClientNo.Text;
        else
            oracom.Parameters["p_client_no"].Value = OracleNumber.Null;
        if(ddlStatus.SelectedIndex > 0)
            oracom.Parameters["p_terminal_status"].Value = ddlStatus.SelectedValue;
        else
            oracom.Parameters["p_terminal_status"].Value = OracleString.Null;
        oracom.Parameters["p_terminal_id"].Value = txtTerminal.Text;
        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 ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadResult(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = true;
        pnlResult.Enabled = false;
        txtTerminalID.ReadOnly = false;

        ViewState["mode"] = "a";
        loadConnectionType(ddlTermType.SelectedValue);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            loadDeviceByID(rowID);

            pnlControls.Visible = true;
            pnlResult.Enabled = false;
            txtTerminalID.ReadOnly = true;

            ViewState["mode"] = "u";
            ViewState["rowid"] = rowID;
        }
        else if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool saved = deleteDevice(rowID, ref error);
            if (saved)
            {
                lblError.Text = "Device deleted";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Delete Failed";
                    ExceptionUtility.LogException(9, "", "", "GridView1_RowCommand.Delete", error);
                }
                else
                    lblError.Text = error;
            }
            loadResult(Helper.gridSize(), 1);
        }
        else if (e.CommandName.Equals("f"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            //string error = "";
            //bool saved = disableDevice(rowID, ref error);
            //if (saved)
            //{
            //    lblError.Text = "Device disabled";
            //}
            //else
            //{
            //    lblError.Text = error;
            //}
            //loadResult(Helper.gridSize(), 1);

            pnlFaulty.Visible = true;
            pnlResult.Enabled = false;

            ViewState["rowid"] = rowID;
        }
        else if (e.CommandName.Equals("uf"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            bool saved = revertFaulty(rowID, ref error);
            if (saved)
            {
                lblError.Text = "Device enabled";
            }
            else
            {
                lblError.Text = error;
            }
            loadResult(Helper.gridSize(), 1);

        }
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "DEVICE_REPOSITORY_EDITOR.Get_Device_by_id";

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

        oracom.Parameters["p_device_code"].Value = deviceID;

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

        oracon.Close();

        //set the data in the controls
        txtSerialNo.Text = ds.Tables[0].Rows[0]["serial_number"].ToString();
        datePurchase.date = ds.Tables[0].Rows[0]["purchase_date"].ToString();
        txtModelNo.Text = ds.Tables[0].Rows[0]["model_number"].ToString();
        txtManufac.Text = ds.Tables[0].Rows[0]["manufacturer"].ToString();
        txtSWVersion.Text = ds.Tables[0].Rows[0]["software_version"].ToString();
        txtTerminalID.Text = ds.Tables[0].Rows[0]["terminal_id"].ToString();
        txtTRMS.Text = ds.Tables[0].Rows[0]["trms"].ToString();
        ddlVendor.SelectedValue = ds.Tables[0].Rows[0]["vendor"].ToString();
        loadTerminalType(ddlVendor.SelectedValue);
        ddlTermType.SelectedValue = ds.Tables[0].Rows[0]["term_type"].ToString();
        lblConType.Text = ds.Tables[0].Rows[0]["conn_type"].ToString();

        //lblFaultyReason.Text = ds.Tables[0].Rows[0]["reason"].ToString();
        //lblClientName.Text = ds.Tables[0].Rows[0]["client_name"].ToString();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string mode = ViewState["mode"].ToString();
            string error = "";
            if (mode.Equals("a"))
            {
                bool saved = addDevice(ref error);
                if (saved)
                {
                    lblError.Text = "Device Saved";
                }
                else
                {
                    lblError.Text = error;
                }
            }
            else if (mode.Equals("u"))
            {
                string Uid = ViewState["rowid"].ToString();
                bool saved = updateDevice(Uid, ref error);
                if (saved)
                {
                    lblError.Text = "Device Saved";
                }
                else
                {
                    lblError.Text = error;
                }
            }
//            getATMS();
            ViewState["mode"] = null;
            ViewState["rowid"] = null;
            pnlControls.Visible = false;
            pnlResult.Enabled = true;
            loadResult(Helper.gridSize(), 1);
        }
        catch (Exception ex)
        {
        }
    }

    private bool addDevice(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 = "DEVICE_REPOSITORY_EDITOR.Add_Device";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_serial_number", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_purchase_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_model_number", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_manufacturer", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_software_version", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_id", OracleType.VarChar).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_vendor_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_TRMS", 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_serial_number"].Value = txtSerialNo.Text;
        oracom.Parameters["p_purchase_date"].Value = datePurchase.date;
        oracom.Parameters["p_model_number"].Value = txtModelNo.Text;
        oracom.Parameters["p_manufacturer"].Value = txtManufac.Text;
        oracom.Parameters["p_software_version"].Value = txtSWVersion.Text;
        oracom.Parameters["p_terminal_id"].Value = txtTerminalID.Text;

        oracom.Parameters["p_vendor_code"].Value = ddlVendor.SelectedValue;
        oracom.Parameters["p_term_type"].Value = ddlTermType.SelectedValue;
        oracom.Parameters["p_TRMS"].Value = txtTRMS.Text;

        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 updateDevice(string rowID, 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 = "DEVICE_REPOSITORY_EDITOR.Update_Device";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_device_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_serial_number", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_purchase_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_model_number", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_manufacturer", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_software_version", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_vendor", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_trms", OracleType.VarChar).Direction = ParameterDirection.Input;
        //oracom.Parameters.Add("p_terminal_id", 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_device_code"].Value = rowID;

        if (txtSerialNo.Text.Length > 0)
            oracom.Parameters["p_serial_number"].Value = txtSerialNo.Text;
        else
            oracom.Parameters["p_serial_number"].Value = OracleString.Null;

        if (datePurchase.date.Length > 0)
            oracom.Parameters["p_purchase_date"].Value = datePurchase.date;
        else
            oracom.Parameters["p_purchase_date"].Value = OracleString.Null;

        if (txtModelNo.Text.Length > 0)
            oracom.Parameters["p_model_number"].Value = txtModelNo.Text;
        else
            oracom.Parameters["p_model_number"].Value = OracleString.Null;

        if (txtManufac.Text.Length > 0)
            oracom.Parameters["p_manufacturer"].Value = txtManufac.Text;
        else
            oracom.Parameters["p_manufacturer"].Value = OracleString.Null;

        if (txtSWVersion.Text.Length > 0)
            oracom.Parameters["p_software_version"].Value = txtSWVersion.Text;
        else
            oracom.Parameters["p_software_version"].Value = OracleString.Null;


        if (txtTRMS.Text.Length > 0)
            oracom.Parameters["p_TRMS"].Value = txtTRMS.Text;
        else
            oracom.Parameters["p_TRMS"].Value = OracleString.Null;

        oracom.Parameters["p_vendor"].Value = ddlVendor.SelectedValue;
        oracom.Parameters["p_term_type"].Value = ddlTermType.SelectedValue;
        
        //oracom.Parameters["p_terminal_id"].Value = txtTerminalID.Text;

        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 deleteDevice(string deviceID, ref string error)
    {
        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 = "DEVICE_REPOSITORY_EDITOR.Delete_Device";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_device_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["p_device_code"].Value = deviceID;

            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;
        }
        catch (Exception ex)
        {
            return true;
        }
    }

    private bool disableDevice(string deviceID, ref string error)
    {
        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 = "DEVICE_REPOSITORY_EDITOR.Make_Device_Faulty";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_device_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_reason", 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_device_code"].Value = deviceID;
            oracom.Parameters["p_reason"].Value = ddlReasons.SelectedValue;

            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;
        }
        catch (Exception ex)
        {
            return true;
        }
    }

    private bool revertFaulty(string deviceID, ref string error)
    {
        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 = "DEVICE_REPOSITORY_EDITOR.revert_faulty_device";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_device_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["p_device_code"].Value = deviceID;

            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;
        }
        catch (Exception ex)
        {
            return true;
        }
    }

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

    protected void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadTerminalType(ddlVendor.SelectedValue);
        loadConnectionType(ddlTermType.SelectedValue);
    }

    protected void ddlVendorF_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadTerminalTypeF(ddlVendorF.SelectedValue);
    }

    protected void ddlVendorUp_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadTerminalTypeUp(ddlVendorUp.SelectedValue);
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (ddlFiles.SelectedIndex > -1)
        {
            string error = "";
            bool loaded = submitBulk(ref error);
            if (!loaded)
                lblError.Text = error;
            else
            {
                lblError.Text = "Terminals are successfully loaded.";
                loadFiles();
            }
        }
        else
        {
            lblError.Text = "Please select a file for loading.";
        }
    }

    private void uploadFile()
    {
        //string error = "";
        //bool loaded;
        //string uploadPath = //ConfigurationSettings.AppSettings["Terminals_Dir"];
        //                    Server.MapPath("../UploadedFiles") + "\\";
        //string fileName = uploadPath + FileUpload1.FileName.Replace(" ", "");
        //string ext = fileName.Substring(fileName.Length - 4, 4);
        //fileName = fileName.Substring(0, fileName.Length - 4) + "_";
        //fileName += ddlVendorUp.SelectedValue + "_" + ddlTypeUp.SelectedValue + ext;

        //FileUpload1.SaveAs(fileName);

        //SFTPSender sftpSender = new SFTPSender(fileName);

        //sftpSender.RemotePath = "NCB_POS";

        //if (sftpSender.SftpUpload(ref error))
        //{
        //    loaded = submitBulk(ref error);
        //    if (loaded)
        //        lblError.Text = "Terminals Uploaded";
        //    else
        //        lblError.Text = error;
        //}
        //else
        //    lblError.Text = error;        
        //File.Create(uploadPath + "Ready"); //SAID_MOD 20121003

        //FileUtilityDLL.TerminalFileSender tfs =
        //    new FileUtilityDLL.TerminalFileSender(uploadPath, FileUpload1.FileName.Replace(" ", ""));

    }

    private bool submitBulk(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 = "DEVICE_REPOSITORY_EDITOR.Submit_Bulk_Devices";

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

        oracom.Parameters.Add("p_vendor", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_term_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_file_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_vendor"].Value = ddlVendorUp.SelectedValue;
        oracom.Parameters["p_term_type"].Value = ddlTypeUp.SelectedValue;
        oracom.Parameters["p_file_name"].Value = ddlFiles.SelectedValue;//FileUpload1.FileName.Replace(" ", "");

        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 ddlTermType_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadConnectionType(ddlTermType.SelectedValue);
    }

    protected void btnCancelReason_Click(object sender, EventArgs e)
    {
        pnlFaulty.Visible = false;
        pnlResult.Enabled = true;
    }

    protected void btnSaveReason_Click(object sender, EventArgs e)
    {
        string Uid = ViewState["rowid"].ToString();
        string error = "";

        bool saved = disableDevice(Uid, ref error);
        if (saved)
        {
            lblError.Text = "Device disabled";
        }
        else
        {
            lblError.Text = error;
        }

            ViewState["rowid"] = null;
            pnlFaulty.Visible = false;
            pnlResult.Enabled = true;
        loadResult(Helper.gridSize(), 1);
    }

    private void loadFiles()
    {
        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 = "poslookup_getter.get_bulkterm_dir_files";

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

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

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

            oracon.Close();
            //ExceptionUtility.LogException("Success", "poslookup_getter.get_dir_files", "loadFiles", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "poslookup_getter.get_dir_files", "", "loadFiles", "");

        }
    }

}
