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

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class MsgoutBoxController : BaseController
    {
        public MsgoutBoxController() : base("pa_msg", "Work")
        {
            base.MvcContext.SqlCondition = "uid=" + base.CurrentUser.Uid + " and is_show=1";
            base.MvcContext.ListOrder = "id desc";
            base.ListDataExecuting = new Action(SelectUid);
            base.AddExecuted = new Action(OnAddExecutingAction);
        }

        public void OnAddExecutingAction()
        {
            var form = base.MvcContext.Data;
            string receiver = StringHelper.Format<string>(form["receiver"]);
            string guid = StringHelper.Format<string>(form["guid"]);
            int? msg_id = (int?)DbContext.GetField("pa_msg", "id", "guid='" + guid + "'");
            TipsInfo tipsInfo = base.MvcContext.TipsInfo;
            if (receiver == "" || msg_id == null)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "填写数据有误，请重新填写！";
                return;
            }
            foreach (var username in receiver.Split(','))
            {
                Dictionary<string, dynamic> dictionary = new Dictionary<string, dynamic>();
                int? uid = (int?)DbContext.GetField("pa_member", "id", "username='" + username + "'");
                if (uid != null)
                {
                    dictionary.Add("uid", uid);
                    dictionary.Add("msg_id", msg_id);
                    dictionary.Add("readed", 0);
                    DbContext.Insert("pa_msg_receiver", dictionary);
                }

            }

        }
        public string GetReceiverUid()
        {
            var form = Request.QueryString;
            int msg_id = StringHelper.Format<int>(form["msg_id"]);
            IDataReader dr = DbContext.GetReader("pa_msg_receiver", "uid", "msg_id=" + msg_id);
            string ids = "";
            while(dr.Read())
            {
                ids += DbContext.GetField("pa_member", "username", "id=" + int.Parse(dr["uid"].ToString())) + ",";
            }
            dr.Close();
            return ids.Substring(0, ids.Length - 1);
        }
        public void SelectUid()
        {
            var form = System.Web.HttpContext.Current.Request.QueryString;
            string username = StringHelper.Format<string>(form["uid"]);
            if (username != "")
            {
                string uid = (string)DbContext.GetField("pa_member", "id", "username like '%" + username + "%'");
                if (uid != null)
                {
                    IDataReader dr = DbContext.GetReader("pa_msg_receiver", "msg_id", "uid=" + int.Parse(uid));
                    string ids = "";
                    while(dr.Read())
                    {
                        ids += dr["msg_id"].ToString() + ",";
                    }
                    dr.Close();
                    base.MvcContext.SqlCondition += " and id in (" + ids.Substring(0, ids.Length - 1) + ")";
                }
                else
                {
                    base.MvcContext.SqlCondition += " and id in (0)";
                }
            }
        }
    }
}