using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;


namespace SUNWEB
{
   public partial class Logout : System.Web.UI.Page
    {


        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DtResult = new DataTable();

        Int32 K = 0;
        Int32 L = 0;
        Int32 S = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string UserId = string.Empty;


        protected void Page_Load(object sender, EventArgs e)
        {

            //if (Session["UserName"] == null)
            //{
            //    Response.Redirect("Login.aspx",false);
            //}

            //UserId = ObjMain.ExecuteScalar("select Userid from portaldb.Usermaster where UserName='" + Session["UserName"].ToString() + "' and Password='" + Session["Password"] .ToString()+ "' ");
            //UPDATEBRANCH();
            //UPTActiveUserid(UserId);

            //Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.Cache.SetNoStore();

            //Session.Abandon(); 
            //Session["UserName"] = null;
            //Session["_default_branch"] = null;
            //Response.Redirect("Login.aspx",false);

            try
            {
                // Check if user session exists
                if (Session["UserName"] == null || Session["Password"] == null)
                {
                    Response.Redirect("Login.aspx", true); // 'true' ends execution
                    return;
                }

                // Get UserId safely using parameters to avoid SQL injection
                string sql = "SELECT Userid FROM portaldb.Usermaster WHERE UserName=@UserName AND Password=@Password";
              


                MySqlParameter[] parameters =
                  {
                     new MySqlParameter("@UserName", Session["UserName"].ToString()),
                     new MySqlParameter("@Password",Session["Password"]),
                    };
                UserId = ObjMain.NewExecuteScalar(sql, parameters);

                UPDATEBRANCH();
                UPTActiveUserid(UserId);

                // Prevent caching
                Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();

                // Clear session and redirect
                Session.Abandon();
                Session.Clear();

                Response.Redirect("Login.aspx", false);
                Context.ApplicationInstance.CompleteRequest(); // safer, no exception
               return;

            }
            catch (Exception ex)
            {
                // Log the error
                // Optionally redirect to an error page
                Log.Error("Error on Logout : ", ex);
                Response.Redirect("Login.aspx", true);
            }




        }
            
        public void UPDATEBRANCH()
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + Session["Fistbranch"].ToString() + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);

            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            
            
        }

        public void UPTActiveUserid(string UserId)
        {
            string ActiveVal = "0";
            L = ObjMain.ExecuteNonQuery("Update portaldb.Usermaster set  Active=" + ActiveVal + ", UPTTIME='"+Curr_Date+"' where   Userid=" + UserId + " ");
        
        }


    }
}