using System;
using DeiNiu.wms.Data.Model;
using System.Data;
using System.Collections;
using System.Collections.Generic;
namespace DeiNiu.wms.Logical
{
    [Serializable]
    public class LAuthority
    {
        Authority _obj;
        public LAuthority()
        {
           
            Initialize();
        }
        public Authority GetAuthority
        {
            get
            {
                return _obj;
            }
        }
		
		/// 
		/// get all data
		/// 
        public DataSet GetAllData()
        {
           return _obj.Query();
        }
        /// 
        /// get all data
        /// 
        public DataSet getAllActiveData()
        {
            return _obj.QueryActived();
        }
/*
        private void leavetypeDsConstruct()
        {
            return;
        }
*/
		
		/// 
		/// get a record by id
		/// 
        public void Initialize(int id)
		{
            _obj = id != 0 ? new Authority(id) : new Authority();
		}
        /// 
		/// get a record by id 0
		/// 
        public void Initialize()
        {
            Initialize(0);
        }
        public List getAuthorities()
        {
            List auths = new List();
            DataTable dt = getAllActiveData().Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                int id = Convert.ToInt32(row["id"].ToString());
                Authority ath = new Authority(id);
                auths.Add(ath);
            }
            return auths;
        }
        public List getAuthorities(int empId, int warehouse)
        {
            
            List auths = new List();
            RoleAuthority  ll = new RoleAuthority();
            DataTable dt = ll.GetAuthByEm(empId, warehouse);
            foreach (DataRow row in dt.Rows)
            {
                int id = Convert.ToInt32(row["id"].ToString());
                Authority ath = new Authority(id);
                auths.Add(ath);
            }
            return auths;
        }
        public Dictionary> getMobileAuthorities(int empId)
        {
            Dictionary> auths = new Dictionary>();
            // DataView dv = new DataView(getAllActiveData().Tables[0]);
            DataView dv = _obj.queryCateAuths().DefaultView;
            List subAuths = getAuthorities(empId,-1); 
           foreach (Authority a in subAuths)
             {
                 dv.RowFilter = "id =" + a.auth_uplevel;
                 string key;
                 foreach (DataRowView dr in dv)
                 {
                     key = dr["auth_name"].ToString();
                     if (auths.ContainsKey(key))
                     {
                         auths[key].Add(a);
                     }
                     else
                     {
                         List sa = new List();
                         sa.Add(a);
                         auths.Add(key, sa);
                     }
                 }
             }
             
            return auths;
        }
      // public Dictionary> getCatedAuthorities(int empId,int warehouse=-1)
        public Dictionary> getCatedAuthorities(int empId,int warehouse=-1)
        {
            Dictionary> auths = new Dictionary>();
           // DataView dv = new DataView(getAllActiveData().Tables[0]);
            DataView dv = _obj.queryCateAuths().DefaultView;
            List subAuths = getAuthorities(empId,  warehouse );
           /*
            * foreach (Authority a in subAuths)
            {
                dv.RowFilter = "id =" + a.auth_uplevel;
                string key;
                foreach (DataRowView dr in dv)
                {
                    key = dr["auth_name"].ToString();
                    if (auths.ContainsKey(key))
                    {
                        auths[key].Add(a);
                    }
                    else
                    {
                        List sa = new List();
                        sa.Add(a);
                        auths.Add(key, sa);
                    }
                }
            }
            */
             
                foreach (DataRowView dr in dv)
                {
                    string formexists = dr["auth_class"].ToString();
                    string special = dr["auth_special"].ToString();
                    if (string.IsNullOrEmpty(formexists) 
                        && string.IsNullOrEmpty(special)
                        )
                    {
                        continue;
                    }
                    List sa = new List();
                    string key = dr["auth_name"].ToString();
                    auths.Add(key, sa);
                    foreach (Authority a in subAuths)
                    {
                        if (a.auth_uplevel == Convert.ToInt16(dr["id"].ToString()))
                        {  
                         //  string  key = dr["auth_name"].ToString();
                            if (auths.ContainsKey(key))
                            {
                                auths[key].Add(a);
                            }
                            else
                            {
                              //  List sa = new List();
                              //  sa.Add(a);
                              //  auths.Add(key, sa);
                            }
                        }
                    }
                }
             
            return auths;
        }
        bool validUser(string userId,string passwd)
        {
            Employee emp = new Employee();
            return emp.login(userId,passwd);
        }
    }
   
}