<%@ 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" %>
<%@ Import Namespace="Kesion.Components" %>
<script runat="server" type="text/C#">
    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    bool isLoadPage = true;  //定义页面是否加载
    bool ispower = false;     //判断用户权限
    public  int TcId = 0;
    public  int classId = 0;
    public  string RoomID = "0";
    public  string sqlParam = string.Empty;
    public  int Provinceid = 0;
    public  int CityId = 0;
    public  string DefaultPic = string.Empty;
    public string TName = string.Empty;
    public string ClassName=string.Empty;
    public string CourseIDS = string.Empty;
    public string FaceCourseIDs = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        RoomID = KSCMS.S("roomid");

        string sql = "select top 1 tcid,TName,ClassID,Provinceid,Cityid,verify from KS_CosTeacher where Field='" + (new TemporaryVar()).UserName + "'";
        if (__user.GroupID == 3)
        {
            sql = "select top 1 tcid,TName,ClassID,Provinceid,Cityid,verify from KS_CosTeacher where Field='" + DataFactory.GetOnlyValue("select top 1 Teacher from KS_CosClassroom where roomid="+RoomID+"") + "'";
        }
        using (DataTable dt = DataFactory.GetDataTable(sql))
        {
            DataRow dr;
            if (dt.Rows.Count > 0)
            {
                dr = dt.Rows[0];
                if (Utils.StrToInt(dr["verify"].ToString()) == 0)
                {
                    Msg.Alert("对不起，审核未通过不能进入!！", BasicField.GetUserUrl("teacher/Teacher"));
                    isLoadPage = false;
                    return;
                }
                TcId = Utils.StrToInt(dr["tcid"].ToString());
                classId = Utils.StrToInt(dr["ClassID"].ToString());
                TName = dr["TName"].ToString();
                ClassName = MyCache.GetCosClass(classId, "ClassName", 0);
                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 (__user.GroupID == 4 && new TemporaryVar().SchoolID == 0)
                {
                    ispower = true;
                }


                using (DataTable dtRoom = DataFactory.GetDataTable("select top 1 courseids,FaceCourseIDs from ks_cosClassRoom where roomid=" + RoomID))
                {
                    if (dtRoom.Rows.Count > 0)
                    {
                        DataRow drRoom = dtRoom.Rows[0];
                        CourseIDS = drRoom["courseids"].ToString();
                        FaceCourseIDs = drRoom["FaceCourseIDs"].ToString();

                    }
                }
                if (Utils.StrToInt(DataFactory.GetOnlyValue("select count(*) from KS_CosClassManage where TaskType = 2 and RoomID =" + RoomID + " and TcID=" + TcId)) != 0)
                {
                    ispower = true;
                }

            }
            else
            {

                Msg.Alert("对不起，您还未完善个人资料!", BasicField.GetUserUrl("teacher/Teacher"));

                isLoadPage = false;
                return;
            }

        }
        string DoMethod = KSCMS.S("do");

        if (KSCMS.S("action").Equals("save"))
        {
            SaveChange();
        }
    }
    protected void SaveChange()
    {
        Hashtable ht = new Hashtable();
        string msg = string.Empty;
        msg = "绑定课程成功!";
        string proids0 = KSCMS.S("LstBVisbile0");
        string proids1 = KSCMS.S("LstBVisbile1");
        proids0 = KSCMS.S("LstBVisbile0");
        ht.Add("CourseIDs", proids0); //添加课程           
        ht.Add("FaceCourseIDs", proids1); //添加直播课程
        int vodCourseCount = (string.IsNullOrEmpty(proids0) ? 0 : Utils.StrToInt(proids0.Split(',').Length));
        int faceCourseCount = (string.IsNullOrEmpty(proids1) ? 0 : Utils.StrToInt(proids1.Split(',').Length));
        int courseCount = vodCourseCount + faceCourseCount;
        ht.Add("CourseCount", courseCount);
        CommonData.Update("KS_CosClassRoom", ht, "where roomid=" + RoomID);
        CourseUserPowerCache.CleanClassRoomUserJson(Utils.StrToInt(RoomID));
        Msg.Alert(msg, BasicField.GetUserUrl("teacher/TcClassRoom"));
        isLoadPage = false;
        return;
    }



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

		var idArr = new Array();
		var dest0 = document.getElementById('LstBVisbile0');
		for (var i = 0; i < dest0.options.length; i++) {
			dest0.options[i].selected = "true";
			idArr.push(dest0.options[i].value);
        }
        var idArr = new Array();
		var dest1 = document.getElementById('LstBVisbile1');
		for (var i = 0; i < dest1.options.length; i++) {
			dest1.options[i].selected = "true";
			idArr.push(dest1.options[i].value);
		}
		return true;
	}

    function addUser(costype) {
        var src = document.getElementById('LstB' + costype);
        var dest = document.getElementById('LstBVisbile' + costype);

		for (var i = 0; i < src.options.length; i++) {
			if (src.options[i].selected) {
				var exist = false;
				for (var j = 0; j < dest.options.length; j++) {
					if (dest.options[j].value == src.options[i].value) {
						exist = true;
						break;
					}
				}
				if (!exist) {
					var opt = document.createElement('OPTION');
					opt.value = src.options[i].value;
					opt.text = src.options[i].text;
					opt.selected = true;
					dest.options.add(opt);
				}
			}
		}
	}
    function delUser(costype) {
        var dest = document.getElementById('LstBVisbile' + costype);

		for (var i = dest.options.length - 1; i >= 0; i--) {
			if (dest.options[i].selected) {
				dest.options[i] = null;
			}
		}
	}
    function searchCourse(costype) {
        var key = jQuery("#TxtSJKey" + costype).val();
		if (key == '') {
            
		    KesionJS.Alert("请输入课程关键字！");
           
			return false;
		} else {
            
		    puburl = "/plus/course/CourseAjax.ashx?action=GetTcCourse&from=user&RoomID=<%=RoomID%>&Key=" + escape(key) + "&costype="+costype;
       
 		jQuery.ajax({
				url: puburl, success: function (d) {
				
					jQuery("#LstB"+costype).empty();
					jQuery("#LstB"+costype).html(d);
				}
			});
		}
	}
</script>

<div class="t_tybox">
  <div class="tytitle"><h4 class="h4">绑定课程</h4>
 
  </div>
     <div class="clear"></div>
     <script src="/ks_inc/DatePicker/WdatePicker.js"></script>
     <script src="../../ks_inc/weiboApi.js"></script>
	    <div class="tab-page" id="sjPanel">
		  <form name="myform" action="<%=BasicField.GetUserUrl("teacher/AddTcClassCourse","do-"+KSCMS.S("do")) %>" method="post" id="Form1" >
		  <input type="hidden" value="save" name="action" />
		    <input type="hidden" value="<%=KSCMS.S("roomid") %>" name="RoomID" />
	            <table width="50%" border="0" cellpadding="0" cellspacing="0" class="border"> 
				<tr>
					<td align="right" class="lefttd" height="30"></td>
					<td class="righttd" valign="top"">&nbsp;
						<table border="0" width='100%' class="exam_seacher">
							<tr>
								<td class="tag_left" width="300" style="line-height: 30px;">
								<label style="display:inline-block;padding-top:18px">搜索在线课程</label>
                                   
                                 <span class="top_c_sc clearfix" style="margin-bottom:15px"><input name="TxtSJKey0" type="text" id="TxtSJKey0" class="textbox" style="width:160px;" />

									<input type="button" value="搜索" class="button" onclick="searchCourse(0);" cssclass="button" style="margin-left: 10px; border-radius: 5px; border: 1px solid #dce2e4;" /></span>
									
									<select size="4" name="LstB0" multiple="multiple" id="LstB0" style="width:320px;float: left; height: 360px !important ; background: #fff"></select>
                                   
								</td>
								<td>
									<p>
										<input type="button" value=" > " onclick="addUser(0)" class="ty_button" />
									</p>
									<br />
									<p>
										<input type="button" value=" < " onclick="delUser(0)" class="ty_button" />
									</p>
								</td>
								<td class="tag_left" style="line-height: 30px;"><label style="display:inline-block;padding-top:18px">绑定下列课程：</label>
									<br />
                                    <br />
									<select size="4" name="LstBVisbile0" multiple="multiple" id="LstBVisbile0" style="width:320px;float: left; height: 360px !important; background: #fff">
							
                                        <%=Kesion.APPCode.Public.GetCouser(CourseIDS) %>
									</select>
								</td>
							</tr>
						</table>
					</td>
				</tr> 
    </table>
              <table width="50%" border="0" cellpadding="0" cellspacing="0" class="border"> 
				<tr>
					<td align="right" class="lefttd" height="30"></td>
					<td class="righttd" valign="top"">&nbsp;
						<table border="0" width='100%' class="exam_seacher">
							<tr>
								<td class="tag_left" width="300" style="line-height: 30px;">
								<label style="display:inline-block;padding-top:18px">搜索面授课程</label>
                                   
                                 <span class="top_c_sc clearfix" style="margin-bottom:15px"><input name="TxtSJKey1" type="text" id="TxtSJKey1" class="textbox" style="width:160px;" />

									<input type="button" value="搜索" class="button" onclick="searchCourse(1);" cssclass="button" style="margin-left: 10px; border-radius: 5px; border: 1px solid #dce2e4;" /></span>
									
									<select size="4" name="LstB1" multiple="multiple" id="LstB1" style="width:320px;float: left; height: 360px !important ; background: #fff"></select>
                                   
								</td>
								<td>
									<p>
										<input type="button" value=" > " onclick="addUser(1)" class="ty_button" />
									</p>
									<br />
									<p>
										<input type="button" value=" < " onclick="delUser(1)" class="ty_button" />
									</p>
								</td>
								<td class="tag_left" style="line-height: 30px;"><label style="display:inline-block;padding-top:18px">绑定下列课程：</label>
									<br />
                                    <br />
									<select size="4" name="LstBVisbile1" multiple="multiple" id="LstBVisbile1" style="width:320px;float: left; height: 360px !important; background: #fff">
						          	<%=Kesion.APPCode.Public.GetCouser(FaceCourseIDs,1) %>

									</select>
								</td>
							</tr>
						</table>
					</td>
				</tr> 
    </table>
	    <div class="cz_btn nofloat" style=" padding:0 0px 20px 175px;">
            <%if(ispower){ %>
			<span><input type="submit" name="Button2" value="确定保存" onclick="return (CheckForm());" id="Button2" class="button" /></span>
            <%} %>
			<span><input type="button" class="button" onclick="window.location.href='<%=BasicField.GetUserUrl("teacher/TcClassRoom")%>';"value=" 返 回 "></span>
	     </div>

		 </form>
	  </div>
</div>