<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Config" %>
<%@ Import Namespace="Kesion.Components" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="System.Data" %>
<script runat="server" type="text/C#">
    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    bool isLoadPage = true;  //定义页面是否加载

    private string action = string.Empty;
    private int Id = 0;

    int recordcount, pagecount;
    int CurrPage = new BasicField().GetCurrPage;
    int PageSize = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        action = KSCMS.S("action");
        if (string.IsNullOrEmpty(action))
        {
            action = KSCMS.S("do");
        }
        Id = Utils.StrToInt(BasicField.GetParam("id"));

        action = action.ToLower();
        switch (action)
        {
            case "delete":
                isLoadPage = false;
                DoDelete();
                break;
            case "deletetf":
                isLoadPage = false;
                DoDeleteTF();
                break;
            case "turntomessage":
                isLoadPage = false;
                TurnToMessage();
                break;
        }



    }
    protected void DoDelete()
    {
        if (KSCMS.S("ids") != "")
        {
            string ids = KSCMS.S("ids").Replace(" ", "");

             using (DataTable dt = DataFactory.GetDataTable("SELECT * [KS_Message] Where ToUserName='"+__user.UserName+"' and id in("+Utils.FilterIDs(ids)+")"))
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string userName = dr["ToUserName"].ToString();
                        DataFactory.ExecuteNonQuery("DELETE FROM [KS_Message] Where ID=" + dr["id"].ToString());
                        Kesion.Cache.CommonJsonCache.CleanMessageNum(userName);    //更新用户新消息数缓存
                    }
                }
            }

        }
        Msg.Alert("恭喜，删除成功!", KSCMS.S("flag")=="message"?BasicField.GetUserUrl("message/Garbagemessage"):KSCMS.GetComeUrl());
    }
    protected void DoDeleteTF()
    {
        if (KSCMS.S("ids") != "")
        {
            string ids = KSCMS.S("ids").Replace(" ", "");
            string[] ids_arr = ids.Split(',');
            Hashtable param = new Hashtable();
            foreach (string id in ids_arr)
            {
                param.Add("deltf",0);
                param.Add("tousername", __user.UserName);
                CommonData.Update("[KS_Message]", param, "where id=" + id);
                Kesion.Cache.CommonJsonCache.CleanMessageNum(__user.UserName);    //更新用户新消息数缓存
                param.Clear();
            }
        }
        Msg.Alert("恭喜，已设置为非垃圾消息!", KSCMS.S("flag")=="message"?BasicField.GetUserUrl("message/Garbagemessage"):KSCMS.GetComeUrl());

    }
    private void TurnToMessage()
    {
        Hashtable param = new Hashtable();
        param.Add("deltf", 0);
        CommonData.Update("[KS_Message]", param, "where id=" +Id);
        Kesion.Cache.CommonJsonCache.CleanMessageNum(__user.UserName);    //更新用户新消息数缓存
        Response.Redirect(KSCMS.GetComeUrl());
    }


</script>

<%
    if (!isLoadPage) return;
%>

<div class="r_tybox nomar t_tybox">
    <div class="r_title tytitle pd0">
      <h4 class="ty-none">站内信息</h4>
    	<div class="tabs">  
          <ul>
              <%
                  Response.Write(@"<li class=""puton""><a href=""" + BasicField.GetUserUrl("message/Receivedmessage") + @""">站内消息</a></li>");
                  Response.Write(@"<li><a href=""" + BasicField.GetUserUrl("other/mychatinfo") + @""">我的咨询</a></li>");
                  Response.Write(@"<li><a href=""" + BasicField.GetUserUrl("other/comment") + @""">我的评价</a></li>");
                  if (CommonJsonCache.CheckYunAPPHasAllow("plus.ask"))
                  {
                      Response.Write(@"<li><a href=""" + BasicField.GetUserUrl("other/MyAsk") + @""">问题互动</a></li>");
                  }
                  if (BaseFun.CheckModulePower("edu"))
                  {
                      Response.Write(@"<li><a href=""" + BasicField.GetUserUrl("Course/MyCosZan", "optype") + @""">我的点赞</a></li>");
                      Response.Write(@" <li><a href=""" + BasicField.GetUserUrl("Course/MyInteractRecord") + @""">互动礼物</a></li>");
                  }
                 %>
          </ul>
        </div>    
    </div> 
    <div class="top-sort clearfix">
        <div class="top-sort-tab">	
            <ul>
                <li><a href="<%=BasicField.GetUserUrl("message/receivedmessage") %>">我收到的消息</a></li>
                <li><a href="<%=BasicField.GetUserUrl("message/sentmessage") %>">我发出的消息</a></li>
                <li class="puton"><a href="<%=BasicField.GetUserUrl("message/Garbagemessage") %>">垃圾消息</a></li>
                <li><a href="<%=BasicField.GetUserUrl("message/Sendmessage") %>">发送短消息</a></li>
            </ul>
        </div> 
     </div>        
  <%
      if (string.IsNullOrEmpty(action))
      {
          Kesion.APPCode.EditorAPI.EchoHead();

          StringBuilder sb = new StringBuilder();
          using (DataTable dt = DataFactory.GetPage("id,MsgTitle,FromUserName,MsgContactMan,MsgTel,AddDate,ReadTf", "[KS_Message] where DelTF=1 and ToUserName='"+__user.UserName+"'", "id", "order by id desc",  CurrPage, PageSize, out recordcount, out pagecount))
          {
              if (dt.Rows.Count > 0)
              {
                  foreach (DataRow dr in dt.Rows)
                  {
                      sb.AppendLine(@" <tr class=""tdbg"">
						  <td class=""splittd"" align=""center""><input type=""checkbox"" name=""ids"" value=""" + dr["ID"].ToString() + @""" /></td>
						  <td class=""splittd""><a href=""" + BasicField.GetUserUrl("message/Garbagemessage", "Action-Show,ID-" + dr["ID"].ToString()) + @""">" + dr["MsgTitle"].ToString() + @"</a>
						  <br /><span class=""tips"">" + Utils.FormatHumanizedTime(Convert.ToDateTime(dr["AddDate"].ToString())) + @"</span>
						  </td> 
						  <td class=""splittd"" style=""text-align:center"">" + dr["FromUserName"].ToString() + @"</td>
						  <td class=""splittd"" align=""center""><a href='" + BasicField.GetUserUrl("message/Garbagemessage", "Action-turntomessage,ID-" + dr["ID"].ToString()) + @"'>转到“收到的消息”</a>
						  </td>
					  </tr> ");

                  }

              }
              else
              {

                  sb.Append(@" <tr class=""tr-nobor"">
					<td class=""splittd"" style=""text-align:center"" colspan=""4"">
					 <div class=""empty"">没有发出消息！</div> 
					</td>
				  </tr><style>.cz_btn{display:none;}</style>");

              }
          }
    %>

    <form name="myform" action="{$GetCurrUrl}" method="post" id="myform">
        <input type="hidden" name="action" id="action" value="delete"/>
        <table id="tablist" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
            <tr align="center" class="title">
                <td width="30" align="center" class="td-first">选择</td>
                <td align="left">标题</td>
                <td align="center">发自</td>
                <td align="center" class="td-last">操作</td>
            </tr>
            <%=sb.ToString() %>
        </table>
         <div class="cz_btn">
              <div class="f-left">
                  <span class="check-all"><input type="checkbox" name="chkall" class="checkbtn" id="chkall" onclick="CheckAll(this.form);"/>全选</span>
                <span><input type="submit" ID="ButtonDel" class="green-bt bt-auto button" value="永久删除" onmousedown="return(GetIDS());" onclick="return (setAction('delete', '永久删除'))"/></span>
              </div>
        </div>
        <div class="fy_box">
			<%
            #region 分页
              string PageStr=__user.GroupID==3||__user.GroupID==4?"[KS:PageStr1]": "[KS:PageStr2]";
            BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
            KSCMS.W(PageStr);
            #endregion
             %>
        </div>
  
        <script>
            function setAction(a,t) {
                $("#action").val(a);
                return (confirm('确定将选中的消息' + t + '吗?'));
            }
        </script>
        <div class="clear"></div>
    </form>

	<%}
    else if (action == "show")
    {

        using (DataTable dt = DataFactory.GetDataTable("select top 1 * From KS_Message Where ID=" + Id+ " and ToUserName='" + __user.UserName + "'"))
        {
            if (dt.Rows.Count <= 0)
            {
                Msg.Alert("找不到记录！", KSCMS.GetComeUrl());
                return;
            }
            else
            {
                DataRow dr = dt.Rows[0];

         %>

    <table width="98%" border="0" align="center" class="border" cellpadding="0" cellspacing="0">
      <tr class="title">
        <td>我收到的留言</td>
      </tr>
    </table>
    <table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="border messageTable">
      <tr class="tdbg">
        <td class="splittd" style="word-break:break-all;text-align:left">
            <b>主题：</b><%= dr["MsgTitle"].ToString() %><br />
        	<b>来自：</b><%=dr["FromUserName"].ToString() %><br />
         	<strong>时间：</strong><%=dr["AddDate"].ToString() %>
            <hr size="1" />
            <div class="messageOrder">
				<%= Utils.HtmlDecode(dr["MsgContent"].ToString()) %>
            </div>
            
          </td>
      </tr>
    </table>
 <div style="margin-top:20px;text-align:center">
 	<div class="cz_btn">
       <form name="myform" action="{$GetCurrUrl}" method="post" id="myform">
       	<input type="hidden" name="action" id="action" value="delete"/>
       	<input type="hidden" name="ids" value="<%=Id %>"/>
       	<input type="hidden" name="flag" value="message"/>
    	<input type="submit" ID="ButtonDel" class="button" value="永久删除"  onclick="return (setAction('delete', '永久删除'))"/>
       	<input type="submit" ID="ButtonDelTF" class="button"  value="转至＂收到的消息＂" onclick="return(setAction('deletetf','转至非垃圾消息'))"/>
        <input type="button" class="button" value=" 返 回 " onclick="history.back()" />
       </form>
          <script>
			function setAction(a,t) {
				$("#action").val(a);
				return (confirm('确定将选中的消息' + t + '吗?'));
			}
		  </script>
    </div>         
 </div>




<%}
        }
    }

    %>
    

    </div>