<%@ 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();   //初始化当前页信息

    string setkong = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
    DataRow drr = null;//编辑初始化
    private string GetVal(string fieldName)
    {
        if (drr != null)
        {
            return drr[fieldName].ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    public string action = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        initial();//初始化信息
        action = KSCMS.S("action");
        if(string.IsNullOrEmpty(action)){action=KSCMS.S("do");}
        switch(action)
        {
            case "initedit":
                if (KSCMS.S("groupinfo") != "")
                {
                    submitinfo();//修改
                }
                else {
                    initedit();//编辑初始化
                }
                break;
            case "add":
                if (KSCMS.S("groupinfo")!="")
                {
                    submitinfo();//新增   
                }
                break;
            case "delete"://删除操作
                deleteinfo(KSCMS.S("ids"));
                break;
        }
    }
    //编辑初始化
    private void initedit() 
    {
        using (DataTable dt = DataFactory.GetDataTable("select top 1 * from KS_BBSBoard where userid=" + __user.UserID + " and BoardID="+Utils.StrToInt(KSCMS.S("id"))))
        {
            if (dt.Rows != null)
            {
                drr = dt.Rows[0];
                string Setting = drr["setting"].ToString() + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
                string[] setinfo = System.Text.RegularExpressions.Regex.Split(Setting, @"[$]{4}");
                __info.times = setinfo[8];//新用户加入时间
                __info.uploadinfo = setinfo[13];//是否允许上传附件
                __info.text1 = setinfo[14];//提问限制
                __info.text2 = setinfo[17];//提问时间限制
                __info.text3 = setinfo[18];//提问时间限制
                __info.text5 = setinfo[26];//提问时间限制
                __info.text4 = setinfo[27];//提问时间限制
                __info.Button2 = "确认修改(O)";
                __info.Label1 = "修改小组信息";
            }
        }
    }
    //添加  或者修改
    private void submitinfo() 
    {
        string setting = KSCMS.S("setting");
        string[] setinfo = System.Text.RegularExpressions.Regex.Split(setting, @"[$]{4}");
        setinfo[8] = KSCMS.S("times");//8 注册时长可以提问
        setinfo[13] = KSCMS.S("upload");//13   是否允许上传附件
        setinfo[14] = KSCMS.S("text1");// 14提问数量限制
        setinfo[17] = KSCMS.S("text2");//17   提问时间限制  始
        setinfo[18] = KSCMS.S("text3");// 18   末    
        setinfo[27] = KSCMS.S("isinvite");//27组员是否允许邀请   暂时默认不邀请
        setinfo[26] = KSCMS.S("allowadd");// 26 加入权限 
        setting = setinfo[0] + "$$$$" + setinfo[1] + "$$$$" + Utils.StrToInt(setinfo[2], 5) + "$$$$" + setinfo[3] + "$$$$" + Utils.StrToInt(setinfo[4], 5) + "$$$$" + Utils.StrToInt(setinfo[5], 5) + "$$$$" + Utils.StrToInt(setinfo[6], 5) + "$$$$" + Utils.StrToInt(setinfo[7], 5) + "$$$$" + setinfo[8] + "$$$$" + setinfo[9] + "$$$$" + setinfo[10] + "$$$$";
        setting += setinfo[11] + "$$$$" + setinfo[12] + "$$$$" + setinfo[13] + "$$$$" + setinfo[14] + "$$$$" + Utils.StrToInt(setinfo[15], 5) + "$$$$" + Utils.StrToInt(setinfo[16], 0) + "$$$$" + setinfo[17] + "$$$$" + setinfo[18] + "$$$$" + Utils.StrToInt(setinfo[19], 0) + "$$$$" + Utils.StrToInt(setinfo[20], 0) + "$$$$";
        setting += Utils.StrToInt(setinfo[21], 0) + "$$$$" + Utils.StrToInt(setinfo[22], 0) + "$$$$" + Utils.StrToInt(setinfo[23], 0) + "$$$$" + Utils.StrToInt(setinfo[24], 0) + "$$$$" + Utils.StrToInt(setinfo[25], 0) + "$$$$" + setinfo[26] + "$$$$" + setinfo[27] + "$$$$$$$$$$$$";
        Hashtable ht = new Hashtable();
        ht.Add("BoardName", Utils.ChkSQL(KSCMS.S("boardname")));//组名
        ht.Add("Intro", Utils.ChkSQL(KSCMS.S("introl")));//介绍
        ht.Add("BoardRules", Utils.ChkSQL(KSCMS.S("BoardRules")));//组规
        ht.Add("ico", Utils.ChkSQL(KSCMS.S("DefaultPic")));//组标
        ht.Add("setting",setting);//配置
        ht.Add("ParentID", KSCMS.S("parentid"));//所属分类
        ht.Add("ParentIDPath", "0," + KSCMS.S("parentid"));//所属ParentIDPath
        ht.Add("adddate", DateTime.Now);//添加时间
        //ht.Add("Classstatus", Utils.StrToInt(KSCMS.S("classstatus")));//是否绑定班级的情况
        //if (KSCMS.S("classstatus")=="1") //判断是否有班级的情况
        //{
        //    ht.Add("ClassRoomId", KSCMS.S("classid"));
        //}
        ht.Add("BoardMaster", Utils.ChkSQL(KSCMS.S("BoardMaster")));//组长信息

        Kesion.Controls.CustomFields.DoSchoolFieldSave(ref ht);        
        
        if (KSCMS.S("groupinfo") == "0")//新增
        {
            ht.Add("UserID", __user.UserID);//添加用户名ID
            ht.Add("UserName", __user.UserName);//添加用户名
            CommonData.Insert("KS_BBSBoard",ht);
            CleanCache();
            Msg.Alert("恭喜，添加成功！", BasicField.GetUserUrl("teacher/TcGroup"));
            return;
        }
        else { //修改信息
            CommonData.Update("KS_BBSBoard", ht, " where BoardID =" + Utils.StrToInt(KSCMS.S("groupinfo")));
            CleanCache();
            Msg.Alert("恭喜，修改成功！", BasicField.GetUserUrl("teacher/TcGroup"));
            return;
        }
    }
    //删除操作
    private void deleteinfo(string Id) 
    {
        if (Id == null || Id == "0") return;

        Id = Utils.FilterIDs(Id);
        using (DataTable dt = DataFactory.GetDataTable("select BoardID from KS_BBSBoard Where BoardID in(" + Id + ")"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int id = Utils.StrToInt(dr["BoardID"].ToString());
                    Hashtable ht = new Hashtable();
                    ht.Add("BoardID", id);
                    CommonData.Delete("[KS_BBSBoard]", ht);
                    //DataFactory.ExecuteNonQuery("UPDATE [KS_enterprise] SET [PosionTotal] = [PosionTotal] - 1 WHERE UserId = " + (new TemporaryVar()).UserID);
                }
            }
        }
        Msg.Alert("恭喜，删除成功！", KSCMS.GetComeUrl());
    }
    //页面初始化参数信息
    private void initial() 
    {
        __info.Label1 = "添加小组信息";
        __info.Button2 = "确认添加(O)";
        __info.times = "20";//新用户加入时间
        __info.uploadinfo = "1";//是否允许上传附件
        __info.text1 = "0";//提问限制
        __info.text2 = "0";//提问时间限制
        __info.text3 = "0";//提问时间限制
        __info.text4 = "0";//**加入权限
        __info.text5 = "0";//提问时间限制
    }
    //教师不能直接建立大分类  只能在现有分类中选择
    public string gettype() 
    {
        StringBuilder sb = new StringBuilder();
        using (DataTable dt = DataFactory.GetDataTable("select * from KS_BBSBoard where parentid=0"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append("<option value='" + dr["BoardID"].ToString() + "'  " + (Utils.StrToInt(GetVal("parentid")) == Utils.StrToInt(dr["BoardID"]) ? "selected" : "") + "  >" + dr["BoardName"].ToString() + "</option>");
                }
            }
        }
        return sb.ToString();
    }
    ////获取班级信息
    //public string getclassroom() 
    //{
    //    StringBuilder sb = new StringBuilder();
    //    using (DataTable dt = DataFactory.GetDataTable("select * from KS_CosClassRoom where userid=" + __user.UserID))
    //    {
    //        if (dt.Rows.Count > 0)
    //        {
    //            foreach (DataRow dr in dt.Rows)
    //            {
    //                sb.Append("<option value='" + dr["roomid"].ToString() + "'" + (GetVal("Classstatus") == "0" ? "" : (GetVal("ClassRoomId") == dr["roomid"].ToString() ? "selected='selected'" : "")) + " >" + dr["title"].ToString() + "</option>");
    //            }
    //        }
    //    }
    //    if(sb.ToString()=="")
    //    {
    //        sb.Append("<option value='0' >暂无班级信息</option>");
    //    }
    //    return sb.ToString();
    //}
    //获取组员的表
    public string getboardMaster()
    {
        StringBuilder sb = new StringBuilder();
        using (DataTable dt = DataFactory.GetDataTable("select * from KS_BBSMember where BoardId='" + GetVal("BoardId")+"'"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append("<option value='" + dr["username"].ToString() + "' " + (GetVal("BoardMaster") == dr["username"].ToString() ? "selected" : "") + " >" + dr["username"].ToString() + "</option>");
                }
            }
        }
        return sb.ToString();
    }
    private void CleanCache()
    {
        #region 更新小组缓存
        KSCache cache = KSCache.GetCacheService();
        cache.RemoveObject("/CMS/CacheBBSCategoryTable");
        cache.RemoveObject("/CMS/CacheBBSCategoryURL");
        cache.RemoveObject("/CMS/BBSCategoryList");

        Cache.Remove("BBSBoard");

        if (Utils.StrToInt(MyCache.GetCacheConfig(70)) > 0)
        {
            Kesion.HtmlTags.PageCache.UpdateCacheFileTag(KSCMS.GetInstallDir() + "cache/BBSindex.cache");
        }

        #endregion
    }
</script>
<%if (action != "add" && action != "initedit")
  { %>
<div class="t_tybox">
    <form action="{$GetCurrUrl}" method="post">
        <div class="tytitle clearfix">
        	<a href="<%=BasicField.GetUserUrl("teacher/TcGroup","do-add") %>" class="addbtn" style="float:right; margin-left:0; margin-right:20px;">+添加小组</a>
            <h4 class="lf">兴趣小组列表</h4>
            <!--
            <a href="<%=BasicField.GetUserUrl("teacher/StuImport","do-group") %>" class="addbtn">导入学员</a>
            <input type="hidden" name="action" value="delete" />
            <input type="submit" value="批量删除" class="addbtn" />-->
        </div>
        <div class="photo_box">
	   <ul class="photo_box_list clearfix">
	     	
           <%
               using (DataTable dt = DataFactory.GetDataTable("select * from KS_BBSBoard where userid=" + __user.UserID))
               {
                   if (dt.Rows.Count > 0)
                   {
                       foreach(DataRow  dr in dt.Rows)
                       {
                          %>
                       <li class="photo_box_list_li">
                            <span class="checkbtn_box border"><input type="checkbox"  name="ids" id="ids" value="<%=dr["BoardID"] %>" class="checkbtn" /></span>
                            <div class="infos-pic">
                                <a href="<%=BasicField.GetRewriteUrl("clubforum",Utils.StrToInt(dr["BoardID"].ToString())) %>" target="_blank">
                                    <img src="<%=dr["ico"].ToString() %>" onerror="this.src='/sysimg/nopic.gif'">
                                </a>
                            </div>
                            <div class="infoConBox">
                                <div class="infos-title">
                                    <h2 class="h5"><a href="<%=BasicField.GetRewriteUrl("clubforum",Utils.StrToInt(dr["BoardID"].ToString())) %>" title="<%=dr["BoardName"].ToString() %>" target="_blank"><%=dr["BoardName"].ToString() %></a></h2>
                                </div>
                                <div class="infos-title">
                                    <span style="color:#999;">组员：<%=Utils.StrToInt(dr["StudentNum"].ToString()) %>人</span> 
                                </div>
                                <div class="btns"> 
                                    <span><a href="<%=BasicField.GetRewriteUrl("clubforum",Utils.StrToInt(dr["BoardID"].ToString())) %>">详情</a></span> 
                                    <span><a href="<%=BasicField.GetUserUrl("teacher/TcGroup","action-initedit,id-"+dr["BoardID"]) %>">编辑</a></span> 
                                    <span><a href="<%=BasicField.GetUserUrl("teacher/TcGroup","action-delete,ids-"+dr["BoardID"]) %>" onclick="return(confirm('您确定要删除此小组吗?'));">删除</a></span>
                                </div>
                            </div>
                            
                     </li> 
            <%
                               }
                           } 
                       }
                        %> 
           </ul>
        </div>
    </form>
</div>
<%}
  else if (action == "add" || action == "initedit")
  { %>
<div class="t_tybox">
    <script>
        function qie(id)
        {
            if (id == 'li1')
            {
                document.getElementById(id).setAttribute("class", "puton");
                document.getElementById("li2").setAttribute("class", " ");
                document.getElementById("li3").setAttribute("class", " ");

                document.getElementById("tab1").style.display = "";
                document.getElementById("tab2").style.display = "none";
                document.getElementById("tab3").style.display = "none";
            } else if (id == 'li2') {
                document.getElementById(id).setAttribute("class", "puton");
                document.getElementById("li1").setAttribute("class", " ");
                document.getElementById("li3").setAttribute("class", " ");

                document.getElementById("tab2").style.display = "";
                document.getElementById("tab1").style.display = "none";
                document.getElementById("tab3").style.display = "none";
            } else if (id == 'li3')
            {
                document.getElementById(id).setAttribute("class", "puton");
                document.getElementById("li1").setAttribute("class", " ");
                document.getElementById("li2").setAttribute("class", " ");

                document.getElementById("tab3").style.display = "";
                document.getElementById("tab1").style.display = "none";
                document.getElementById("tab2").style.display = "none";
            }
        }

        ////是否显示绑定班级相关信息
        //function classsshow(status)
        //{
        //    //alert(status);
        //    if (status == '0')
        //    {
        //        $("#cls").hide();
        //        //document.getElementById("cls").style.display = "none";
        //    } else 
        //    {
        //        $("#cls").show();
        //        //document.getElementById("cls").style.display = " ";
        //    }
        //}
        function CheckForm()
        {
            if (jQuery("#boardname").val() == '')
            {
                alert("小组名称不能为空！");
                return false;
            }
        }
    </script>
    <div class="ty_title"><span id="Label1"><%=__info.Label1 %></span></div>
    <form action="{$GetCurrUrl}" method="post" >
            <div class="tabs">
                <ul class="clearfix">
                    <li id="li1" class='puton'><a href="javascript:qie('li1');">基本信息</a></li>
                    <li id="li2"><a href="javascript:qie('li2');">提问权限</a></li>
                    <li id="li3" ><a href="javascript:qie('li3');">加入权限</a></li>
                </ul>
            </div>
        <!--基本信息 style="display:none"-->
    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border" id="tab1">
        <tr>
            <td align="right" class="lefttd" height="30" width="150">所属分类：</td>
            <td class="tag_left"><select  name="parentid" > 
                        <%=gettype() %>
                                 </select>
            </td>
        </tr>
        <tr style="display:none;">
            <td align="right" class="lefttd" height="30" width="150">小组组长：</td>
            <td class="tag_left"><select name="BoardMaster" >
                                    <option value="">请选择组长信息</option>
                <%=getboardMaster() %>
                                 </select> </td>
        </tr>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">小组名称：</td>
            <td class="tag_left"><input type="text" class="textbox" id="boardname" name="boardname" value="<%=GetVal("BoardName") %>" /></td>
        </tr>
        <%--<tr>
            <td align="right" class="lefttd" height="30" width="150">选择班级：</td>
            <td class="tag_left">
                <input type="radio" name="classstatus" value="0" <%=Utils.StrToInt(GetVal("Classstatus"))==0?"checked":"" %>   onclick="classsshow('0')" />不绑定班级
                <input type="radio" name="classstatus" value="1" <%=Utils.StrToInt(GetVal("Classstatus"))==1?"checked":"" %>  onclick="classsshow('1')" />绑定班级
                <div class="ep-mt10" id="cls" <%=Utils.StrToInt(GetVal("Classstatus"))==1?"":"style='display:none'" %> >
                    <select name="classid" id="classid"  >
                        <%=getclassroom() %>
                    </select>
                </div>
            </td>
        </tr>--%>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">小组介绍：</td>
            <td class="tag_left"  style="height: 50px"><textarea name="introl" id="introl" rows="3" cols="20" Style="width: 80%; height: 200px;"><%=GetVal("Intro") %></textarea>
                         <a class="textarea_min" href="javascript:zoomtextarea(-5,'introl');">-</a>
                         <a class="textarea_max" href="javascript:zoomtextarea(5,'introl');">+</a>
        </td>
        </tr>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">小组图标：</td>
            <td class="tag_left">              
                <input id="DefaultPic" name="DefaultPic" Class="textbox" style="float:left" type="text" value="<%=GetVal("ico")%>" >
               <input type="button" class="ty_button" onclick="SelectUserFiles('DefaultPic', 0);" value="选择图片" />
                <div class="clear blank10"></div>
     	        <%=Kesion.APPCode.UploadAPI.EchoUpload("courseUserPic","DefaultPic") %> 
                </td>
        </tr>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">小组组规：</td>
            <td class="tag_left">
                        <textarea id="Content" name="BoardRules" Style="width: 100%; height: 380px;" ><%=GetVal("BoardRules") %></textarea>
                        <div class="clear blank10"></div>
                        <%Kesion.APPCode.EditorAPI.EchoHead(); %>
                        <%Kesion.APPCode.EditorAPI.EchoEditor("editor", "Content", "Basic",0,false,true,true,false);%>
                    </td>
        </tr>
    </table>
        <!--提问权限-->
    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border" id="tab2" style="display:none">
        <tr>
            <td align="right" class="lefttd" height="30" width="150">新用户注册：</td>
            <td class="tag_left"><input type="text" class="textbox" name="times" style="width:80px;text-align:center;" value="<%=__info.times%>" />分钟后才可以在本小组提问</td>
        </tr>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">是否允许上传附件：</td>
            <td class="tag_left"><input type="radio" class="radiobtn" name="upload"  value="1" <%= __info.uploadinfo=="1"?"checked":"" %> />允许<input type="radio" class="radiobtn" name="upload" value="0" <%= __info.uploadinfo=="0"?"checked":"" %> />不允许</td>
        </tr>
        <tr>
            <td align="right" class="lefttd" height="30" width="150">提问限制：</td>
            <td class="tag_left"  style="height: 50px;"> 
               <div class="clearfix" style="margin-top:10px"> 一天内每个会员最多只能提<input type="text" class="textbox" name="text1" style="width:60px;text-align:center;" value="<%=__info.text1 %>" />个问题<span class="tips"> 不限制请输入“0”。</span><br /></div>
               <div class="clearfix" style="margin-top:10px"> 提问时间从<input type="text" class="textbox" name="text2" style="width:60px;text-align:center;" value="<%=__info.text2 %>" />-<input type="text" class="textbox" name="text3" style="width:60px;text-align:center;" value="<%=__info.text3 %>" />点不能提问（24小时制）<span class="tips"> 不限制请输入“0”。</span></div>
            </td>
        </tr>
    </table>
        <!--添加组员的方式-->
    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border" id="tab3" style="display:none">
        <tr style="display:none;">
            <td align="right" class="lefttd" height="30" width="300">小组成员是否允许邀请进入小组：</td>
            <td class="tag_left"><input type="radio" name="isinvite" value="0" <%=__info.text4=="0"?"checked":"" %> />不允许<input type="radio" name="isinvite" value="1" <%=__info.text4=="1"?"checked":"" %>  />允许</td>
            <%--购买过相关课程的用户自动加入小组--%>
        </tr>
         <tr>
            <td align="right" class="lefttd" height="30" width="300">加入权限：</td>
            <td class="tag_left"><input type="radio" name="allowadd" value="0" <%=__info.text5=="0"?"checked":"" %>  />不允许<input type="radio" name="allowadd" value="1" <%=__info.text5=="1"?"checked":"" %>  />允许</td>
        </tr>
    </table>
        <input type="hidden" name="setting" value="<%=(drr==null?setkong:GetVal("setting")) %>" />
        <input type="hidden" name="groupinfo" value="<%= (drr==null?"0":GetVal("BoardID")) %>" />
        <!--提交以及返回按钮-->
    <div class="cz_btn nofloat clearfix" style="padding: 20px 0px 0px 192px">
                <span>
                    <input type="submit" ID="Button2" onclick="return (CheckForm())" class="button" value="<%=__info.Button2 %>"  /></span>
                <span>
                    <input type="button" class="button" onclick="window.history.go(-1);" value=" 返 回 "></span>
            </div>
    </form>
</div>
<%} %>