    <%@ 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 "readtf"://设置为已读
                isLoadPage = false;
                DoReadTF();
                break;
        }
    }
    protected void DoReadTF()
    {
        #region 设置为已读
        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("ReadTF", 1);
                param.Add("tousername", __user.UserName);
                CommonData.Update("[KS_Message]", param, "where ReadTF=0 AND  id=" + id);

                Kesion.Cache.CommonJsonCache.CleanMessageNum(__user.UserName);    //更新用户新消息数缓存
                param.Clear();
            }
            Msg.Alert("恭喜，已设置为已读消息!", KSCMS.S("flag")=="message"?BasicField.GetUserUrl("message/receivedmessage"):KSCMS.GetComeUrl());

        }

        #endregion
    }
    protected void DoDelete()
    {
        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("id", id);
                param.Add("tousername", __user.UserName);
                CommonData.Delete("[KS_Message]",param);
                Kesion.Cache.CommonJsonCache.CleanMessageNum(__user.UserName);    //更新用户新消息数缓存
                param.Clear();
            }
        }
        Msg.Alert("恭喜，删除成功!", KSCMS.S("flag")=="message"?BasicField.GetUserUrl("message/receivedmessage"):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", 1);
                param.Add("tousername", __user.UserName);
                CommonData.Update("[KS_Message]", param, "where id=" + id);
                Kesion.Cache.CommonJsonCache.CleanMessageNum(__user.UserName);    //更新用户新消息数缓存
                param.Clear();
            }
        }
        Response.Redirect(KSCMS.S("flag") == "message" ? BasicField.GetUserUrl("message/receivedmessage") : KSCMS.GetComeUrl());
       // Msg.Alert("恭喜，已设置为垃圾消息!", KSCMS.S("flag")=="message"?BasicField.GetUserUrl("message/receivedmessage"):KSCMS.GetComeUrl());

    }
    public string GetMsgStatus(int ReadTF)
    {
        if (ReadTF == 0)
            return "<font color=red>未读</font>";
        else if (ReadTF == 1)
            return "<font color=#999999>已读</font>";
        else
            return "<font color=#cccccc>已回复</font>";

    }


</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 class="puton"><a href="<%=BasicField.GetUserUrl("message/receivedmessage") %>">我收到的消息</a></li>
                <li><a href="<%=BasicField.GetUserUrl("message/sentmessage") %>">我发出的消息</a></li>
                <li><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=0 and ToUserName='" + (new TemporaryVar()).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"" class=""checkbtn"" name=""ids"" value=""" + dr["ID"].ToString() + @""" /></td>
										  <td class=""splittd"" style=""text-align:left""><a href=""" + BasicField.GetUserUrl("message/receivedmessage", "Action-Show,ID-" + dr["ID"].ToString()) + @"""><img src=""/sysimg/default/msg" + dr["ReadTF"].ToString() + @".gif"" />" + dr["MsgTitle"].ToString() + @"</a>
										  <br /><span style=""color:#999"">" + 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"">" +
													  GetMsgStatus(Convert.ToInt16(dr["ReadTF"].ToString())) + @"
										  </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 class="title">
                <td width="40" align="center">选择</td>
                <td style="text-align:left"> 标题</td>
                <td align="center" width="150">发自</td>
                <td align="center" width="70">状态</td>
          </tr>
          <%=sb.ToString() %>
        </table>
        <div class="cz_btn">
            <div class="f-right">
                <span class="check-all"><label><input type="checkbox" class="checkbtn mt5" name="chkall"  id="chkall" onclick="CheckAll(this.form);"/>全选</label></span>
                <span><input type="submit" ID="ButtonDel" class="grey-bt bt-w-auto button" value="永久删除" onmousedown="return(GetIDS());" onclick="return (setAction('delete', '永久删除'))"/></span>
                <span><input type="submit" ID="ButtonDelTF" class="grey-bt bt-w-auto button"  value="转为垃圾消息" onmousedown="return(GetIDS());" onclick="return(setAction('deletetf','转为垃圾'))"/></span>
                <span><input type="submit" ID="ButtonReadTF" class="grey-bt bt-w-auto button"  value="设置为已读"  onmousedown="return(GetIDS());" onclick="return(setAction('readtf','设置为已读'))"/></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>
        <div class="clearfix"></div>
        <script>
            function setAction(a,t) {
                $("#action").val(a);
                return (confirm('确定将选中的消息' + t + '吗?'));
            }
        </script>
    </form>
	<%}
        else if (action == "show"){

            using (DataTable dt = DataFactory.GetDataTable("select top 1 * From KS_Message Where ID=" + Id+ " and ToUserName='" + new TemporaryVar().UserName + "'")){
                if (dt.Rows.Count <= 0){
                    Msg.Alert("找不到记录！", KSCMS.GetComeUrl());
                    return;
                }else{
                    DataRow dr = dt.Rows[0];
                    #region 更新标志已读
                    Hashtable ht=new Hashtable();
                    ht.Add("ReadTF",1);
                    CommonData.Update("[KS_Message]", ht, "where ReadTF=0 and id=" + Id);
                    Kesion.Cache.CommonJsonCache.CleanMessageNum(new TemporaryVar().UserName);
                    #endregion
     %>
                <table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="border">
                    <tr class="tdbg">
                    <td class="lefttd"">主题：</td>
                    <td class="tag_left"><%= dr["MsgTitle"].ToString() %></td>     
                    </tr>	
                    <tr class="tdbg">
                    <td class="lefttd">来自：</td>
                    <td class="tag_left"><%=dr["FromUserName"].ToString() %></td>     
                    </tr>	
                    <tr class="tdbg">
                    <td class="lefttd">时间：</td>
                    <td class="tag_left" style="line-height:20px">
                        <%=dr["AddDate"].ToString() %>
                    </td>     
                    </tr>	
                    <tr class="tdbg">
                    <td class="lefttd">内容：</td>
                    <td class="tag_left" style="line-height:20px">
                        <%= Utils.HtmlDecode(dr["MsgContent"].ToString()) %>
                    </td>     
                    </tr>		
                </table>
 				<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"/>
                        <%if (dr["FromUserName"].ToString() != "系统消息")
                        { %>
                        <span><input type="button" ID="ReplyButtonDel" class="green-bt bt-w-auto button" value="回复"  onclick="location.href = '<%=BasicField.GetUserUrl("message/Sendmessage","id-"+Id)%>';"/></span>
                        <%} %>
                        <span><input type="button" class="grey-bt bt-w-auto button" value="返 回" onclick="history.back()" /></span>
                        <span><input type="submit" ID="ButtonDel" class="grey-bt bt-w-auto button" value="永久删除"  onclick="return (setAction('delete', '永久删除'))"/></span>
                        <span><input type="submit" ID="ButtonDelTF" class="grey-bt bt-w-auto button"  value="转为垃圾消息" onclick="return(setAction('deletetf','转为垃圾'))"/></span>
                    </form>  
  				</div>
   				<div class="clearfix"></div>
				<script>
                    function setAction(a,t) {
                        $("#action").val(a);
                        return (confirm('确定将选中的消息' + t + '吗?'));
                    }
                </script>

<%}
        }
    }

    %>
</div>