<%@ 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" %>
<%@ Import Namespace="Kesion.XML" %>
<script runat="server" type="text/C#">

    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息

    int id = 0;
    float wang = 0;
    float face = 0;
    float shenq = 0;
    float sum = 0;
    int EpId = 0;

    int recordcount, pagecount;
    int PageIndex = new BasicField().GetCurrPage;
    int pagesize = 10;
    DataRow drr = null;//修改初始化使用
    public int groupID = 3;
    string Nums = "0";  //提现手续费

    private string GetVal(string fieldName)
    {
        if (drr != null)
        {
            return drr[fieldName].ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        id =Utils.StrToInt(KSCMS.S("id"));
        //num !=1 包括 普通网授  团购 限时  不包括面授 和积分
        EpId = EnterPrise.GetEpId(UserManage.GetUserInfo().UserID, UserManage.GetUserInfo().UserName);
        if (new TemporaryVar().GroupID == 4)
        {
            groupID = 4;
        }
        if (groupID == 3)
        {

            sum = Utils.StrToFloat(DataFactory.GetOnlyValue("select sum(b.Freight) from KS_CosOrder a inner join KS_CosOrderItem b on a.orderid=b.orderid   where b.SchoolID=" + new TemporaryVar().UserID + " and a.status=1"));
        }
        else if (groupID == 4 && new TemporaryVar().SchoolID==0)
        {
            sum = Utils.StrToFloat(DataFactory.GetOnlyValue("select sum(b.Freight) from KS_CosOrder a inner join KS_CosOrderItem b on a.orderid=b.orderid   where b.AttrID=2 and b.Business='" + new TemporaryVar().UserName + "' and a.status=1"));

        }

        //已提金额
        face = Utils.StrToFloat(DataFactory.GetOnlyValue("select sum(Money) from  KS_EnterPriseAccount  where  tcid=" + (groupID == 3 ? 0 : 1) + " and  username='" + new TemporaryVar().UserName + "'"));
        //可提金额
        wang = sum - face;
        if (wang <= 0) wang = 0;
        if (groupID == 4)
        {
            string status = XmlObject.ReadXmlReturnNode(Utils.GetMapPath(MyCache.GetCacheConfig(0) + "config/course/teacher.config"), "//TeacherScoreConfig/DivideStatus");
            if (status == "1") //开启分成计算是否需要手续费
            {
                Nums = XmlObject.ReadXmlReturnNode(Utils.GetMapPath(MyCache.GetCacheConfig(0) + "config/course/teacher.config"), "//TeacherScoreConfig/Nums");

                if (Nums == "0")
                {
                    __info.Label5 = "无";
                }
                else
                {
                    __info.Label5 = Nums;
                }
            }
            else
            {
                __info.Label5 = "已关闭";
            }
        }
        else
        {
            if (Utils.StrToInt(new Kesion.Components.EpConfig().GetEpConfig(13)) == 1) //开启了分成
            {

                Nums = XmlObject.ReadXmlReturnNode(Utils.GetMapPath(MyCache.GetCacheConfig(0) + "config/course/EpSpace.config"), "//EpConfigInfo/Nums");
                if (Nums == "0")
                {
                    __info.Label5 = "无";
                }
                else
                {
                    __info.Label5 = Nums;
                }
            }
            else
            {
                __info.Label5 = "已关闭";
            }
        }

        __info.Label2 = sum.ToString("f2");
        __info.Label3 = face.ToString("f2");
        __info.Label4 = wang.ToString("f2");
        __info.Label1 = wang.ToString("f2");
        __info.Button2 = "确定保存";
        if (KSCMS.S("addorupdate") != "")
        {
            addinfo();//新增  以及修改操作
        }
        if (KSCMS.S("action") == "modify")
        {
            initialEdit(Utils.StrToInt(KSCMS.S("id")));//修改初始化
        }
        if(KSCMS.S("action")=="delete")
        {
            deleteSj(KSCMS.S("ids"));//删除
        }

    }
    //编辑初始化
    private void initialEdit(int id)
    {
        __info.Button2 = "确定修改(O)";

        SqlParam[] param = new SqlParam[1];
        param[0] = new SqlParam("id", id);

        using (DataTable dt = CommonData.GetDataTable("[KS_EnterPriseAccount]", "*", param))
        {
            if (dt.Rows.Count > 0)
            {
                drr = dt.Rows[0];
            }
        }
    }

    //添加信息
    private void addinfo()
    {

        float Money = Utils.StrToFloat(KSCMS.S("TxtMoney"));
        float realmoney = Money;
        float chargeMoney = 0;
        if (Utils.StrToFloat(Nums) > 0)
        {
            chargeMoney = Utils.StrToFloat(realmoney * Utils.StrToFloat(Nums) * 0.01);
            Money += chargeMoney;

            //加上手费费用
        }

        if (string.IsNullOrEmpty(KSCMS.S("TxtTitle")))
        {
            Msg.Alert("请输入标题!",KSCMS.GetComeUrl());
            return;
        }

        if (Money > sum && KSCMS.S("addorupdate").Equals("0"))
        {
            Msg.Alert("申请提现金额不可大于实际到账金额!", KSCMS.GetComeUrl());
            return;
        }

        if (Utils.StrToFloat(KSCMS.S("TxtMoney")) < 0.01)
        {
            Msg.Alert("提现金额有误!",KSCMS.GetComeUrl());
            return;
        }
        Hashtable ht = new Hashtable();

        ht.Add("username", new TemporaryVar().UserName);//机构         
        ht.Add("wayType", KSCMS.S("radioway"));//方式        
        ht.Add("Title", Utils.ChkSQL(Utils.RemoveXss(KSCMS.S("TxtTitle"))));//标题       

        ht.Add("Money", Money);//支付金额      

        ht.Add("realMoney", realmoney);//到账金额     

        ht.Add("chargeMoney", chargeMoney);//手费费金额      

        ht.Add("Applicant", Utils.RemoveXss(KSCMS.S("Applicant")));//申请人    
        ht.Add("phone", Utils.RemoveXss(KSCMS.S("phone")));//申请电话    
        ht.Add("BankAccount", Utils.RemoveXss(KSCMS.S("TxtBankAccount")));//银行       
        ht.Add("AccountNumber", Utils.RemoveXss(KSCMS.S("TxtAccountNumber")));//账号       
        ht.Add("Remarks", KSCMS.S("Remarks"));//备注            
        ht.Add("tcid", (groupID == 3 ? 0 : 1));//1为名师订单提现 
        ht.Add("epid", EpId);
        if (KSCMS.S("addorupdate").Equals("0"))
        {
            ht.Add("userID", new TemporaryVar().UserID);
            ht.Add("adddate", DateTime.Now);
            if (KSCMS.S("radioway") == "2" && Money <= wang)
            {
                ht.Add("verify", 0); //转账
                ht.Add("status", 0);  //审核
            }
            else
            {
                ht.Add("verify", 0); //转账
                ht.Add("status", 0);  //审核
            }
            CommonData.Insert("KS_EnterPriseAccount", ht);
            Msg.Alert("恭喜,添加成功!", BasicField.GetUserUrl("order/UserEpAccount"));
        }
        else
        {
            CommonData.Update("KS_EnterPriseAccount", ht, "where id=" + id);
            Msg.Alert("恭喜,修改成功!", BasicField.GetUserUrl("order/UserEpAccount"));
        }
    }
    //删除
    private void deleteSj(string id)
    {
        if (id == null || id == "0") return;
        id = Utils.FilterIDs(id);
        using (DataTable dt = DataFactory.GetDataTable("select top 30 id from KS_EnterPriseAccount Where id in(" + id + ")"))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("id", Utils.StrToInt(dr["id"].ToString()));
                    ht.Add("status", 0);
                    CommonData.Delete("[KS_EnterPriseAccount]", ht);
                }
            }
        }
        Msg.Alert("恭喜,删除成功!", KSCMS.GetComeUrl());
    }

    protected string GetVerify(int num)
    {
        return Teacher.GetVerify(num);
    }
    protected string GetWay(string n)
    {
        return Teacher.GetWay(n);
    }
</script>
<%if(KSCMS.S("action")==""){ %>
<div ID="ListPanel"  Width="100%">  
	<div class="t_tybox">
		<div class="tytitle clearfix"><h4 class="fl lf">订单结算</h4><a  href="<%=BasicField.GetUserUrl("order/UserEpAccount","action-add") %>" class="addbtn mt7 fl">+申请提现</a>
	 	<div class="orderBoxCon fr" style="line-height:40px;height:40px;">
            管理员设置提现手续费百分之: <span ID="Label5"  ForeColor="Green" Text=""><%=__info.Label5 %></span>
            &nbsp; &nbsp; 当前账户总额：<span ID="Label2"  ForeColor="Green" Text="0"><%=__info.Label2 %></span><font color=Green>元</font>
            &nbsp; &nbsp; 已经申请提现: <span ID="Label3"  ForeColor="Green" Text="0"><%=__info.Label3 %></span><font color=Green>元</font>
            &nbsp; &nbsp; 可提现：<span ID="Label4"  class="red_cor" Text="0" style=" font-size:30px"><%=__info.Label4 %></span><span class="red_cor">(直播道具：<%=Kesion.APPCode.Public.CashMoneys(__user.UserName) %> 点播道具：<%=Kesion.APPCode.Public.CashMoneys(__user.UserName,2) %>)</span><font class="red_cor">元</font>
        </div>
	  </div>
	<div class="clear"></div>      
           <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="8%" align="center">结算</td>            
                    
                <td width="6%" align="center">审核</td>          
                <td width="8%">结算方式</td>   
				         
                <td  width="18%" align="center">标题</td>
				  <td  width="8%" align="center">支付金额(元)</td>
                  <td  width="8%" align="center">到账金额(元)</td>
				  <td  width="8%" align="center">申请时间</td>
               <td  width="8%" align="center">结算时间</td>
				   <td  width="10%" align="center">操作</td>
              </tr>
               <%
				   string sqlParam = "KS_EnterPriseAccount where tcid=" + (groupID==3?0:1) + " and username='" + UserManage.GetUserInfo().UserName + "' ";
                   using (DataTable dt = DataFactory.GetPage("*", sqlParam, "id", "order by  adddate desc", PageIndex, pagesize, out recordcount, out pagecount))
                   {
                       if (dt.Rows.Count > 0)
                       {
                           int i = 0;
                           foreach(DataRow dr in dt.Rows)
                           {
                               %>
                <TR class="tdbg sort bar">
                 <td align="center" height="30px" nowrap>
               		<%=i+1%>.  
                 </td>
                <td align="center">
                 	<%= GetVerify(Utils.StrToInt(dr["verify"].ToString())) %>   
				</td>				
                <td>				
		        	      <%= Utils.StrToInt(dr["status"].ToString())==0?"未审":Utils.StrToInt(dr["status"].ToString())==1?"<font class='green_cor'>已审</font>":"<font color='red'>审核不通过</font>" %>   
				</td> 
                <td align="center">
                	   <%= GetWay(dr["wayType"].ToString())%>
                </td>              
			    <td align="center">
                    <%=dr["title"]%>
                </td>    
                       <td>
                   	   <%= Utils.StrToFloat( dr["Money"]).ToString("f2")%>
				   	</td>            
                   <td>
                   	   <%= Utils.StrToFloat( dr["RealMoney"]).ToString("f2")%>
				   	</td>
				   <td>
                   	 <%= dr["adddate"] %>
				   	</td>
				      <td>
                   <%= dr["paydate"] %>    
				   	</td>
				    <td>
                    	
                        <div class="gl_btn">
                   			<span class="span1" style="width:50%"><a href="<%=BasicField.GetUserUrl("order/UserEpAccount","action-modify,id-"+dr["id"]) %>" ><i class="icon iconfont icon-chakan"></i>查看</a></span>         
                    		<span class="span5" style="width:50%" <%= (Utils.StrToInt(dr["status"])>0||Utils.StrToInt(dr["verify"])>0) ? "hidden":"" %>><a href="<%=BasicField.GetUserUrl("order/UserEpAccount","action-delete,ids-"+dr["id"]) %>"  OnClick="return(confirm('确定删除吗?'));"><i class="icon iconfont icon-lajitong-copy"></i>删除</a></span>
                         </div>   
				   	</td>
             </TR>	
               <%
                               i++;
                           }
                       }
                       else {
                           KSCMS.W(@" <TR class=""tdbg sort bar"" >
                                        <td colspan=""10"">
                 	                        <div class=""empty"">没有任何提现记录</div>
                                        </td>
                                       </TR> ");
                       }
                   }
                    %>
           </table>

		
    	<div class="fy_box">
            <%
            #region 分页
            string PageStr = "[KS:PageStr1]";
            BasicField.ReplaceFilterPage(ref PageStr, PageIndex, pagesize, pagecount, recordcount);
            KSCMS.W(PageStr);
            #endregion
         %>
    	</div>
    	<div class="clear "></div>
   
    </div>
     

    </div>
        <div style="padding-top:10px" >
			<script type="text/javascript">
			    showtips('<span>操作说明：<br /></span><span class="tips_cor" style="display:block;margin-top:5px;">已申请的结算不能编辑，已审核的申请不能删除，请谨慎操作！</span>');
			</script>
	    </div>
<%} if (KSCMS.S("action") == "add" || KSCMS.S("action") == "modify")
  { %>
<div ID="AddPanel"  Width="100%">
      
    <form action="{$GetCurrUrl}" method="post">
        <input type="hidden" name="addorupdate" value="<%=KSCMS.S("action")=="add"?"0":"1" %>" />
    <div class="t_tybox">	
	<div class="tytitle"><h4>申请提现</h4></div>
     <table width="100%" border="0" cellpadding="0" cellspacing="0" class="border">    
     <tr>
          <td class="lefttd" align="right" width="120">结算方式：</td>
          <td class="tag_left">
          <table class="radio">
          <tr>
          <td style="min-width:80px"> <input type="radio" class="radio" name="radioway" id="count" Value="0" onclick="free(this.value)"<%=GetVal("wayType")==""||GetVal("wayType")=="0"?"checked":"" %> /><label for="count">银行转账</label></td>
          <td style="min-width:80px"><input type="radio" id="count1" class="radio" name="radioway" Value="1" onclick="free(this.value)" <%=GetVal("wayType")=="1"?"checked":"" %>/><label for="count1"> 线下现金</label></td>
          <td style="min-width:80px;display:none"><input type="radio" id="count2" class="radio" name="radioway" Value="2" onclick="free(this.value)" <%=GetVal("wayType")=="2"?"checked":"" %> /><label for="count2">转入预存款</label></td>
          </tr>
          </table>              
         </td>          
     </tr>
   <tr>
        <td class="lefttd">结算标题：</td>
         <td class="tag_left"><input type="text" name="TxtTitle" value="<%=GetVal("Title") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %> ID="TxtTitle"  Class="textbox" />
             <span class="tips">如:1月份结算清单</span>
         </td>
     </tr>
 <tr>
        <td align="right" class="lefttd" height="30" ><span style="color:red;">*</span>结算金额：</td>
         <td class="tag_left"><input type="text" name="TxtMoney" value="<%=Utils.StrToFloat(GetVal("Money")).ToString("f2") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %>  ID="TxtMoney" Class="textbox" />
           <span class="tips">可申请金额[<span ID="Label1"  ForeColor="Green" Text="0"><%=__info.Label1 %></span>]元

             <b style="color:red;"> 注：提现手续费<%=Nums %>%,实际到账<i id="actualmoney">0</i>元</b>
           </span>
         </td>
     </tr>
    <tbody id="pricebody" style=" display:none">
       <tr>
        <td align="right" class="lefttd" height="30" ><span style="color:red;">*</span>开户人：</td>
         <td class="tag_left"><input type="text" name="Applicant" value="<%=GetVal("Applicant") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %> ID="Applicant" Class="textbox" /></td>
     </tr>
       <tr>
        <td align="right" class="lefttd" height="30" ><span style="color:red;">*</span>开户银行：</td>
         <td class="tag_left"><input type="text" name="TxtBankAccount" value="<%=GetVal("BankAccount") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %> ID="TxtBankAccount" Class="textbox" /><span style="color:red;">请填写详细的开户支行，以便提现审核通过</span></td>
     </tr>
     <tr>
        <td align="right" class="lefttd" height="30" ><span style="color:red;">*</span>银行账号：</td>
         <td class="tag_left"><input type="text" name="TxtAccountNumber" value="<%=GetVal("AccountNumber") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %> ID="TxtAccountNumber" Class="textbox"  /></td>
     </tr>
            
      <tr>
             </tbody>
     <tr>
        <td align="right" class="lefttd" height="30" ><span style="color:red;">*</span>联系电话：</td>
         <td class="tag_left"><input type="text" name="phone" ID="phone" value="<%=GetVal("phone") %>" <%=KSCMS.S("action") == "modify"?"readonly='readonly'":"" %> Class="textbox" /></td>
     </tr>
     <tr>
        <td align="right" class="lefttd" height="30" >备注：</td>
         <td class="tag_left"><input type="text" name="Remarks" ID="Remarks"  class="textbox" value="<%=GetVal("Remarks") %>"  TextMode="MultiLine" Width="280px" Height="150px" style="width:280px !important;border: 1px solid #dce2e4;" ></input>
            <%Kesion.APPCode.EditorAPI.EchoHead(); %>
            <% Kesion.APPCode.EditorAPI.EchoEditor("editor", "Remarks", "Basic");%>
        </td>
     </tr> 
  
     <tbody id="tbody1"  style="display:none">
     <tr>
        <td align="right" class="lefttd" height="30" >管理员回复：</td>
         <td class="tag_left"><input type="text" name="ReRemarks" value="<%=GetVal("ReRemarks") %>" ID="ReRemarks"  TextMode="MultiLine" Width="80%" Height="400px" />
            <%Kesion.APPCode.EditorAPI.EchoHead(); %>
            <% Kesion.APPCode.EditorAPI.EchoEditor("editor", "ReRemarks", "Basic");%>
        </td>
     </tr>      
      </tbody>        
  </table>
      <div class="clear"></div>
      <div class="cz_btn nofloat  t-a-center">
          <%if(GetVal("status")!="1"||GetVal("verify")!="1"){ %>
      		<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>
 		<div class="clear blank20"></div>
</div>
    </form>
     </div>

<script>
      	function free(checkrad) {
      		if (checkrad == "1") {
      			$("#pricebody").hide();
      		} else {
      			$("#pricebody").show();
      		}
      	}
      	$(document).ready(function () {
      		free(<%=GetVal("wayType")%>);

          });
          $("#TxtMoney").keyup(function () {
              var reg = $(this).val().match(/\d+\.?\d{0,2}/);
              var txt = '';
              if (reg != null) { txt = reg[0]; }
              var poundage = '<%=Nums%>';
              var actualmoney = toMoney(txt - txt * poundage * 0.01);
              $("#actualmoney").html(actualmoney);
              $(this).val(txt);
          }).change(function () {
              $(this).keyup();
          });
          //将数字转换成金额显示
          function toMoney(num) {
              num = num.toFixed(2);
              num = parseFloat(num)
              num = num.toLocaleString();
              if (num == 0) num = '0.00';
              return num;//返回的是字符串23,245.12保留2位小数
          }          function CheckForm() {
              var d = $("input[name='radioway'][type=radio]:checked").val();
              var money = jQuery("#Label1").text();
              var temoeny = jQuery("#TxtMoney").val();

              <%if (KSCMS.S("action") == "modify") {%>
              if (parseFloat(money) < parseFloat(temoeny)) {
                  KesionJS.Alert("提现金额不正确!");
                  return false;
              }
              <%}%>
      		 var title = jQuery("#TxtTitle").val();

              if (title == '') {
                  KesionJS.Alert("请输入标题!");
      				return false;
              }
              if (d == '0') {
                  if ($("#Applicant").val() == "") {
                      KesionJS.Alert("请填写开户人信息!");
                      return false;
                  }
                  if ($("#TxtBankAccount").val() == "") {
                      KesionJS.Alert("请填写开户银行信息!");
                      return false;
                  }
                  if ($("#TxtAccountNumber").val() == "") {
                      KesionJS.Alert("请填写银行账号信息!");
                      return false;
                  }
              }
      			if ($("#phone").val() == "") {
                        KesionJS.Alert("请填写联系电话信息!");
      			    return false;
      			}

      			return true;
      		}
</script>  
<%} %>





