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

    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息

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

    protected DataRow drr;
    private string GetVal(string fieldName)
    {
        if (drr != null)
        {
            return drr[fieldName].ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        __info.replaycontent = "";
        if (KSCMS.S("teacherreplay") == "replay") 
        {
            //教师回复内容
            teacherreplay();
        }
        //根据参数判盘类型信息
        switch (KSCMS.S("action"))
        {
            case "delete"://删除
                delete(KSCMS.S("ids"));
                break; 
            case "upstatus"://更新状态
                DataFactory.ExecuteNonQuery("update KS_CourseChat set chatstatus=1 where chatid="+Utils.StrToInt(KSCMS.S("id")));
                EnterPrise.AddInorOutScore("other", "ask", __user.UserName, __user.UserID);
                Msg.Alert("恭喜，状态修改成功！",KSCMS.GetComeUrl());
                break;
            case "replay":
                initreplay(Utils.StrToInt(KSCMS.S("id")));
                break;
        }
    }
    //删除信息
    private void delete(string id)
    {
        if (id == null || id == "0") return;
        id = Utils.FilterIDs(id);
        using (DataTable dt = DataFactory.GetDataTable("select * from KS_CourseChat where chatid in (" + id + ")"))
        {
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("chatid", dr["chatid"].ToString());
                    if (dr["chattype"].ToString() == "1")
                    {
                        //删除问答相关的表信息
                        string tablename = DataFactory.GetOnlyValue("select top 1 PostTable from KS_AskTopic where topicid=" + dr["ChatInfoid"].ToString());
                        if (tablename != "0")
                        {
                            DataFactory.ExecuteNonQuery("delete from " + tablename + " where topicid=" + dr["ChatInfoid"].ToString());//回复表
                        }
                        DataFactory.ExecuteNonQuery("delete from KS_AskTopic where topicid=" + dr["ChatInfoid"].ToString());//问题表
                    }
                    else if (dr["chattype"].ToString() == "99")
                    {
                        //删除点播道具的使用记录
                        DataFactory.ExecuteNonQuery("delete from KS_CosPropItem where id=" + dr["ChatInfoid"].ToString());//道具使用表
                    }
                    CommonData.Delete("KS_CourseChat", ht);
                }
            }
        }
        Msg.Alert("恭喜，删除成功！", KSCMS.GetComeUrl());
    }
    
    //教师回复
    private void teacherreplay() 
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(__user.UserID).Append(KSCMS.S("chatid"));
        string content = Utils.ChkSQL(KSCMS.S("replaycontent"));
        if(content==null||content=="")
        {
            Msg.Alert("请输入回复内容！",KSCMS.GetComeUrl());
            return;
        }
        Hashtable ht = new Hashtable();
        ht.Add("ChatID", Utils.StrToInt(KSCMS.S("chatid")));
        ht.Add("UserName", __user.UserName);
        ht.Add("Userid", __user.UserID);
        ht.Add("UserIP ", Utils.GetRealIP());
        ht.Add("ReplayData", DateTime.Now);
        ht.Add("ReplayContent", content);
        ht.Add("ChatStatus", 0);
        ht.Add("ReplayType", 0);//老师回答的
        CommonData.Insert("KS_CourseReplayChat",ht);
        Msg.Alert("恭喜，回复成功！", KSCMS.GetComeUrl());
    }
     
    //回复初始化
    private void initreplay(int chatid) 
    {
        using (DataTable dt = DataFactory.GetDataTable("select top 1 * from KS_CourseChat where chatid="+chatid))
        {
            if (dt != null) 
            {
                drr = dt.Rows[0]; 
            }
        }
    } 
    #region    获取聊天的相关信息
    //获取存储的类型信息
    public string getchattype(int chattype)
    {
        string chattypenaem = string.Empty;
        switch (chattype)
        {
            case 1:
                chattypenaem = "问老师";
                break;
            case 2:
                chattypenaem = "问客服";
                break;
            case 99:
                chattypenaem = "点播互动道具";
                break;
            case 100:
                chattypenaem = "直播互动道句";
                break;
            default:
                chattypenaem = "问答";
                break;
        }
        return chattypenaem;
    }
    //获取所属课程名
    public string getcoursename(int courseid,int coursetype,string coursename)
    {
        StringBuilder sb = new StringBuilder();
		if (coursetype == 8)
		{
			if (coursename == "0")
			{
				coursename = DataFactory.GetOnlyValue("select top 1 title from KS_CosClassRoom where roomid=" + courseid);
			}
			sb.Append("<a target='_blank' href=" + BasicField.GetRewriteUrl("roomview", Utils.StrToInt(courseid)) + ">" + coursename + "(班级)</a>");
		}
		else
		{
			if (string.IsNullOrEmpty(coursename))
			{
				 coursename = DataFactory.GetOnlyValue("select top 1 name from KS_Course where courseid=" + courseid);
			}
			if (coursename == "0")
			{
				sb.Append("<span>--</span>");
			}
			else
			{
				sb.Append("<a target='_blank' href=" + BasicField.GetRewriteUrl("courseview", Utils.StrToInt(courseid)) + ">" + coursename + "</a>");
			}
		}
        return sb.ToString();
    }
    //课时名称
    public string getperiodname(int periodid) 
    {
        string periodname = DataFactory.GetOnlyValue("select top 1 PeriodName from  KS_CosPeriod where PeriodID ="+periodid);
        return periodname;
    }
        #endregion
</script>

<div class="t_tybox">
     
    <% if(KSCMS.S("action")==""){ %>
    <div class="tytitle clearfix"><h4 class="lf">学员提问管理</h4>
        <div class="top_c_sc mt10"> 
		        <form name="myform" action="{$GetCurrUrl}" method="post" class="fl">
                    <select name="selecttype"> 
                        <option value="1" <%=KSCMS.S("selecttype")=="1"?"selected='selected'":"" %>>学员</option>
                        <option value="2" <%=KSCMS.S("selecttype")=="2"?"selected='selected'":"" %>>课程</option>
                        <option value="3" <%=KSCMS.S("selecttype")=="3"?"selected='selected'":"" %>>问题</option>
                    </select>
            <input name="searchteaxt" type="text"  value="<%=KSCMS.S("searchteaxt") %>" class="textbox" placeholder="搜索问题的关键字..." />
            <input type="submit" name="Button4" value=" 搜索 " id="Button4" class="button" />
	        </form>
        </div>
     </div>
    <div class="clear"></div>
     <div class="tabs">	
      <ul class="clearfix">
          <li <%=KSCMS.S("chatstatus")==""?"class='puton'":"" %>><a href="<%=BasicField.GetUserUrl("teacher/TcChatInfo") %>">所有信息</a></li>	
          <li <%=KSCMS.S("chatstatus")=="1"?"class='puton'":"" %>><a href="<%=BasicField.GetUserUrl("teacher/TcChatInfo","chatstatus-1") %>">已解答</a></li>	
          <li <%=KSCMS.S("chatstatus")=="0"?"class='puton'":"" %>><a href="<%=BasicField.GetUserUrl("teacher/TcChatInfo","chatstatus-0") %>">未解答</a></li>	 			
      </ul> 
    </div> 
     <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
         <tr align="center" class="title">
             <td  align="center">ID</td> 
             <td  align="center">所属课程</td> 
             <td  align="center">提问内容</td>
             <td  align="center">状态</td>
             <td  align="center">查看</td>
             <td  align="center">删除</td>
         </tr>
        <%  
            //获取教师下的学员信息 

           string sqlParam = " KS_CourseChat where chattype=1 ";
           if (KSCMS.S("chatstatus")!="")
           {
               sqlParam += " and chatstatus=" + Utils.StrToInt(KSCMS.S("chatstatus"));
           }
           if (KSCMS.S("selecttype") != "")//搜索类型
           {
               if (KSCMS.S("searchteaxt") != "")
               {
                   switch (KSCMS.S("selecttype"))
                   {
                       case "1"://按照学员搜索
                           sqlParam += " and username like '%" + Utils.RemoveXss(KSCMS.S("searchteaxt")) + "%'";
                           break;
                       case "2"://课程名称搜索
                           sqlParam += " and coursename like '%" + Utils.RemoveXss(KSCMS.S("searchteaxt")) + "%'";
                           break;
                       case "3"://问题内容搜索
                           sqlParam += " and chatcontent like '%" + Utils.RemoveXss(KSCMS.S("searchteaxt")) + "%'";
                           break; 
                   }
               } 
           }
            
           sqlParam += " and TcID = " + DataFactory.GetOnlyValue("select TcID from KS_CosTeacher where userid =" + __user.UserID);
           using (DataTable dt = DataFactory.GetPage("*", sqlParam, "chatid", "order by chatid desc", CurrPage, PageSize, out recordcount, out pagecount))
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        %>
         <tr class="tdbg">
             <td class="tag_left" style="text-align:center;"><%=dr["chatid"]%></td> 
              <td> <%=getcoursename(Utils.StrToInt(dr["courseid"].ToString()),Utils.StrToInt(dr["coursetype"].ToString()),dr["coursename"].ToString())%>
                  <%=Utils.StrToInt(dr["periodid"].ToString())==0?"":"(课时:"+getperiodname(Utils.StrToInt(dr["periodid"].ToString()))+")" %>
              </td>
             <td class="tag_left" style="text-align:left;padding: 20px;">
                 <%if (Utils.StrToInt(dr["chattype"].ToString()) != 99)
                   {%>
                 <a target="_blank" href="<%=BaseFun.GetRewriteUrl("askview", Convert.ToInt32(dr["chatinfoid"].ToString()))%>" target="_blank" class="asktitle"><%=dr["chatcontent"].ToString()%></a>
                    <div class="info_num"><span class="grayf">提问者：<%=dr["Username"].ToString()%></span><span class="grayf">提问时间：<%=Utils.FormatHumanizedTime(Convert.ToDateTime(dr["AddData"]))%> </span>
                  </div>
                 <%}
                   else
                   { %> 
                 <div  class="asktitle"> <%=dr["chatcontent"].ToString()%></div>
                 <div class="info_num"><span class="grayf">用户：<%=dr["Username"].ToString()%></span><span class="grayf">参与日期：<%=Utils.FormatHumanizedTime(Convert.ToDateTime(dr["AddData"]))%></span></div>
                 <%} %>
             </td>
             <td><%=dr["chatstatus"].ToString()=="0"?("<a href='"+BasicField.GetUserUrl("teacher/TcChatInfo","action-upstatus,id-"+dr["chatid"].ToString())+"' style='color:red'>未解决</a>"):("<span style='color:green'>已解决</span>") %></td>
             <td><div  class="gl_btn"><span class="span4" style="width:100%"><a href="<%=BasicField.GetUserUrl("teacher/TcChatInfo","action-replay,id-"+dr["chatid"].ToString()) %>" ><i class="icon iconfont icon-xiazai48"></i>回复</a></span></div></td>
              <td>
                  <%if(dr["chatstatus"].ToString()=="0"){ %>
                  <div  class="gl_btn"><span class="span5" style="width:100%">
                  <a href="<%=BasicField.GetUserUrl("teacher/TcChatInfo","action-delete,ids-"+dr["chatid"].ToString()) %>" onclick="return(confirm('确定删除么？此操作不可逆，请谨慎操作！'))"><i class="icon iconfont icon-lajitong-copy"></i>删除</a>
                  </span></div>
                      <%}
                    else { KSCMS.W("<span>-- </span>"); } %>
              </td>
            </tr>
         <%
                    }
                }
                else
                {
                    KSCMS.W("<tr class=\"tdbg\"> <td colspan=\"5\" style=\"border-bottom:0px;\"><div class=\"clear blank10\"></div><div class=\"empty\"> 暂无学员相关信息！</div></td></tr>");
                }
            }
            %>
         </table>
    <div class="fy_box fy_center">
	 <%
            #region 分页
            string PageStr = "[KS:PageStr1]";
            BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
            KSCMS.W(PageStr);
            #endregion
         %>
     </div>
	    <div class="clear"></div>

    <%}else if(KSCMS.S("action")=="replay"){%>
    <form method="post" action="{$GetCurrUrl}">
     <div class="tytitle"><h4>提问详情<a href="javascript:;"  onclick="history.back()">（返回）</a></h4></div>
     <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
          <tr>
             <td class="tag_right">提问编号:</td>
             <td class="tag_left"><%=GetVal("chatid") %></td>
             <td class="tag_right">提问内容:</td>
             <td class="tag_left"><div style="width: 300px;overflow: hidden;word-break: break-all;"><%=GetVal("chatcontent") %></div></td>
             <td class="tag_right">提问时间:</td>
             <td class="tag_left"><%=GetVal("adddata") %></td>
             <td class="tag_right">提问学员:</td>
             <td class="tag_left"><%=GetVal("username") %></td>
             <td class="tag_right">提问状态:</td>
             <td class="tag_left"><%=GetVal("chatstatus")=="1"?"已解决":"未解决" %></td>
         </tr>
     </table>
     <input type="hidden" name="chatid" value="<%=GetVal("chatid") %>" />
    <div class="reply">
       <ul>
        <%
          using (DataTable dt = DataFactory.GetDataTable("select * from KS_CourseReplayChat where chatid=" + GetVal("chatid")))
          {
              if (dt.Rows.Count > 0)
              {
                  foreach(DataRow dr in dt.Rows)
                  {
                  %>
			<li> <span><%=dr["username"].ToString() %>:<%=dr["ReplayContent"].ToString() %></span>
        <span><%=dr["ReplayData"].ToString() %></span>
        <%
                  }
              } 
          }
             %>
			</ul>
        <%if(Utils.StrToInt(GetVal("chatstatus"))==0){ %>
        <div class="text-box" style="margin-top:10px;margin-left:20px">
           <label class="f-l">回复：</label> 
            <textarea  name="replaycontent" aria-multiline="true" placeholder="请输入回复的内容。。。"  class="form-control" style="min-height:120px;width:70%"></textarea>
            </div>
        </div>
            <input type="hidden" name="teacherreplay" value="replay" />
          <div class="cz_btn cz_btn_pad">
		    <span><input type="submit"  ID="Button2"  Class="mainButton button"  value="确 定 回 复"   /></span>
		    <span><input type="button" class="button" onclick="window.history.go(-1);"value=" 返 回 "></span>
	      </div>
	      <div class="clearfix"></div>
        <%} %>
        </form>
    <%} %>

</div>