using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataOper;
using System.Data;
public partial class admin_admin : System.Web.UI.MasterPage
{
    DataOperation dOper = new DataOperation();
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["User_ID"] == null && Request.Cookies["User_ID"] == null)
        {
            Response.Redirect("logout.aspx");
        }
        else if (Session["User_ID"] == null && Request.Cookies["User_ID"] != null)
        {
            HttpCookie cookieemp_id = Request.Cookies["User_ID"];
            Session["User_ID"] = cookieemp_id.Value;
        }

        DataTable dtCheck = dOper.return_dt("select * from tbl_users where u_id='" + Convert.ToString(Session["User_ID"]) + "'");
        if (dtCheck.Rows.Count == 0)
        {
            Response.Redirect("logout.aspx");
        }

        DataTable dtPermission = dOper.return_dt("select mp.role_type,mp.menu_name,mp.menu_sub_name from tbl_menu_permission mp, tbl_menu m where mp.menu_name=m.menu_name and mp.menu_sub_name='' and mp.role_type='" + Convert.ToString(dtCheck.Rows[0]["u_role"]) + "' and (m.page_name='" + GetCurrentPageName().Replace("'", "") + "' or m.page_name1='" + GetCurrentPageName().Replace("'", "") + "' or m.page_name2='" + GetCurrentPageName().Replace("'", "") + "' or m.page_name3='" + GetCurrentPageName().Replace("'", "") + "') union select mp.role_type,mp.menu_name,mp.menu_sub_name from tbl_menu_permission mp, tbl_menu_sub ms where mp.menu_name=ms.menu_name and mp.menu_sub_name=ms.menu_sub_name and mp.menu_sub_name<>'' and mp.role_type='" + Convert.ToString(dtCheck.Rows[0]["u_role"]) + "' and (ms.page_name='" + GetCurrentPageName().Replace("'", "") + "' or ms.page_name1='" + GetCurrentPageName().Replace("'", "") + "' or ms.page_name2='" + GetCurrentPageName().Replace("'", "") + "' or ms.page_name3='" + GetCurrentPageName().Replace("'", "") + "')");
        if (dtPermission.Rows.Count == 0 && GetCurrentPageName().ToLower() != "change_password.aspx")
        {
            Response.Redirect("logout.aspx");
        }

        PopMenu(Convert.ToString(dtCheck.Rows[0]["u_role"]));
        litMUser.Text = "Welcome : " + Convert.ToString(dtCheck.Rows[0]["name"]);

        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    public void PopMenu(string role_type)
    {
        string str_menu = "";
        DataTable dtMenu = dOper.return_dt("select * from tbl_menu where menu_name in (select distinct menu_name from tbl_menu_permission where role_type='" + role_type + "') order by order_no");

        for (int i = 0; i < dtMenu.Rows.Count; i++)
        {
            string m_css = "href='javascript:void(0);'";
            string str_sub_menu = "";
            if (GetCurrentPageName() == Convert.ToString(dtMenu.Rows[i]["page_name"]))
            {
                m_css = "href='" + Convert.ToString(dtMenu.Rows[i]["page_name"]) + "' class='active'";
            }
            else if (Convert.ToString(dtMenu.Rows[i]["page_name"]) != "")
            {
                m_css = "href='" + Convert.ToString(dtMenu.Rows[i]["page_name"]) + "'";
            }
            else
            {
                m_css = "href='javascript:void(0);' class='accordion'";
                string ms_css = "";
                string sub_menu = "";
                DataTable dtSubMenu = dOper.return_dt("select * from tbl_menu_sub where menu_name='" + Convert.ToString(dtMenu.Rows[i]["menu_name"]) + "' and menu_sub_name in (select distinct menu_sub_name from tbl_menu_permission where role_type='" + role_type + "') order by order_no");
                for (int j = 0; j < dtSubMenu.Rows.Count; j++)
                {
                    if (GetCurrentPageName() == Convert.ToString(dtSubMenu.Rows[j]["page_name"]) || GetCurrentPageName() == Convert.ToString(dtSubMenu.Rows[j]["page_name1"]) || GetCurrentPageName() == Convert.ToString(dtSubMenu.Rows[j]["page_name2"]) || GetCurrentPageName() == Convert.ToString(dtSubMenu.Rows[j]["page_name3"]))
                    {
                        m_css = "href='javascript:void(0);' class='accordion active'";
                        ms_css = "panel_active";
                        sub_menu = sub_menu + "<li class='li_active'><a href='" + Convert.ToString(dtSubMenu.Rows[j]["page_name"]) + "'>" + Convert.ToString(dtSubMenu.Rows[j]["menu_sub_name"]) + "</a></li>";
                    }
                    else
                    {
                        sub_menu = sub_menu + "<li><a href='" + Convert.ToString(dtSubMenu.Rows[j]["page_name"]) + "'>" + Convert.ToString(dtSubMenu.Rows[j]["menu_sub_name"]) + "</a></li>";
                    }
                }
                str_sub_menu = str_sub_menu + "<div class='panel " + ms_css + "'><ul class='accordion_menu'>" + sub_menu + "</ul></div>";
            }
            str_menu = str_menu + "<a " + m_css + " >" + Convert.ToString(dtMenu.Rows[i]["menu_name"]) + "</a>" + str_sub_menu;
        }
        litMenu.Text = str_menu;
    }

    public string GetCurrentPageName()
    {
        string sPath = Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        //sRet = sRet + Request.Url.Query;
        return sRet;
    }
}
