using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Business.System.Op.Entity;
using Business.System.Op.Vo;
using Common;
using LogisticsSystem.Control;
using LogisticsSystem.Control.Helper;


namespace LogisticsSystem.SysMain.Setting
{
    public partial class LimitSrv : SrvBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var type = Request.QueryString["type"];

            if (!CheckUserLogin()) return;

            if (!CheckUserDo("用户执行--权限管理", type))
            {
                DataRow[] row = InitHelper.Ops.Select("code='" + type + "'");
                if (row.Length <= 0)
                {
                    Response.Write("用户没有权限操作");
                    return;
                }
                Response.Write("用户没有权限操作:" + row[0]["name"]);
                return;
            }

            //根据类型 执行相关操作
            switch (type)
            {
                case "GetOpList":           //获取权限列表
                    GetOpList();
                    break;
                case "UpdateLmt":           //更新权限信息
                    UpdateLmt();
                    break;
                case "GetSysLog":          //查看系统日志
                    GetSysLog();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }

        private void GetOpList()
        {
           //获取数据
            ArrayList op = new OpVo().OpList("select * from SysOp order by parentcode");
            var html=new StringBuilder();       //返回的html内容
            var first = new ArrayList();       //保存第一级操作
            var second = new ArrayList();      //保存第二级操作
            var third = new ArrayList();       //保存第三级操作
            string flag = string.Empty;
            string lmt = new LmtVo().GetLmt(Request.QueryString["id"]); //获取用户权限
            try
            {
                //获取第一级操作
                foreach (OpEty opEty in op)
                {
                    if (opEty.ParentCode == "--")
                    {
                        first.Add(opEty);
                        flag += opEty.Code + ",";
                    }
                }
                //获取第二和第三级操作
                foreach (OpEty opEty in op)
                {
                    if (opEty.ParentCode == "--")continue;
                    if (flag.Contains(opEty.ParentCode))
                    {
                        second.Add(opEty);
                    }
                    else
                    {
                        third.Add(opEty);
                    }
                }
                //建立权限列表
                html.Append("<div id=\"lmtList\" style=\"height:500px; overflow:auto; text-align:left; padding-left:5%;\">");
                foreach (OpEty opEty in first)
                {
                    html.Append("<div><h4 class=\"lmtListTitle\">" + opEty.Name + "</h4><ul>");
                    foreach (OpEty sopEty in second)
                    {
                        if (sopEty.ParentCode == opEty.Code)
                        {
                            html.Append("<li><h6 class=\"lmtListTitle\">" + sopEty.Name + "</h6><ul>");
                            foreach (OpEty topEty in third)
                            {
                                if (topEty.ParentCode == sopEty.Code)
                                {
                                    if (lmt.Contains(topEty.Code) || lmt=="**")
                                    {
                                        html.Append("<li>" + topEty.Name + "<input type=\"checkbox\" value=\"" +
                                                    topEty.Code + "\"></li>");
                                    }
                                    else
                                    {
                                        html.Append("<li>" + topEty.Name + "<input checked type=\"checkbox\" value=\"" + topEty.Code + "\"></li>");
                                    }
                                }
                            }
                            html.Append("</ul></li>");
                        }
                    }
                    html.Append("</ul></div>");
                }
                html.Append("</div><input id=\"lmtSave\" type=\"button\" value=\"保存\" />");
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
                return;
            }
           
            Response.Write(html.ToString());
           
        }

        /// <summary>
        /// 修改用户权限
        /// </summary>
        private void UpdateLmt()
        {
            var result = new LmtVo().UpdateLmt(Request.QueryString["id"], Request.QueryString["ops"]);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 获取日志表头
        /// </summary>
        /// <returns>日志表头</returns>
        private StringBuilder LogHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\"><thead>");
            tableHead.Append("<td>时间</td>");
            tableHead.Append("<td>用户姓名</td>");
            tableHead.Append("<td>执行操作</td></thead>");
            return tableHead;
        }


        private void GetSysLog()
        {
            if (Request.QueryString["sT"]!="")
            {
                try
                {
                    Convert.ToDateTime(Request.QueryString["sT"]);
                }
                catch (Exception)
                {
                    Response.Write("请输入正确的时间格式 如:2014-12-05");
                    return;
                }
            }
            
            //获取表头
            StringBuilder table = LogHead();
            //获取用户数据
            DataTable data = new LmtVo().GetSysLog(Request.QueryString["sT"], Request.QueryString["sN"]);
            //整合数据表
            NoCheckTable(table, data);
            //返回数据
            Response.Write(table.ToString());
        }
    }
}