<%@ 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" %>
<%@ Import Namespace="System.IO" %>
<script runat="server" type="text/C#">
    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    bool isLoadPage = true;  //定义页面是否加载
    public DataRow pdr = null;
    public static int CourseId = 0;
    public static int ZLID;
    public static string name;
    public static int type;
    public static string prt = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        Course.CheckMVCVerify("Teacher/CourseList");//判断权限

        string sql1 = string.Empty;
        prt = KSCMS.S("prt");
        type = Utils.StrToInt(KSCMS.S("type"), 0);
        CourseId = Utils.StrToInt(KSCMS.S("CourseId"));
        if (CourseId != 0)
        {
            if (type == 0)
            {
                sql1 = "select top 1 Name,Price_Member,isfree from KS_Course where CourseId = " + CourseId;
            }
            else
            {
                sql1 = "select top 1 Name,Price_Member,isfree from KS_CourseFace where CourseId = " + CourseId;
            }

            using (DataTable dt = DataFactory.GetDataTable(sql1))
            {
                if (dt.Rows.Count > 0)
                {
                    pdr = dt.Rows[0];
                    name = pdr["name"].ToString();
                }
            }
        }
        string DoMethod = BasicField.GetParam("do");
        if (string.IsNullOrEmpty(DoMethod))
        {
            DoMethod = KSCMS.S("action");
        }
        switch (DoMethod)
        {
            case "modify":
                initialEdit(Utils.StrToInt(KSCMS.S("zlid")));
                break;
            case "add":
                pdr=null;
                break;
            case "del":
                deleteSj(KSCMS.S("ids"));
                break;
            case "search":
                break;
        }
        if (KSCMS.S("action").Equals("save"))
        {
            SaveZL();
        }

    }
    private void deleteSj(string ZLID)
    {

        if (ZLID == null || ZLID == "0"||string.IsNullOrEmpty(ZLID))
        {
            Msg.Alert("对不起,您没有选择资料!", BasicField.GetUserUrl("teacher/CsZllist", "type-" + type + ",prt-" + prt + ",courseid-" + CourseId));
            isLoadPage = false;
            return;
        }
        else
        {

            ZLID = Utils.FilterIDs(ZLID);

            using (DataTable dt = DataFactory.GetDataTable("select ZLID from KS_CosZL Where ZLId in(" + ZLID + ") and username='" + UserManage.GetUserInfo().UserName + "' and CourseID=" + CourseId + ""))
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        int id = Utils.StrToInt(dr["ZLID"].ToString());

                        Hashtable ht = new Hashtable();
                        ht.Add("ZLID", id);
                        CommonData.Delete("[KS_CosZl]", ht);

                    }
                }
            }
            Msg.Alert("恭喜,资料删除成功!", BasicField.GetUserUrl("teacher/CsZllist", "type-" +type + ",prt-" + prt + ",courseid-" + CourseId));
            isLoadPage = false;
            return;
        }
    }
    private void initialEdit(int ZLID)
    {
        SqlParam[] param = new SqlParam[1];
        param[0] = new SqlParam("ZLID", ZLID);

        using (DataTable dt = CommonData.GetDataTable("[KS_CosZL]", "*", param))
        {
            if (dt.Rows.Count > 0)
            {
                pdr = dt.Rows[0];
            }
        }
    }
    public string GetParam(string Fields)
    {
        string result = "";
        if (pdr != null)
        {
            result = pdr[Fields].ToString();
        }
        return result;
    }
    protected void SaveZL()
    {
        Utils.TipsDisabled();  //检查是否演示状态

        int page = Utils.StrToInt(KSCMS.S("TextPage"));
        Hashtable ht = new Hashtable();

        ht.Add("Zlname", Utils.RemoveXss(KSCMS.S("TxtName"))); //资料名称
        ht.Add("ZLIntro", KSCMS.S("TxtIntro"));

        ht.Add("adddate", DateTime.Now);

        ht.Add("username", UserManage.GetUserInfo().UserName);//提供者

        ht.Add("IsPublic", Utils.StrToInt( KSCMS.S("RadioPrices")));//资料状态

        ht.Add("verify", Utils.StrToInt( KSCMS.S("RdbVerify")));
        ht.Add("photourl", Utils.RemoveXss(KSCMS.S("photourl")));
        string DefaultFile = Utils.RemoveXss(KSCMS.S("DefaultFile"));
        ht.Add("ZLAdd", DefaultFile);
        ht.Add("type",KSCMS.S("coursetype") );
        /*
        if ( KSCMS.S("RadioPrices") == "0")
        {

            ht.Add("prices", Utils.StrToFloat(KSCMS.S("TextPrices")).ToString("f2"));//收费
            ht.Add("page", page);  //可看页数
        }
        else
        {
            ht.Add("prices", 0);//免费
            ht.Add("page", 0);
            page = 100;
        }
        */
        ht.Add("prices", Utils.StrToFloat(KSCMS.S("TextPrices")).ToString("f2"));//收费
        // ht.Add("isfree", Utils.StrToInt(this.RadioPrices.SelectedValue));


        #region 文件大小,页数
        string DocType = "";
        if (!string.IsNullOrEmpty(DefaultFile.ToString()) && DefaultFile.ToString().IndexOf('.') != -1)
        {
            DocType = System.IO.Path.GetExtension(DefaultFile).Replace(".", string.Empty).Substring(0, 3);
        }
        ht.Add("DocType", DocType);
        if (DocType == "pdf")
        {
            float MyFileSize = 0;
            FileInfo MyFileInfo = new FileInfo(Utils.GetMapPath(DefaultFile));
            MyFileSize = (float)MyFileInfo.Length / (1024);
            ht.Add("size", MyFileSize.ToString("f3"));  //文件大小

        }

        #endregion
        ht.Add("CourseID", CourseId);
        ht.Add("CourseName", name);
        if (KSCMS.S("do").Equals("add"))
        {
            int PrevOrderID = Convert.ToInt32(DataFactory.GetOnlyValue("select Max(OrderID) From KS_CosZl where courseID=" + CourseId));
            ht.Add("OrderID", PrevOrderID + 1);

            CommonData.Insert("KS_CosZL", ht);
            //新增资料信息
            if (KSCMS.S("type") == "1")
            {
                Kesion.APPCode.Public.CountInfo(CourseId, "facezl");
            }
            else
            {
                Kesion.APPCode.Public.CountInfo(CourseId, "zl");
            }
            Msg.Alert("恭喜,资料添加成功!", BasicField.GetUserUrl("teacher/CsZLlist", "type-" + KSCMS.S("coursetype") + ",prt-" + KSCMS.S("prt") + ",courseid-" + CourseId));
            isLoadPage = false;
            return;
        }
        else
        {

            CommonData.Update("KS_CosZL", ht, " where  ZLID= " + Utils.StrToInt(KSCMS.S("zlid")));
            Msg.Alert("恭喜,资料修改成功!", BasicField.GetUserUrl("teacher/CsZLlist", "type-" + KSCMS.S("coursetype") + ",prt-" + KSCMS.S("prt") + ",courseid-"+KSCMS.S("courseid")));
            isLoadPage = false;
            return;
        }
    }
</script>
<%
    if (!isLoadPage) return;
	if (!KSCMS.S("do").Equals("modify") && !KSCMS.S("do").Equals("add"))
	{
%>
    <style>
       #upframe {height:60px!important}
    </style>
    <div class="t_tybox">
		<div class="tytitle clearfix"><h4 class="lf">资料管理</h4><a href="<%=BasicField.GetUserUrl("teacher/Cszllist","do-add,type-"+type+",courseid-"+CourseId+",prt-"+prt+"") %> " class="addbtn">+添加资料</a>
			<a onclick="" href="<%=prt=="0"?BasicField.GetUserUrl("teacher/courselist","costype-"+type):prt=="1"?BasicField.GetUserUrl("teacher/LiveList"):BasicField.GetUserUrl("teacher/faceList") %>" class="addbtn">返回</a>
		<form name="myform" action="<%=BasicField.GetUserUrl("teacher/CsZLlist","type-"+type+",courseid-"+CourseId+",prt-"+prt+"") %>" method="post" id="myform2">
            <div class="top_c_sc">
        	<span>资料名称：</span>
           <input name="TextBox1" type="text" id="TextBox1" class="textbox" />
           <input type="submit" name="Button4" value=" 搜索 " id="Button4" class="button" />
    	</div>
		</form>
		</div>
		<div class="clear blank10"></div>
     	
		<!--<div class="part_now">当前课程：<%=name %></div>-->
        <div class="clear blank10"></div>
      	<form name="myform" action="<%=BasicField.GetUserUrl("teacher/CsZLlist","type-"+type+",courseid-"+CourseId+",prt-"+prt+"") %>" method="post" id="myform2">	  			
		 <table id="tablist" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
            <tr align="center" class="title">
                 <td width="6%" align="center">选择</td>
				 <td width="6%" align="center">序号</td>
                 <td width="6%" align="center">状态</td>
                 <td width="31%">资料名称</td>              
                 <td width="8%">文件大小</td>
                 <td width="6%">权限</td>                 
                 <td width="15%">添加时间</td>
                 <td align="center" nowrap width="15%">操作</td>
              </tr>
 
		  <%
				
					 int recordcount, pagecount;
					 int CurrPage = new BasicField().GetCurrPage;
					 int PageSize = 10;
				   string sqlParam = "KS_CosZL where  type=" + type + " and username='" + UserManage.GetUserInfo().UserName + "' and CourseId=" + CourseId + "";

				   if (!string.IsNullOrEmpty(KSCMS.S("TextBox1")))
            {
				sqlParam += " and Zlname like '%" + Utils.ChkSQL(KSCMS.S("TextBox1")) + "%'";
            }
			using (DataTable dt = DataFactory.GetPage("*", sqlParam, "ZLid", "order by  adddate desc", CurrPage, PageSize, out recordcount, out pagecount))
			{
				if (dt.Rows.Count > 0)
				{
					int i = (CurrPage - 1) * PageSize + 1;
					foreach (DataRow dr in dt.Rows)
					{
						KSCMS.W(" <TR class=\"tdbg sort bar\"> <td height=\"38\" align=\"center\" nowrap><input type=\"checkbox\"  name=\"ids\" id=\"ids\" value=\"" + dr["zlid"].ToString() + "\" class=\"checkbtn\" /></td>" +
		 "<td  align=\"center\"> " + (i++) + "</td><td class=\"Ctd\" align=\"center\"><a href=\"" + BasicField.GetUserUrl("teacher/CsZllist", "do-IsPublic,zlid-" + dr["zlid"].ToString() + ",courseid-" + dr["courseid"].ToString() + "") + "\" >" + (Utils.StrToInt(dr["verify"]) == 1 ? "审核" : "未审核") + "</a>	</td>" +
		 "<td class=\"tag_left lefticon\"> <a href=\"" + BasicField.GetRewriteUrl("graphics", Utils.StrToInt(dr["zlid"].ToString())) + "\" target=\"_blank\"><i class=\"iconfont\">&#xe637;</i></a>" +
			"<font class=\"fonths\">" + Utils.CutStr(Convert.ToString(dr["Zlname"]), 30) + "</font></td><td align=\"center\">" + dr["size"] + "KB </td>" +
			"<td  align=\"center\"> <strong>" + (dr["IsPublic"].ToString() == "1" ? "免费" : "购课后下载")+"</td>" +
		 "<td width=\"135\">" + Convert.ToDateTime(dr["adddate"].ToString()).ToString("yyyy-MM-dd") + "</td>" +
		 "<td style=\"text-align:center\" nowrap><div class=\"gl_btn\"><span class=\"span4\" style=\"width:50%\">" +
			 "<a id=\"LinkButton1\" title=\"编辑\" href=\"" + BasicField.GetUserUrl("teacher/CsZLlist", "do-modify,zlid-" + dr["zlid"].ToString() + ",courseid-"+dr["courseid"].ToString()+"") + " \"><i class=\"iconfont\">&#xe61d;</i>编辑</a></span>" +
			 " <span class=\"span5\" style=\"width:50%\">" +
					"<a href=\" " + BasicField.GetUserUrl("teacher/CsZLlist", "do-del,ids-" + dr["zlid"].ToString() + ",courseid-" + dr["courseid"].ToString() + "") + "  \" onclick=\"return confirm('确认删除吗?如果要删除,请点确认,否则请取消.')\"><i class=\"iconfont\">&#xe731;</i>删除</a> " +
			 " </span></div></td> </TR>");
					}
				}
				else
				{
					KSCMS.W("<tr class=\"tdbg\"><td class=\"splittd\" style=\"text-align:center\" colspan=\"10\" ><div class=\"empty\">暂未添加任何资料</div></td> </tr> ");
				}
			}
		  %>
          

        
           </table>

    
		   
		  
        <div class="cz_btn"> 
            
            <span><input type="checkbox" name="chkall" class="checkbtn" id="chkall" onclick="CheckAll(this.form);"/>全选</span>
        	<span><input type="submit" class="button" value="批量删除" onclick="return (CheckIds('del'));"></span>
            <input type="hidden"  name="action" value="del" id="action"/>
        </div> 
         <div class="fy_box" >
		   <%
            #region 分页
            string PageStr = "[KS:PageStr1]";
            BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
            KSCMS.W(PageStr);
            #endregion
         %>
				
 		 </div>
		<div class="clearfix"></div>           

	   </form>

 		
 		 
  </div>
   <div style="padding:10px 0px 0px;">
			<script type="text/javascript">
			    showtips(' <span><strong>操作说明：</strong><br /></span>1.资料只能是<font color="red">pdf</font>格式的；2.这里设置的是否收费是根据课程来的，如果这门课程是<font color="blue">收费</font>的话，那么该资料可以是<font color="red">收费或者是免费</font>的，如果这门课程是<font color="blue">免费</font>的，那么该资料只能是<font color="red">免费</font>的。');
			</script>
			</div>
  <%}else{
		Kesion.APPCode.EditorAPI.EchoHead();
		%>
   <form name="aspnetForm" method="post" action="<%=BasicField.GetUserUrl("teacher/CsZLlist","type-"+type+",courseid-"+CourseId+",prt-"+KSCMS.S("prt")+",do-"+KSCMS.S("do")+"")%> " id="aspnetForm">
  <input type="hidden" value="save" name="action" />
   <input type="hidden" value="<%=KSCMS.S("costype") %>" name="costype" />
	 <input type="hidden" value="<%=GetParam("zlid") %>" name="zlid" />
        <div class="t_tybox">
            <div class="ty_title">资料管理</div>
            <div class="clear"></div>
			<div class="clear blank10"></div>
      <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border"> 
    
   	<input name="coursetype" type="text" value="<%=(KSCMS.S("do").Equals("add")?KSCMS.S("type"): GetParam("type"))%>" id="coursetype" style="display:none" />
    
      <tr>
        <td align="right" class="lefttd" height="30" > 课程名称：</td>
         <td class="tag_left">
             <%=name %>
         </td>
     </tr>
     <tr>
        <td align="right" class="lefttd" height="30" >    <font color=red >*</font>资料名称：</td>
         <td class="tag_left"><input name="TxtName" type="text" value="<%=GetParam("ZLName")%>" id="TxtName" class="textbox"  />
             
         </td>
     </tr>
	 <tr>
        <td align="right" class="lefttd" height="30" >封面图片：</td>
         <td class="tag_left">

               <input id="photourl" name="photourl" Class="textbox" style="float:left;" type="text" value="<%=GetParam("photourl") %>" >
               <%=Kesion.APPCode.UploadAPI.EchoUpload("courseUserPic","photourl") %> 

         </td>
     </tr>
       <tr>
        <td align="right" class="lefttd" height="30" ><font color=red >*</font>上传资料：</td>
           <td class="tag_left">
          <input id="DefaultFile" name="DefaultFile" Class="textbox" style="float:left;" type="text" value="<%=GetParam("ZLAdd") %>" >
              <%=Kesion.APPCode.UploadAPI.EchoUpload("courseZLFile","DefaultFile") %> 
               上传PDF格式文件支持在线图文浏览
         </td>
     </tr>
     <tr>
            <td align="right" class="lefttd" height="30" style="width: 120px">是否购课后下载：</td>
            <td class="tag_left">
             <table id="RadioPrices" border="0" style="height:24px;width:197px;" onclick="free()">
		<tr>
			<td class="bo-min"><input id="RadioPrices_0" type="radio" class="radiobtn" name="RadioPrices" value="1" <%=Utils.StrToInt(GetParam("IsPublic"))==1?"checked":"" %> /><label for="RadioPrices_0">免费</label></td>
			<td class="bo-min"><input id="RadioPrices_1" type="radio" class="radiobtn" name="RadioPrices" value="0" <%=Utils.StrToInt(GetParam("IsPublic"))==0?"checked":"" %> /><label for="RadioPrices_1">收费</label></td>
		</tr>
	</table>
             </td>
        </tr>
<tbody id="pricebody" style="display:none">
      <tr  >
          <td align="right" class="lefttd" height="30">价格：</td>
          
              <td class="tag_left">
           <input name="TextPrices" type="text" value="<%= Utils.StrToFloat(GetParam("prices")).ToString("f2") %>" id="TextPrices" class="textbox nopadtext" style="width:50px;" /> 元
             </td>
          </tr>
     <tr>
           <tr style="display:none">
          <td align="right" class="lefttd" height="30" style="width: 102px">浏览几页：</td>
          
              <td class="tag_left">
           <input name="TextPage" type="text" value="<%=GetParam("page") %>" id="TextPage" class="textbox nopadtext" style="width:50px;" /> 0为全部可以看
             </td>
          </tr>
    </tbody>
     <tr>
        <td align="right" class="lefttd" height="30" >资料简介：</td>	`
         <td class="tag_left"><textarea name="TxtIntro" rows="2" cols="20" id="TxtIntro" style="height:100px;width:600px;"><%=GetParam("ZLIntro") %></textarea>
		  <% Kesion.APPCode.EditorAPI.EchoEditor("editor", "TxtIntro", "Basic", 0, false, true, true, false);     %>
       </td>
     </tr>
        <tr>
            <td align="right" class="lefttd" height="30" style="width: 102px">
             状态：</td>
            <td class="tag_left">
             <table id="RdbVerify" border="0" style="height:24px;width:190px;">
		<tr>
			<td class="bo-min"><input id="RdbVerify_0" type="radio"  class="radiobtn" name="RdbVerify" value="1" <%=Utils.StrToInt(GetParam("verify"))==1?"checked":"" %> /><label for="RdbVerify_0">审核</label></td>
			<td class="bo-min"><input id="RdbVerify_1" type="radio" class="radiobtn" name="RdbVerify" value="0" <%=Utils.StrToInt(GetParam("verify"))==0?"checked":"" %>/><label for="RdbVerify_1">未审核</label></td>
		</tr>
	</table>
             </td>
        </tr>
  
  </table>
	 <script>
    
       $('#upframe').css('height','60px')
         function free() {
               return;
	 		var checkrad = jQuery("#RadioPrices :radio[checked]").val();
		
	 		if (checkrad == "0") {
	 			$("#pricebody").show();
	 		} else {
	 			$("#pricebody").hide();
	 		}
	 	}
	 	$(document).ready(function () {
	 		free();

	 	});
         function CheckForm() {
              var isFree = jQuery("#RadioPrices :radio[checked]").val();//0收费，1免费
		
	 		if (jQuery('#TxtName').val() == '') {
	 			KesionJS.Alert('资料名称必须输入!', "jQuery('#TxtName').focus()");
	 			return false;
	 		}
	 		if (jQuery('#DefaultFile').val() == '') {
	 			KesionJS.Alert('请选择上传的文件!', "jQuery('#DefaultFile').focus()");
	 			return false;
             }
             if(isFree == '0')
             {
                 if (parseFloat($("#TextPrices").val()) < 0) {
                      KesionJS.Alert("价格不能小于0！", "jQuery('#TextPrices').focus()");
                        return false;
                 }
             }
              return true;
	 	}

    
</script>
         <div class="cz_btn nofloat " style="padding:20px 0px 0px 192px;height:auto">
         	<span><input type="submit" name="Button2" value="<%=KSCMS.S("do").Equals("modify")?"确定修改":"确定添加" %> (O)" onclick="return (CheckForm());" id="Button2" class="button" /></span>
            <span><input type="button" class="button" onclick="window.history.go(-1);"value=" 返 回 "></span>
            <div class="clearfix"></div>
            
         </div>

   
</div>


		</form>	


  <%} %>
