<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.APPCode" %>
<%@ 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();   //初始化当前页信息
    bool isLoadPage = true;  //定义页面是否加载
    bool ispower = false;     //判断用户权限
    public static int RoomID = 0;
    public static int type;
    public static int workID;
    public static string name;
    public static string DefaultFile = string.Empty;
    public static string photourl = string.Empty;
    public static string ClassRoomTitle = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        string DoMethod = BasicField.GetParam("do");
        if (string.IsNullOrEmpty(DoMethod))
        {
            DoMethod = KSCMS.S("action");
        }
        switch (DoMethod)
        {
            case "cancel":

                break;
        }
        RoomID = Utils.StrToInt(KSCMS.S("RoomID"));
        int tcid = Utils.StrToInt(DataFactory.GetOnlyValue("select TcID from KS_CosTeacher where UserID =" + __user.UserID));
       /* if (__user.GroupID == 3 || Utils.StrToInt(DataFactory.GetOnlyValue("select count(*) from KS_CosClassroom where BanzhuRenID =" + new TemporaryVar().UserID + " and RoomID =" + RoomID)) > 0)
        {
            ispower = true;
        }
        else if (Utils.StrToInt(DataFactory.GetOnlyValue("select count(*) from KS_CosClassManage where TaskType = 7 and RoomID =" + RoomID + " and TcID=" + tcid)) != 0)
        {
            ispower = true;
        }
        */
        ispower = true;
        string sql1 = "";
        if (RoomID != 0)
        {
            sql1 = "select top 1 Title from KS_CosClassRoom where RoomID=" + RoomID;
            using (DataTable dt = DataFactory.GetDataTable(sql1))
            {
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    ClassRoomTitle = dr["Title"].ToString();
                }
            }
        }
        if (DoMethod.Equals("verify"))
        {

            VerifyStatus(KSCMS.S("ids"));
        }
        else if (DoMethod.Equals("del"))
        {

            deleteSj(KSCMS.S("ids"));
        }
    }

    private void VerifyStatus(string WorkID)
    {
        if (WorkID == null || WorkID == "0") return;

        WorkID = Utils.FilterIDs(WorkID);

        using (DataTable dt = DataFactory.GetDataTable("select chatid,status From KS_CourseChat WHERE chatid in(" + WorkID + ")"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["status"].ToString() == "1")
                    {
                        DataFactory.ExecuteNonQuery("Update KS_CourseChat set status=0 Where chatid=" + dr["chatid"].ToString() + "");
                    }
                    else
                    {
                        DataFactory.ExecuteNonQuery("Update KS_CourseChat set status=1 Where chatid=" + dr["chatid"].ToString() + "");
                    }
                }
            }
        }
        if (Utils.StrToInt(MyCache.GetCacheConfig(70)) > 0)
        {
            Kesion.HtmlTags.PageCache.UpdateCacheFileTag(KSCMS.GetInstallDir() + "cache/askindex.cache");
        }
        Msg.Alert("修改成功!", BasicField.GetUserUrl("teacher/TcClassRoomAskList", "roomid-" + RoomID));
    }

    private void deleteSj(string WorkID)
    {
        if (WorkID == null || WorkID == "0") return;

        WorkID = Utils.FilterIDs(WorkID);

        using (DataTable dt = DataFactory.GetDataTable("select UserID,chatid,CourseID from KS_CourseChat Where chatid in(" + WorkID + ") and coursetype=8"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int id = Utils.StrToInt(dr["chatid"].ToString());
                    int UserID = Utils.StrToInt(dr["UserID"].ToString());
                    int RoomID1 = Utils.StrToInt(dr["CourseID"].ToString());
                    Hashtable ht = new Hashtable();
                    ht.Add("chatid", id);
                    DataFactory.ExecuteNonQuery("update KS_CosClassStudent set AskCount=AskCount-1 where userid=" + UserID + " and RoomID=" + RoomID1);
                    CommonData.Delete("[KS_CourseChat]", ht);
                }
            }
        }
        Kesion.Course.Course.CheckCount("KS_CourseChat", "CourseID=" + RoomID + " and coursetype=8", "chatid", 1, "KS_CosClassRoom", "askcount", "roomid=" + RoomID + "");
        Msg.Alert("删除成功!", BasicField.GetUserUrl("teacher/TcClassRoomAskList", "roomid-" + RoomID));
    }



    public string GetImg(string imgInfo)
    {
        string data = "";
        if (string.IsNullOrEmpty(imgInfo))
        {
            string[] img = imgInfo.Split(',');
            foreach (string photo in img)
            {
                if (!string.IsNullOrEmpty(photo))
                {
                    data+="<img src='" + photo + "' style='width:120px;height:120px;' onerror=this.src='/sysimg/nopic.gif' />";
                }
            }
            return data;
        }
        else{
            return data;
        }
    }



</script>
<%
    if (!isLoadPage) return;
%>
 <form name="myform" action="<%=BasicField.GetUserUrl("teacher/TcClassRoomAskList") %>" method="post" id="Form1" >
	  <div class="t_tybox">
            <div class="tytitle"><h4>班级问答管理</h4>
           	<a onclick="" href="<%=BasicField.GetUserUrl("teacher/TcClassRoom") %>" class="addbtn">返回</a>
            </div>
            <div class="clear blank10"></div>
			<div class="part_now">当前班级：<%=ClassRoomTitle %></div>
			<div class="clear blank10"></div>
      
         <table id="Table1" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
      <tr align="center" class="title">
        	<td width="100" align="center">选择</td>
        	<td align="center">问题</td>
        	<td align="center">提问者</td>
            <td align="center">提问时间</td>
            <td align="center">状态</td>
            <td align="center">操作</td>
        </tr>
		<%
            int recordcount, pagecount;
            int CurrPage = new BasicField().GetCurrPage;
            int PageSize = 10;
            using (DataTable dt = DataFactory.GetPage("*", "KS_CourseChat where  CourseID=" + RoomID + " and coursetype=8", "chatID", "order by  AddData desc", CurrPage, PageSize, out recordcount, out pagecount))
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        %>
             <tr class="tdbg">
                 <td class="splittd" align="center">
                     <input class="cleckbtn" type="checkbox" name="ids" value="<%=dr["chatid"].ToString() %>"></td>
                 <td class="tag_left" style="text-align: left; padding: 20px;"> <%=dr["chatcontent"].ToString() %>
                     <%=GetImg(dr["chatimg"].ToString())  %>
                 </td>
                 <td class="splittd" align="center"><%=dr["UserName"].ToString() %></td>
                 <td class="splittd" align="center"><%=Utils.FormatHumanizedTime(Utils.StrToDateTime(dr["AddData"].ToString()))  %></td>
                 <%  if (ispower)
                 {%>
                 <td><a href="<%=BasicField.GetUserUrl("teacher/TcClassRoomAskList", "do-verify,ids-" + dr["chatid"].ToString()+",roomid-"+RoomID) %>"><%=(Utils.StrToInt(dr["status"]) == 0 ? "<span style='color:red'>未审核</span>" : "<span style='color:green'>已审核</span>") %></a></td>
                 <td>
                     <div class="gl_btn">
                         <span class="span5" style="width: 50%"><a href="<%=BasicField.GetUserUrl("teacher/TcClassRoomAskList", "do-del,ids-" + dr["chatid"].ToString()+",roomid-" + RoomID) %>" class="delete" onclick="return(confirm('确定删除该问答将会影响的您的班级的问答数吗?'));">
                             <i class="iconfont">&#xe637;</i>删除</a></span>
                     </div>
                 </td>
                 <%}
                 else
                 {%>
                 <td><%=(Utils.StrToInt(dr["status"]) == 0 ? "<span style='color:red'>未审核</span>" : "<span style='color:green'>已审核</span>")  %></td>
                 <td class="splittd" align="center">无操作权限</td>
                 <%} %>
             </tr>
             <%
					}
				}
				else
				{
					KSCMS.W(" <tr>	<td colspan=\"5\" style=\"border-bottom:0px;\"><div class=\"clear blank10\"></div> <div class=\"empty\">没有找到问题！</div> </td></tr>");
				}
			}
	   %>
      </table>
    
     	<div class="fy_box">
		  <%
            #region 分页
            string PageStr = "[KS:PageStr1]";
            BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
            KSCMS.W(PageStr);
            #endregion
         %>
     	</div>
          <%if(ispower){ %>
       <div class="cz_btn">
		  <input type="hidden" value="<%=RoomID %>" name="RoomID" />
		 <input type="hidden" value="del" name="action" />
        <span><input type="checkbox" class="checkbtn" name="chkall" id="chkall" onclick="CheckAll(this.form);"/>全选</span>
        <span><input type="submit" class="button" value="批量删除" onclick="return (CheckIds('del'));"></span>
        
     </div>
          <%} %>
     <div class="clear"></div>
     </div>
	
	 </form>