<%@ 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="Kesion.UCenter" %>
<%@ Import Namespace="Kesion.XML" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" type="text/C#">

    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息
    bool isLoadPage = true;  //定义页面是否加载

    public static int flag = 0;
    public string postTable = MyCache.GetBBSPostTable();
   
    int recordcount, pagecount;
    int CurrPage = new BasicField().GetCurrPage;
    int PageSize = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        flag = Utils.StrToInt(KSCMS.S("flag"));
       
        if (KSCMS.S("action") == "delete")
        {
            isLoadPage = false;
            DeleteTopic();
            return;
        }
        else if (KSCMS.S("action") == "deleteanswer")
        {
            isLoadPage = false;
            DeleteAnswer();
            return;
        }
       
    }


    private void DeleteTopic()
    {
        #region 删除话题
        if (MyCache.GetBBSCacheConfig(15).Equals("0"))
        {
            Msg.Alert("不允许删除", KSCMS.GetComeUrl());
            return;
        }
        else
        {
            if (string.IsNullOrEmpty(KSCMS.S("ids")))
            {
                Msg.Alert("没有选择要删除的问题!", KSCMS.GetComeUrl());
                return;
            }

            string topicIds = Utils.FilterIDs(KSCMS.S("ids"));
            int boardId = Utils.StrToInt(KSCMS.S("boardId"));
            int delPostNum = 0;
            int delTopicNum = 0;
            using (DataTable dt = DataFactory.GetDataTable("select topicId,boardId,postNum,PostTable From KS_BBSTopic Where username='" + (new TemporaryVar()).UserName + "' AND topicId IN(" + topicIds + ")"))
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {

                        boardId = Utils.StrToInt(dr["boardID"].ToString());
                        int postNum = Utils.StrToInt(dr["postNum"].ToString());
                        string PostTable = dr["PostTable"].ToString();

                        delTopicNum++;
                        delPostNum += postNum;

                        #region 更新到缓存
                        if (Cache["BBSBoard"] != null)
                        {
                            DataTable dtt = (DataTable)Cache["BBSBoard"];
                            DataRow drr = dtt.Rows.Find(boardId);
                            if (drr != null)
                            {
                                drr["TopicNum"] = Utils.StrToInt(drr["TopicNum"].ToString()) - 1;
                                drr["PostNum"] = Utils.StrToInt(drr["PostNum"].ToString()) - postNum;
                                if (Utils.StrToInt(drr["PostNum"].ToString()) < 0) drr["PostNum"] = 0;
                                //dr["TodayNum"] = Utils.StrToInt(dr["TodayNum"].ToString()) + 1;
                                dr.AcceptChanges();
                                dt.AcceptChanges();
                            }
                            dt.Dispose();
                        }
                        #endregion

                        DataFactory.ExecuteNonQuery("Update KS_BBSBoard Set TopicNum=TopicNum-1,PostNum=PostNum-" + postNum + " Where boardId=" + boardId);
                        DataFactory.ExecuteNonQuery("Delete From " + postTable + " Where topicId=" + dr["topicId"].ToString());
                    }
                    DataFactory.ExecuteNonQuery("Delete From KS_BBSTopic Where topicId In(" + topicIds + ")");

                }
            }
            if (delTopicNum > 0)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Utils.GetMapPath("~/config/BBSTopicInfo.xml"));
                if (xmlDoc != null)
                {
                    XmlNode xn = xmlDoc.SelectSingleNode("//topicinfo/item");
                    if (xn != null)
                    {
                        xn.SelectSingleNode("@topicnum").InnerText = (Utils.StrToInt(xn.SelectSingleNode("@topicnum").InnerText) - delTopicNum).ToString();
                        if (Utils.StrToInt(xn.SelectSingleNode("@topicnum").InnerText) < 0) xn.SelectSingleNode("@topicnum").InnerText = "0";
                        xn.SelectSingleNode("@postnum").InnerText = (Utils.StrToInt(xn.SelectSingleNode("@postnum").InnerText) - delPostNum).ToString();
                        if (Utils.StrToInt(xn.SelectSingleNode("@postnum").InnerText) < 0) xn.SelectSingleNode("@postnum").InnerText = "0";
                        xmlDoc.Save(Utils.GetMapPath("~/config/BBSTopicInfo.xml"));
                    }
                }
            }


            Msg.Alert("恭喜，删除成功!", KSCMS.GetComeUrl());
            return;
        }

        #endregion
    }

    private void DeleteAnswer()
    {
        #region 删除回复
        if (MyCache.GetBBSCacheConfig(20).Equals("0"))
        {
            Msg.Alert("对不起，不允许删除自已的回复！", KSCMS.GetComeUrl());
            return;
        }
        else
        {
            string postIds = Utils.FilterIDs(KSCMS.S("ids"));
            int topicId = Utils.StrToInt(KSCMS.S("topicid"));
            if (postIds != "0")
            {


                using (DataTable dt = DataFactory.GetDataTable("select top 1 postTable,ReplyText,boardID From KS_BBSTopic Where TopicID=" + topicId))
                {
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        string postTable = dr["PostTable"].ToString();
                        string ReplyText = dr["ReplyText"].ToString();
                        int boardId = Utils.StrToInt(dr["boardId"].ToString());
                        bool hasDel = false;
                        using (DataTable dtt = DataFactory.GetDataTable("select top 100 * From " + postTable + " Where TopicID=" + topicId + " and PostId in(" + postIds + ")"))
                        {
                            if (dtt.Rows.Count > 0)
                            {
                                foreach (DataRow drr in dtt.Rows)
                                {
                                    string userName = drr["userName"].ToString();
                                    if (userName != UserManage.GetUserInfo().UserName)
                                    {
                                        Msg.Alert("不是您发表的回复，不能删除!", KSCMS.GetComeUrl());  //不是自己的回复不能删除
                                        return;
                                    }
                                    else
                                    {
                                        hasDel = true;
                                        ReplyText = ReplaceBBSFun.RemoveFromReplyText(Utils.StrToInt(drr["postId"].ToString()), ReplyText);
                                        ReplaceBBSFun.DelReplySubScore(drr, boardId); //删除扣积分
                                        //删除
                                        DataFactory.ExecuteNonQuery("Delete From " + postTable + " Where PostId=" + drr["postId"].ToString());
                                    }

                                }
                            }
                        }
                        if (hasDel)
                        {
                            string PostNum = DataFactory.GetOnlyValue("select count(1) From " + postTable + " Where TopicId=" + topicId + " and Verify=1");
                            DataFactory.ExecuteNonQuery("Update KS_BBSTopic Set ReplyText='" + ReplyText.Replace("'", "''") + "',PostNum=" + PostNum + " Where TopicID=" + topicId);
                        }
                    }
                }

                Msg.Alert("恭喜，删除成功!", KSCMS.GetComeUrl());
                return;
            }
            else
            {
                Msg.Alert("对不起，没有选择记录！");
                return;
            }
        }
        #endregion
    }

</script>
<%
    if (!isLoadPage) return;
%>

<div class="t_tybox nomar">   
  <div class="r_title tytitle  pd0">
    <h4 class="ty-none">我的帖子</h4>
    <div class="tabs">  
                <ul>
                     <%
                  Response.Write(@"<li><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  class=""puton""><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<%=KSCMS.S("flag")==""?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/myask") %>">我提问的问题</a></li>
                  <li<%=KSCMS.S("flag")=="1"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/myask","flag-1") %>">我参与的回答</a></li>
                  <li<%=KSCMS.S("flag")=="2"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/myask","flag-2") %>">谁向我提问</a></li>
                  <li<%=KSCMS.S("flag")=="3"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/myask","flag-3") %>">申请问答专家</a></li>
                  <li<%=KSCMS.S("flag")=="0"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/MyTopic","flag-0") %>">我的帖子</a></li>
  				<li<%=KSCMS.S("flag")=="5"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("other/mytopic","flag-5") %>">我参与的话题</a></li>
  			</ul>
      	</div>         
  	</div> 
  <%if (flag==0){ %>
  <form name="myform" action="{$GetCurrUrl}" method="post" id="myform">
  <input type="hidden" name="action" value="delete"/>
   <table id="tablist" width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
      <tr align="center" class="title">
        	<td width="50" align="center">选择</td>
        	<td align="center">话题</td>
        	<td align="center" nowrap>回复/浏览</td>
            <td align="center">作者</td>
            <td align="center">操作</td>
        </tr>
      <%
          string param = "KS_BBSTopic Where UserID=" + (new TemporaryVar()).UserID;
          using (DataTable dt = DataFactory.GetPage("*", param, "topicid", "order by TopicID desc", CurrPage, PageSize, out recordcount, out pagecount))
          {
              if (dt.Rows.Count > 0)
              {
                  foreach (DataRow dr in dt.Rows)
                  {
                      KSCMS.W(@" <tr class=""tdbg"">
          <td class=""splittd"" align=""center""><input type=""checkbox"" name=""ids"" value="""+dr["TopicID"].ToString()+@""" /></td>
          <td class=""splittd tag_left""> 
           <a href="""+BasicField.GetClubShowUrl(Utils.StrToInt(dr["topicid"].ToString()))+@""" target=""_blank"">"+ Utils.CutStr(Utils.RemoveHtml(Convert.ToString(dr["Content"].ToString())), 80)+@"</a>
           <div class=""tips"">发布时间："+ Utils.FormatHumanizedTime(Convert.ToDateTime(dr["AddDate"].ToString()))+@" 是否审核："+(Convert.ToString(dr["verify"].ToString())=="1"?"<span style='color:green'>已审核</span>":"<span style='color:red'>未审核</span>")+@" 版面："+ MyCache.GetCacheBBSCategory(Convert.ToInt32(dr["boardId"].ToString()), "boardname")+@"</div>
          </td>
          <td class=""splittd"" style=""text-align:center"">"+dr["PostNum"].ToString()+"/"+dr["Hits"].ToString()+@"</td>
          <td class=""splittd"" style=""text-align:center"">"+ dr["UserName"].ToString()+@"</td>
          <td class=""splittd"" style=""text-align:center"">"+
              (MyCache.GetBBSCacheConfig(15).Equals("0") ? "---" : " <div class=\"bt\"><a onclick=\"return(confirm('确定删除吗？'));\" href='"+BasicField.GetUserUrl("other/mytopic","action-delete,ids-" + dr["topicid"].ToString())) + @"'><i class=""userfont uf-delete""></i></a></div>
           </td>
      </tr>");
                  }
              }
              else
              {
                  KSCMS.W(@"<tr class=""tr-nobor"">
                <td class=""splittd"" style=""text-align:center"" colspan=""10"">
                  <div class=""empty"">没有记录！</div>
                </td>
           </tr><style>.cz_btn{display:none;}</style>");
              }


          }
        %>
     
    </table>
   
    <div class="cz_btn">
      <div class="f-right">
    	  <span class="check-all"><input type="checkbox" name="chkall" id="chkall" onclick="CheckAll(this.form);"/>全选</span>  
        <span><input type="submit" class="button green-bt" value="删 除" /></span>
      </div>
    </div>
  </form>
 <%}else{ %>
     <table id="Table1" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
      <tr align="center" class="title">
        	<td width="50" align="center">序号</td>
        	<td align="center">话题</td>
            <td align="center">回复/浏览</td>
            <td align="center">操作</td>
        </tr>
      <%
       string sqlParam = postTable + " a inner join KS_BBSTopic b ON a.topicid=b.topicid Where a.username='" + (new TemporaryVar()).UserName + "'";
       using (DataTable dt = DataFactory.GetPage("a.*,b.Content as subject,b.postnum,b.hits,b.boardid,b.verify", sqlParam, "postid", "order by postID desc", CurrPage, PageSize, out recordcount, out pagecount))
       {
           if (dt.Rows.Count > 0)
           {
               int i = 1;
               foreach (DataRow dr in dt.Rows)
               {
                   KSCMS.W(@" <tr class=""tdbg"">
          <td class=""splittd"" align=""center"">"+i+@"</td>
          <td class=""splittd tag_left""> 
           <a href=""" + BasicField.GetClubShowUrl(Utils.StrToInt(dr["topicid"].ToString())) + @""" target=""_blank""><strong>" + Utils.CutStr(Utils.RemoveHtml(Convert.ToString(dr["Subject"].ToString())), 80) + @"</strong></a>
           <div style=""color:#999"">我的回复：" + Utils.CutStr(Utils.RemoveHtml(Convert.ToString(dr["Content"].ToString())), 80) + @"</div>
           <div style=""color:#999"">发布时间：" + Utils.FormatHumanizedTime(Convert.ToDateTime(dr["AddDate"].ToString())) + @" 作者：" + dr["username"].ToString() + @" 版面：" + MyCache.GetCacheBBSCategory(Convert.ToInt32(dr["boardId"].ToString()), "boardname") + @"</div>
          </td>
          <td class=""splittd"" style=""text-align:center"">" + dr["PostNum"].ToString() + "/" + dr["Hits"].ToString() + @"</td>
          <td class=""splittd"" style=""text-align:center"">" +
             (MyCache.GetBBSCacheConfig(20).Equals("0") ? "---" : " <div class=\"bt\"><a onclick=\"return(confirm('确定删除吗？'));\" href='" + BasicField.GetUserUrl("other/mytopic", "action-deleteanswer,ids-" +dr["postid"].ToString()+",topicid-"+ dr["topicid"].ToString())) + @"'><i class=""userfont uf-delete""></i></a></div>
           </td>
      </tr>");
                   i++;
               }
           }
           else
           {
               KSCMS.W(@"<tr class=""tdbg"">
                <td class=""splittd"" style=""text-align:center"" colspan=""10"">
                  <div class=""empty"">没有参与任何话题讨论！</div>
                </td>
           </tr><style>.cz_btn{display:none;}</style>");
           }

       }     
      %>
      </table>
  <%} %>
  
    <div class="cz_btn">
        <%
            #region 分页
            if (pagecount > 1)
            {
                string PageStr = "[KS:PageStr2]";
                BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
                KSCMS.W(PageStr);
            }
            #endregion
         %>
  </div>
  <div class="clear"></div>
  