using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
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 Loginaspx : System.Web.UI.Page
    {
        BL_ErrorLog ObjLog = new BL_ErrorLog();
        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();
        Int32 K = 0;
        Int32 L = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtEnduer = new DataTable();
        DataTable DtLicense = new DataTable();
        // User Variables 

        string UserId = string.Empty;
        string UserName = string.Empty;
        string PortalTypeId = string.Empty;
        string cd_Id = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string IsMultiBranch = string.Empty;
        string IsSuperAdmin = string.Empty;
        // License Variables 
        string L_MaxUserCnt = string.Empty;
        string L_RetailDashboard = string.Empty;
        string L_SummarySales = string.Empty;
        string L_ItemSales = string.Empty;
        string L_TaxAnalysis = string.Empty;
        string L_TopSale = string.Empty;
        string L_MothlySales = string.Empty;
        string L_HourlySales = string.Empty;
        string L_GroupSale = string.Empty;
        string CM_ID;
        string CD_ID;
        string Fistbranch = string.Empty;
        string ActiveUserid = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSingnin_Click(object sender, EventArgs e)
        {
           if (txtUserName.Text != "" && txtPassword.Text != "")
            {
               

               ReadUsers();

            }
            else
            {
               
                Page page = HttpContext.Current.Handler as Page;
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' User Name is Wrong \n  Password is  Wrong\n Invalid User - Contact Suntoyo ');location.href='Login.aspx'", true);
            }

        }

        public void ReadUsers()
        {

            try
            {

                DT = ObjUserBL.ReadUsersForIsMultiBranch(txtUserName.Text, txtPassword.Text);
                Dt2 = ObjUserBL.ReadUsersForIsSuperAdmin(txtUserName.Text, txtPassword.Text);
                DtEnduer = ObjUserBL.ReadUsers4Enduser(txtUserName.Text, txtPassword.Text);


                if (DT.Rows.Count > 0 || Dt2.Rows.Count > 0)
                {

                    string UserId = DT.Rows[0]["Userid"].ToString();
                    string UserName = DT.Rows[0]["UserName"].ToString();
                    string PortalTypeId = DT.Rows[0]["PortalTypeId"].ToString();
                    string cd_Id = DT.Rows[0]["cd_id"].ToString();
                    string Datapath = DT.Rows[0]["Datapath"].ToString();
                    string cd_BranchAlias = DT.Rows[0]["cd_BranchAlias"].ToString();
                    CM_ID = ObjSel.GETcmid(cd_Id);
                    Fistbranch = ObjSel.GETBrachAlias(CM_ID);

                    Session["Fistbranch"] = Fistbranch;
                    Session["Branch_HQ"] = cd_BranchAlias;
                    Session["DataPath"] = Datapath;
                    Session["UserId"] = UserId;
                    Session["Cd_Id"] = cd_Id;
                    Session["PortalTypeId"] = PortalTypeId;
                    string IsMultiBranch = DT.Rows[0]["IsMultiBranch"].ToString();
                    Session["IsMultiBranch"] = IsMultiBranch;

                    string IsSuperAdmin = DT.Rows[0]["IssuperAdmin"].ToString();
                    Session["IsSuperAdmin"] = IsSuperAdmin;
                    Session["UserName"] = txtUserName.Text;
                    Session["Password"] = txtPassword.Text;
                    // New  Code: June:18-19
                    ActiveUserid = ObjMain.ExecuteScalar("SELECT Active from portaldb.usermaster  where userid=" + UserId + " ");
                    if (ActiveUserid == "")
                    {

                        //   ActiveUserUPT(UserId);                 // Update in UserMaster Active - as 1
                        Response.Redirect("Dashboard.aspx");
                    }
                    else if (Convert.ToBoolean(ActiveUserid) != true)
                    {
                        // ActiveUserUPT(UserId);                 // Update in UserMaster Active - as 1
                        Response.Redirect("Dashboard.aspx",false);
                    }
                    else
                    {
                        Response.Redirect("Test.aspx", false);
                    }


                }
                else if (DtEnduer.Rows.Count > 0)
                {

                    string UserId = DtEnduer.Rows[0]["Userid"].ToString();
                    string UserName = DtEnduer.Rows[0]["UserName"].ToString();
                    string PortalTypeId = DtEnduer.Rows[0]["PortalTypeId"].ToString();
                    string cd_Id = DtEnduer.Rows[0]["cd_id"].ToString();
                    string Datapath = DtEnduer.Rows[0]["Datapath"].ToString();
                    string cd_BranchAlias = DtEnduer.Rows[0]["cd_BranchAlias"].ToString();

                    CM_ID = ObjSel.GETcmid(cd_Id);
                    Fistbranch = ObjSel.GETBrachAlias(CM_ID);
                    Session["Fistbranch"] = Fistbranch;
                    Session["Branch_HQ"] = cd_BranchAlias;
                    Session["DataPath"] = Datapath;
                    Session["UserId"] = UserId;
                    Session["Cd_Id"] = cd_Id;
                    Session["PortalTypeId"] = PortalTypeId;
                    string IsMultiBranch = DtEnduer.Rows[0]["IsMultiBranch"].ToString();
                    Session["IsMultiBranch"] = IsMultiBranch;
                    string IsSuperAdmin = DtEnduer.Rows[0]["IssuperAdmin"].ToString();
                    Session["IsSuperAdmin"] = IsSuperAdmin;
                    Session["UserName"] = txtUserName.Text;
                    Session["Password"] = txtPassword.Text;





                    // Chk with License Detatils 
                    DtLicense = ObjUserBL.GetLicenseDetails(PortalTypeId);

                    if (DtLicense.Rows.Count > 0)
                    {
                        L_MaxUserCnt = DtLicense.Rows[0]["UserCnt"].ToString();
                        L_RetailDashboard = DtLicense.Rows[0]["RETAILDASHBOARD"].ToString();
                        L_SummarySales = DtLicense.Rows[0]["SUMMARYSALES"].ToString();
                        L_ItemSales = DtLicense.Rows[0]["ITEMSALE"].ToString();
                        L_TaxAnalysis = DtLicense.Rows[0]["TAXANALYSIS"].ToString();
                        L_TopSale = DtLicense.Rows[0]["TOPSALE"].ToString();
                        L_MothlySales = DtLicense.Rows[0]["MONTHLYSALE"].ToString();
                        L_HourlySales = DtLicense.Rows[0]["HOURLYSALE"].ToString();
                        L_GroupSale = DtLicense.Rows[0]["GROUPSALE"].ToString();
                        // Create Session for License Variables 
                        Session["L_MaxUserCnt"] = L_MaxUserCnt;
                        Session["L_RetailDashboard"] = L_RetailDashboard;
                        Session["L_SummarySales"] = L_SummarySales;
                        Session["L_ItemSales"] = L_ItemSales;
                        Session["L_TaxAnalysis"] = L_TaxAnalysis;
                        Session["L_TopSale"] = L_TopSale;
                        Session["L_MothlySales"] = L_MothlySales;
                        Session["L_HourlySales"] = L_HourlySales;
                        Session["L_GroupSale"] = L_GroupSale;

                    }

                    ActiveUserid = ObjMain.ExecuteScalar("SELECT Active from portaldb.usermaster  where userid=" + UserId + " ");
                    if (ActiveUserid == "")
                    {

                        // ActiveUserUPT(UserId);                 // Update in UserMaster Active - as 1
                        Response.Redirect("Dashboard.aspx" , false);
                    }
                    else if (Convert.ToBoolean(ActiveUserid) != true)
                    {
                        // ActiveUserUPT(UserId);                 // Update in UserMaster Active - as 1
                        Response.Redirect("Dashboard.aspx" , false);
                    }
                    else
                    {
                        Response.Redirect("Test.aspx" , false);
                    }

                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Credential Wrong or Invalid User !! ');location.href='Login.aspx'", true);
                }

            }

            catch (Exception ex)
            {
                ObjLog.Write(ex);            
            }

        }
        /// <summary>
        ///  This function for Avoid user : MultiUser access on Same Login credential 
        /// </summary>
        /// <param name="Userid_Active"></param>
        public void ActiveUserUPT(string Userid_Active)
        {
            string Val="1";
            K = ObjMain.ExecuteNonQuery("UPDATE   portaldb.UserMaster set  Active=" + Val + "  where userid=" + Userid_Active + "");
        
        }





    }
}