<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Config" %>
<%@ Import Namespace="Kesion.Components" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Kesion.Course" %>
<script runat="server"  type="text/C#">

    int Id;
    double avail;//可用金额
    int courseid;//课程ID 
    DataRow dr = null;
    float money = 0;
    float isfreemoney = 0;
    string orderId = string.Empty;
    string _TypeName = string.Empty;
    string _type = string.Empty;
    string vmoney = string.Empty;
    string Label3 = string.Empty;
    string Label4 = string.Empty;
    string Label2 = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserLogin.Run();
        pay();
        if (KSCMS.S("pay") == "pay")
        {
            losemoney();//支付操作
        }
       
       
    }
    private void losemoney() 
    {
        bool cos = false;
        cos = cosorder(Utils.StrToFloat(KSCMS.S("vmoney")), orderId, KSCMS.S("Label3"));

        if (cos)  //判断扣款
        { 
            int EffectiveDay = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 EffectiveDay from KS_EnterPriseSpread where orderId='" + orderId + "'"));
            Hashtable ht = new Hashtable();
            ht.Add("ispay", 1);
            ht.Add("paydate", DateTime.Now);
            ht.Add("EndDate", DateTime.Now.AddDays(EffectiveDay));
            CommonData.Update("KS_EnterPriseSpread", ht, "where orderId='" + orderId + "'");
            DataFactory.ExecuteNonQuery(string.Format("Update  KS_Course Set {0}=1 where courseid={1}", _type, courseid));
            Msg.Alert("支付成功！", BasicField.GetUserUrl("teacher/UserEpSpread"));
        }
        else
        {
            Msg.Alert("金额不够请充值！",  BasicField.GetUserUrl("basic/PayOnline"));
        }
    }
    private void pay()
    {
        orderId = Utils.RemoveXss(Utils.ChkSQL(KSCMS.S("orderId")));

        avail = UserManage.GetUserInfo(new TemporaryVar().UserID).Money;//可用金额     

        if (string.IsNullOrEmpty(orderId))
        {
            //Utils.ShowError("参数出错啦!", 1);
            Msg.Alert("参数出错啦!",KSCMS.GetComeUrl());
            return;
        }
        using (DataTable dt = DataFactory.GetDataTable("select top 1 a.*,b.name  from KS_EnterPriseSpread a inner join KS_Course b on a.courseid=b.courseid where a.orderid='" + orderId + "'"))
        {
            if (dt.Rows.Count <= 0)
            {
                Msg.Alert("找不到订单无法支付!", KSCMS.GetComeUrl());
                return;
            }
            else
            {
               // this.Label1.Text = (new TemporaryVar()).UserName;
                string itemstr = string.Empty;
                DataRow drr = dt.Rows[0];
                itemstr = drr["name"].ToString();
                money = Utils.StrToFloat(drr["TotalMoney"].ToString());
                courseid = Utils.StrToInt(drr["courseid"].ToString());
                vmoney = money.ToString();
                Label2 = orderId;
                Label3 = itemstr;
                Label4 = GetType(Utils.StrToInt(drr["type"].ToString()));
                _TypeName = GetType(Utils.StrToInt(drr["type"].ToString()));
            }
        }
    }

    protected string GetType(int num)
    {
        string _Type = string.Empty;

        switch (num)
        {
            case 1:
                _Type = "课程推荐";
                _type = "Recommend";
                break;
            case 2:
                _Type = "课程精彩";
                _type = "popular";
                break;
            case 3:
                _Type = "课程固顶";
                _type = "istop";
                break;
            case 4:
                _Type = "课程幻灯";
                _type = "Huandeng";
                break;
            case 5:
                _Type = "手机同步";
                _type = "is3g";
                break;
        }
        return _Type;
    }
    //  扣款处理
    protected bool cosorder(double money, string orderid, string name)
    {
        bool iscount = false;
        if (money <= 0)
        {
            Msg.Alert("支付金额不能为零！");
            return false;
        }
        else if (avail >= money)
        {
            iscount = Charge.MoneyInOrOut(UserManage.GetUserInfo().UserID, UserManage.GetUserInfo().RealName, money, 4, 2, orderid, "system", "用于支付推广《" + name.ToString() + "》的【" + _TypeName + "】属性", -5000, 0, 1);
        }
        if (iscount)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


</script>
  <div ID="PanList"  >
  
 <div class="t_tybox">
	 <div class="tytitle"><h4>确认余额支付</h4></div>
    <div class="clear"></div>
     <form action="{$GetCurrUrl}" method="post">
         <input type="hidden" name="pay" value="pay" />
<table  cellspacing="1" cellpadding="3" class="border" width="100%" align="center" border="0">
    <tr>
       <td class="title" align="left" style="font-size:16px" colspan="2">
         <span class="fl" style="padding-left:70px">当前可用余额 <font color="red" ><%=avail %> 元</font></span>
         </td>
     </tr>
    <%if (!string.IsNullOrEmpty((new TemporaryVar()).UserName)){ %>
     <tr class="tdbg">
         <td class="splittd w120 lefttd" style="height: 25px">用 户 名： </td>
         <td class="splittd" style="text-align:left;">
          <span ID="Label1"   Text=""><%=(new TemporaryVar()).UserName %></span>
         </td>
     </tr>
    <%} %>
     <tr class="tdbg">
         <td class="splittd w120 lefttd" style="height: 25px">支付编号： </td>
         <td class="splittd" style="text-align:left;">
          <span ID="Label2"   Text="0"><%=Label2 %></span>
         </td>
     </tr>
     <tr class="tdbg">
         <td class="splittd w120 lefttd" style="height: 25px">推广课程： </td>
         <td class="splittd" style="text-align:left;"><span class="tips"><span ID="Label3"   Text="0"><%=Label3 %></span>
          </span>
         </td>
     </tr>

     <tr class="tdbg">
         <td class="splittd w120 lefttd" style="height: 25px">推广类型： </td>
         <td class="splittd" style="text-align:left;"><span class="tips"><span ID="Label4"   Text="0"><%=Label4 %></span>
          </span>
         </td>
     </tr>

    <tr class="tdbg">
         <td class="splittd w120 lefttd" style="height: 25px">支付金额：</td>
         <td class="splittd" style="text-align:left;">
             <%if  (money==0 ){ %>
          ￥ <span style="font-size:16px;font-weight:bold;color:#ff6600">
            <%=Kesion.APPCode.Public.ReturnShopPrice(Utils.StrToDecimal((money)).ToString()) %>
          </span> 元
             <%}
               else if (money > isfreemoney)
               { %>
            本单总金额￥<span style="font-size:16px;font-weight:bold;color:#ff6600">
                <%=Kesion.APPCode.Public.ReturnShopPrice(Utils.StrToDecimal((money)).ToString()) %></span>元，
             本次抵用：￥ <span style="font-size:16px;font-weight:bold;color:#ff6600"><%=money>isfreemoney?isfreemoney:0%></span> 元
             本次支付订金：￥ <span style="font-size:16px;font-weight:bold;color:#ff6600">
              <span ID="vmoney"  Text="Label"><input name="vmoney"  type="hidden" value="<%=(money-isfreemoney)%>"  /><%=(money-isfreemoney)%></span>
                 </span> 元
           <%} %>
         </td>
     </tr>
     <tr class="tdbg">
         <td colspan="2" style="height:40px;text-align: left;padding-left:70px" >
             <input  type="submit" ID="Button1"   value="确定支付"  class="button1"/>
         </td>
     </tr>
    </table>
         </form>
</div>
    </div>

