using PageAdmin.BLL;using PageAdmin.Filter;
using PageAdmin.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class MsgInboxController : BaseController
    {
        public MsgInboxController():base("pa_msg_receiver", "Work")
        {
            base.MvcContext.SqlCondition="uid=" + base.CurrentUser.Uid;
            base.MvcContext.ListOrder ="id desc";
            base.ListDataExecuting = new Action(SelectUid);
        }

        public string GetMsg()
        {
            var form = Request.QueryString;
            int id = StringHelper.Format<int>(form["id"]);
            DataTable dt=PA.GetDataTable("pa_msg", "title,uid,thedate", "id =" + id);
            return Json(dt);
        }

        public ActionResult Tabhead()
        {
            return View();
        }
        public void SelectUid()
        {
            var form = System.Web.HttpContext.Current.Request.QueryString;
            #region 用户名搜索
            string username = StringHelper.Format<string>(form["uid"]);
            DataTable dt = new DataTable();
            string ids = "";
            if (username!="")
            {
                string uid =DbContext.GetField("pa_member", "id", "username like '%" + username + "%'");
                if (uid != null)
                {
                    dt = PA.GetDataTable("pa_msg", "id", "uid=" + int.Parse(uid));
                    ids = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        ids += dr["id"].ToString() + ",";
                    }
                    base.MvcContext.SqlCondition += " and msg_id in (" + ids.Substring(0, ids.Length - 1) + ")";
                }
                else
                {
                    base.MvcContext.SqlCondition += " and msg_id in (0)";
                }
            }
            #endregion
            #region 标题搜索
            string title = StringHelper.Format<string>(form["title"]);
            if (title!="")
            {
                dt = PA.GetDataTable("pa_msg", "id", "title like '%" + title + "%'");
                if (dt.Rows.Count > 0)
                {
                    ids = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        ids += dr["id"].ToString() + ",";
                    }
                    base.MvcContext.SqlCondition += " and msg_id in (" + ids.Substring(0, ids.Length - 1) + ")";
                }
                else
                {
                    base.MvcContext.SqlCondition += " and msg_id=0";
                }
            }
            #endregion
            #region 日期搜索
            string startthedate = StringHelper.Format<string>(form["startthedate"]);
            string endthedate = StringHelper.Format<string>(form["endthedate"]);
            DateTime dtDate;
            if (DateTime.TryParse(startthedate, out dtDate)==true && DateTime.TryParse(endthedate, out dtDate) == true)
            {
                if (PA.DbType == "Access")
                {
                    dt = PA.GetDataTable("pa_msg", "id", " thedate between #" + Convert.ToDateTime(startthedate) + "# and #" + Convert.ToDateTime(endthedate) + "#");
                }
                else
                {
                    dt = PA.GetDataTable("pa_msg", "id", " thedate between '" + Convert.ToDateTime(startthedate) + "' and '" + Convert.ToDateTime(endthedate) + "'");
                }
                if (dt.Rows.Count > 0)
                {
                    ids = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        ids += dr["id"].ToString() + ",";
                    }
                    base.MvcContext.SqlCondition += " and msg_id in (" + ids.Substring(0, ids.Length - 1) + ")";
                }
                else
                {
                    base.MvcContext.SqlCondition += " and msg_id in (0)";
                }
            }
            #endregion
        }
    }
}