<%@ 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;  //定义页面是否加载
	public DataRow drr = null;
	public float sum = 0;
	public int ID;
	protected void Page_Load(object sender, EventArgs e)
	{
		string DoMethod = KSCMS.S("do");
		ID =Utils.StrToInt(KSCMS.S("ids"));
		switch (DoMethod)
		{
			case "modify":
				initialEdit(ID); //编辑
				break;
			case "del":
				deleteSj(ID.ToString()); //删除
				break;
		}
		if (KSCMS.S("action").Equals("save"))
		{
			SaveChange();
		}
	}
	private void initialEdit(int ID)
	{
		using (DataTable dt = DataFactory.GetDataTable("select top 1 b.cname,a.username,a.GoodsMoney,a.orderid  from KS_CosOrder  a  inner join KS_CosOrderItem b on a.orderid=b.orderid where a.status=0 and  a.id=" + ID + " and  b.business = '" + UserManage.GetUserInfo().UserName + "'"))
		{
			if (dt.Rows.Count > 0)
			{
			 drr = dt.Rows[0];
			}
		}
	}
	private void deleteSj(string ID)
	{
		if (ID == null || ID == "0") return;

		ID = Utils.FilterIDs(ID);
		using (DataTable dt = DataFactory.GetDataTable("select top 20 id,orderid from KS_CosOrder Where id in( " + ID + ") and status=0 "))
		{
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Hashtable ht1 = new Hashtable();

					// int id = Utils.StrToInt(dr["orderid"].ToString());
					ht1.Add("orderid", dr["orderid"].ToString());
					CommonData.Delete("KS_CosOrder", ht1);
					CommonData.Delete("KS_CosOrderItem", ht1);
					DataFactory.ExecuteNonQuery("UPDATE  KS_ProCouponUser SET Courseorder='',UseFlag=0 where  Courseorder='" + dr["orderid"].ToString() + "'");
				}
			}
		}
		Msg.Alert("恭喜,订单删除成功!", KSCMS.GetComeUrl());
		isLoadPage=false;
		return;
	}
	protected void SaveChange()
	{
		if (string.IsNullOrEmpty(KSCMS.S("GoodsMoney").Trim()))
		{
			Msg.Alert("请输入最终价格!");
			return;
		}
        int vipid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 ScoreitemID from KS_CosTeacher where field='" + __user.UserName + "'"), 0);
        float nums = Utils.StrToFloat(DataFactory.GetOnlyValue("select top 1 DivideMoney from KS_EnterPrisePower where type=2 and  title='" + vipid + "'"));
        float goodsmoney = Utils.StrToFloat(KSCMS.S("GoodsMoney"));
		Hashtable ht = new Hashtable();
        ht.Add("GoodsMoney", goodsmoney); //课程价    
		ht.Add("OrderTotalPrice", goodsmoney); //课程价
		CommonData.Update("KS_CosOrder", ht, "where status=0 and  id=" + ID + "");
        if (Kesion.XML.XmlObject.ReadXmlReturnNode(Utils.GetMapPath(MyCache.GetCacheConfig(0) + "/config/course/teacher.config"), "//DivideStatus") == "1")
        {
            DataFactory.ExecuteNonQuery("update KS_CosOrderItem set Freight = " + (goodsmoney * nums * 0.01) + " where OrderID = '" + KSCMS.S("orderid") + "'");
        }
        else
        {
            DataFactory.ExecuteNonQuery("update KS_CosOrderItem set Freight = 0 where OrderID = '" + KSCMS.S("orderid") + "'");
        }
		Msg.Alert("恭喜,订单修改成功!", BasicField.GetUserUrl("teacher/TcOrderList"));
		isLoadPage = false;
		return;
	}
	protected string Gettype(int _type)
	{
		string Pass = string.Empty;
		switch (_type)
		{
			case 0:
				Pass = "网授";
				break;
			case 1:
				Pass = "面授";
				break;
			case 2:
				Pass = "限时";
				break;
			case 3:
				Pass = "团购";
				break;
			case 4:
				Pass = "积分";
				break;
			case 5:
				Pass = "直播";
				break;
            case 10:
                Pass = "预约直播";
                break;
		}
		return Pass;
	}
	public string GetUrl(int ordertype, int courseID)
	{
		#region 连接地址
		string url = string.Empty;

		if (ordertype == 1)
		{
			url = BasicField.GetRewriteUrl("faceview", courseID);
		}
		else if (ordertype == 5)
		{
			url = BasicField.GetRewriteUrl("liveview", courseID);
		}
        else if (ordertype == 10)
        {
            url = BasicField.GetRewriteUrl("bespeakinfo", courseID);
        }
        else
		{
			url = BasicField.GetRewriteUrl("courseview", courseID);
		}
		return url;
		#endregion
	}
	public string GetParam(string Fields)
	{
		string result = string.Empty;
		if (drr != null)
		{
			result = drr[Fields].ToString();
		}
		return result;
	}

</script>
<%
    if (!isLoadPage) return;
	if(!KSCMS.S("do").Equals("modify")){
%>

	<script src="../../ks_inc/DatePicker/WdatePicker.js"></script>
	 
	
	    <div class="t_tybox">
	   
        <div class="clear blank10"></div>
		      <!--搜索——标题 -->
		<div class="tytitle clearfix">
            <h4 class="lf">订单管理</h4>
            <div class="top_c_sc mt10">
            	<form method="post" action="<%=BasicField.GetUserUrl("teacher/TcOrderList") %>">
                    <span style="float:right; padding-right:0px">
                      <span>课程名称：</span>
                      <input name="TextBox1" type="text" id="TextBox1" class="textbox width140" style="border-radius:5px;" />
                      <span style="margin-left:10px">时间：</span>
                      <input name="BeginDate" type="text" id="BeginDate" class="textbox width140" onclick="WdatePicker({dateFmt:&#39;yyyy-MM-dd&#39;});" style="border-radius:5px;" />
                      <span style="margin-left:10px">至</span>
                      <input name="EndDate" type="text" id="EndDate" class="textbox width140" onclick="	WdatePicker({dateFmt:&#39;yyyy-MM-dd&#39;});" style="border-radius:3px;" />
                      <input type="submit" name="Button1" value=" 搜索 " id="Button1" class="button" style="margin-left:10px;border:1px solid #dce2e4;border-radius:5px; width:auto;" />
                   </span>      
               </form>
            </div>
		</div>    
		 <!--end -->
        <div class="tabs">
            <ul class="clearfix">
                <li <%=KSCMS.S("Status")==""?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("teacher/TcOrderList") %>"><span>所有订单</span></a></li>
                <li <%=KSCMS.S("Status")=="1"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("teacher/TcOrderList","status-1") %>">今日订单</a></li>
                <li <%=KSCMS.S("status")=="2"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("teacher/TcOrderList","status-2") %>"><span>点播已付款</span></a></li>
                <li <%=KSCMS.S("status")=="3"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("teacher/TcOrderList","status-3") %>"><span>面授已付款</span></a></li>
               <li <%=KSCMS.S("status")=="4"?" class='puton'":string.Empty %>><a href="<%=BasicField.GetUserUrl("teacher/TcOrderList","status-4") %>"><span>直播已付款</span></a></li>
            </ul>
        </div>
  
           <table id="tablist" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
            <tr align="center" class="title">
                <td width="8%" align="center">状态</td>
                <td width="20%">课程名称</td>
                <td width="15%" align="center" >订单号</td>
                <td width="8%">姓名</td>
                <td width="8%">提现价</td>
                <td width="8%">成交价</td>
                <td width="10%">购买会员</td>
                <td width="10%">购买时间</td>              
                <td width="13%">操作</td>              
              </tr>
    <%
		             int recordcount, pagecount;
					 int CurrPage = new BasicField().GetCurrPage;
					 int PageSize = 10;
					 string sqlParam = " KS_CosOrder a inner join KS_CosOrderItem b on a.orderid=b.orderid  Where b.business = '" + UserManage.GetUserInfo().UserName + "'  AND a.OrderFlag!=1";
					 if (!string.IsNullOrEmpty(KSCMS.S("TextBox1")))
					 {
						 sqlParam += " and b.cname like '%" + Utils.ChkSQL(KSCMS.S("TextBox1")) + "%'";
					 }
					 if (!string.IsNullOrEmpty(KSCMS.S("BeginDate")))
					 {
						 sqlParam += " and a.InputTime >='" + Utils.ChkSQL(KSCMS.S("BeginDate")) + "'";
					 }
					 if (!string.IsNullOrEmpty(KSCMS.S("EndDate")))
					 {
						 sqlParam += " and a.InputTime <='" + Utils.ChkSQL(KSCMS.S("EndDate")) + "'";
					 }
					 int Status = Utils.StrToInt(KSCMS.S("Status"));

					 if (!string.IsNullOrEmpty(KSCMS.S("Status")))
					 {
						 switch (Status)
						 {
							 case 1:
								 sqlParam += "  and datediff(day,[InputTime],getdate())=0 ";
								 break;
							 case 0:
								 sqlParam += " and b.OrderType <>4 ";
								 break;
							 case 2:
								 sqlParam += " and (a.status=1 or a.PayStatus=2) and b.OrderType=0 ";
								 break;
							 case 3:
								 sqlParam += " and a.status=1 and b.OrderType=1 ";
								 break;
							 case 4:
								 sqlParam += " and a.status=1 and b.OrderType=5 ";
								 break;
                             case 10:
                                 sqlParam += " and a.status=1 and b.OrderType=10 ";
                                 break;
						 }
					 } 
					 using (DataTable dt = DataFactory.GetPage("b.Courseid,b.CName,b.ordertype,b.OrderTotalPrice,b.UserName,a.realname,a.scoretf,a.orderscore,a.mobile,a.GoodsMoney,a.Status,a.IsAnonymous,a.id,a.OrderID,a.InputTime,b.Freight", sqlParam, "a.id", " order by a.id desc,a.PayTime desc", CurrPage, PageSize, out recordcount, out pagecount))
					 {
						 if (dt.Rows.Count > 0)
						 {
							 foreach (DataRow dr in dt.Rows)
							 {
								 KSCMS.W(" <TR class=\"tdbg sort bar\"> <td style=\"display:none\"><span> <input type=\"checkbox\" class=\"checkbtn\" style='display: " + (Convert.ToString(dr["status"]) == "1" ? "none" : "inline") + "' name=\"ids\" id=\"ids\" value=\"" + dr["id"].ToString() + "\" /></span></td>" +
				 "<td>" + (Convert.ToString(dr["status"].ToString()) == "1" ? "<font color=green>已付</font>" : "<font color=red>未付</font>") + "</td><td style=\"text-align: left;\">" +
				 "<a href=\"" + GetUrl(Convert.ToInt16(dr["ordertype"].ToString()), Convert.ToInt32(dr["Courseid"].ToString())) + "\" target=\"_blank\">" + dr["CName"].ToString() + "</a>" +
				 "( " + Gettype(Convert.ToInt16(dr["ordertype"].ToString())) + ")</td>" +
				 "<td class=\"time-color\">" + dr["OrderID"].ToString() + "</td><td >" + dr["RealName"].ToString() + "</td>" +
				 "<td > <div class=\"price_cor\">¥" + Kesion.APPCode.Public.ReturnShopPrice(Utils.StrToDecimal(dr["Freight"].ToString())) + "</div> </td>" +
                 "<td ><div class=\"price_cor\">" + (Utils.StrToInt(dr["scoretf"].ToString()) == 0 ? (Utils.StrToInt(dr["orderscore"].ToString()) > 0 ? dr["orderscore"].ToString() + MyCache.GetCacheConfig(90) + Kesion.APPCode.Public.ReturnShopPrice(Convert.ToString(dr["OrderTotalPrice"])) : "" + "¥" + Kesion.APPCode.Public.ReturnShopPrice(Utils.StrToDecimal(dr["GoodsMoney"]))) : dr["orderscore"].ToString() + MyCache.GetCacheConfig(90)) + "</div></td>" +
				 "<td class=\"time-color\">" + dr["UserName"].ToString() + " </td><td class=\"time-color\"> " + dr["InputTime"].ToString() + "</td>" +
				" <td  class=\"splittd Ctd\" align=\"center\"><div style=\"display:" + (dr["status"].ToString() == "1" ? "none" : "") + "\" class=\"gl_btn\">" +
				"<span style=\"width:50%\"><a  title=\"编辑\" class=\"edit\" href=\"" + BasicField.GetUserUrl("teacher/TcOrderList", "do-modify,ids-" + dr["id"].ToString() + "") + "\"><i class=\"iconfont icon-xiazai48\"></i>编辑</a></span>" +
				"<span style=\"width:50%\"><a onclick=\"return(confirm(确定删除该订单吗?));\"  title=\"删除\" class=\"delete\" href=\"" + BasicField.GetUserUrl("teacher/TcOrderList", "do-del,ids-" + dr["id"].ToString() + "") + "\"><i class=\"iconfont icon-lajitong-copy\"></i>删除</a></span>" +
				"</div></td></TR>");
								 if (KSCMS.S("Status") == "0")
								 {
									 sum = sum + Utils.StrToFloat(dr["GoodsMoney"].ToString()) * Utils.StrToFloat(dr["IsAnonymous"].ToString()); //商品价格
								 }
								 else
								 {
                                     sum = sum + Utils.StrToFloat(dr["Freight"].ToString()); //商品价格  //份成价格
								 }
								 if (Utils.StrToInt(dr["ordertype"].ToString()) == 4)
								 {
									 sum = 0;
								 }
							 }
							 KSCMS.W(" <TR class=\"tdbg sort bar\" > <td style=\"text-align:right;padding:10px 0\"  colspan=\"9\">" +
				"合计：<span  style=\"font-size:22px;color:#F34E29;margin-right: 10px;\">￥" + sum + "元</span></td> </tr>");
						 }
						 else
						 {
							 KSCMS.W(" <tr class=\"tdbg\"><td class=\"Ctd\" style=\"text-align:center\" colspan=\"9\"><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="clearfix"></div>
     
      </div>
      <div>
        <script type="text/javascript">
            showtips(' <span>操作说明：<br /></span><span style="display:block;margin-top:5px;color:#dd5232">已付款的订单不能删除！</span>');
		</script>
	  </div>
	  <%}else{ %>
	   <script src="../../ks_inc/weiboApi.js"></script>
	   <form method="post" action="<%=BasicField.GetUserUrl("teacher/TcOrderList") %>">
	   <input type="hidden" name="ids" value="<%=KSCMS.S("ids") %>" />
	   <div class="t_tybox">  
		 <div class="ty_title">修改订单</div>
		 <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border"> 
			 <tr>
				<td align="right" class="lefttd" height="30" width="120">课程名称：</td>
				 <td class="tag_left">
					  <span id="Label1"><%=GetParam("cname") %></span>
					 <span></span>
				 </td>
			 </tr>
			 <tr>
				<td align="right" class="lefttd" height="30" >最终价格：</td>
				 <td class="tag_left">
				 <input name="GoodsMoney" type="text" value="<%=Kesion.APPCode.Public.ReturnShopPrice(Utils.StrToDecimal(GetParam("GoodsMoney"))) %>" id="GoodsMoney" class="textbox nopadtext" style="width:80px;" /> 元
				 </td>
			 </tr>  
			 <tr>
				<td align="right" class="lefttd" height="30" >购买学员：</td>
				 <td class="tag_left">
				 <span id="Label2"><%=GetParam("username") %></span>
				 </td>
			 </tr>
			 <tr>
				<td align="right" height="30" class="lefttd">订单编号：</td>
				 <td class="tag_left">
					 <span id="Label3"><%=GetParam("orderid") %></span>
                     <input name="orderid" type="hidden" value="<%=GetParam("orderid") %>" />
				  </td>
			 </tr>    
		</table>
		 <div class="cz_btn nofloat t-a-center">
			<span>
			<input type="hidden" value="save" name="action" />
			<input type="submit" value="确定修改(O)" onclick="return (CheckForm());"  class="button" />
			</span>
			<span><input type="button" class="button" onclick="window.history.go(-1);"value=" 返 回 "></span>
		 </div>
	 </div>
	 
	 
	 
	 </form>
	  <%} %>