<%@ 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.XML" %>
<script runat="server" type="text/C#">
    public static string path = MyCache.GetCacheConfig(0) + "Plus/Plus.ShareReturn/ShareConfig.config"; //配置文件的绝对路径
                                                                                                        //每天提现额度
    public static float withdrawdaily = Utils.StrToFloat(XmlObject.ReadXmlReturnNode(Utils.GetMapPath(path), "//shareconfig/withdrawdaily"));
    //余额满多少可以提现     
    public static float money = Utils.StrToFloat(XmlObject.ReadXmlReturnNode(Utils.GetMapPath(path), "//shareconfig/money"));
    //当前用户收益总额
    public static float SumReturn = 0;
    //已提现总金额
    public static float doneWithdraw = 0;
    //当日已提现总金额
    public static float doneDairyWithdraw = 0;
    //可提现
    public static float canWithdraw = 0;
    //收益类型returnType
    public string type = XmlObject.ReadXmlReturnNode(Utils.GetMapPath(path), "//shareconfig/returnType");
    //课程id集合
    public string ids = XmlObject.ReadXmlReturnNode(Utils.GetMapPath(path), "//shareconfig/CourseTemplates");
    int recordcount, pagecount;
    int CurrPage = new BasicField().GetCurrPage;
    int PageSize = 10;
    public string param = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
         Kesion.HtmlTags.PageCache.CleanAllCache();
        SumReturn = Utils.StrToFloat(DataFactory.GetOnlyValue("SELECT sum(returnmoney)  FROM KS_ShareReturnDraw WHERE UserID=" + new TemporaryVar().UserID + " and status=1 and type!=3"));
        doneWithdraw = Utils.StrToFloat(DataFactory.GetOnlyValue("SELECT SUM(withdraw)  FROM KS_ShareReturnDraw WHERE UserID=" + new TemporaryVar().UserID + " And type=3 and status=1"));
        doneDairyWithdraw = Utils.StrToFloat(DataFactory.GetOnlyValue("SELECT SUM(withdraw)  FROM KS_ShareReturnDraw WHERE UserID=" + new TemporaryVar().UserID + " And type=3 and status=1 and DATEDIFF(day,adddate,GETDATE())=0"));
        canWithdraw = SumReturn - doneWithdraw;
        param += " AND CourseID in(" + ids + ") and verify=1";
        if (KSCMS.S("action").Equals("dosave"))
        {
            WithDrawMoney();
        }
    }
    /// <summary>
    /// 验证输入金额格式
    /// </summary>
    /// <param name="moneycheck"></param>
    /// <returns></returns>
    public static bool Money(string moneycheck)
    {
        string monval = @"^([1-9]\d+|[0-9])(\.\d\d?)*$";
        return Regex.IsMatch(moneycheck, monval);
    }

    private void WithDrawMoney()
    {
        #region 提现校验
        float withdraw = Utils.StrToFloat(KSCMS.S("BlogName"));  //自定义输入提现金额
        if (!Money(KSCMS.S("BlogName")) || withdraw <= 0)  //输入格式出错
        {
            Msg.Alert("请输入有效提现金额!");
        }
        else if (SumReturn - doneWithdraw >= money)  //当前余额满足提现条件
        {
            if (doneDairyWithdraw < withdrawdaily)  //当日已提现一部分 不超过限额  还能提现
            {
                if (DataFactory.Exists("KS_ShareReturnDraw", "UserID=" + new TemporaryVar().UserID + " AND Type=3 AND  status=0"))
                {
                    Msg.Alert("存在未审核的提现记录，等待管理员审核进行下一次提现！");
                }
                else if (withdraw > withdrawdaily - doneDairyWithdraw)
                {
                    Msg.Alert("超过每日提现额度,每日限额" + withdrawdaily + "元,今日已提现" + doneDairyWithdraw + "元!");
                }
                else
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("SumReturn", SumReturn);
                    ht.Add("UserID", new TemporaryVar().UserID);
                    ht.Add("UserName", new TemporaryVar().UserName);
                    ht.Add("Withdraw", withdraw);
                    ht.Add("AddDate", DateTime.Now);
                    ht.Add("Type", 3);
                    ht.Add("Status", 0);
                    ht.Add("Remark", new TemporaryVar().UserName + "提现" + withdraw + "元");
                    CommonData.Insert("KS_ShareReturnDraw", ht);
                    Msg.Alert("提交成功，请等待管理员审核");
                }
            }
            else
            {
                Msg.Alert("当日已提现" + withdrawdaily + "元,请明日再来！");
            }
        }
        else
        {
            Msg.Alert("余额达到" + money + "元才能提现，继续加油吧");
        }
        #endregion
    }

</script>

<div class="r_tybox t_tybox nomar clearfix">
    <div class="r_title tytitle  pd0">
        <div class="tabs " style="float: left">
            <ul>
                <li class="puton"><a href="<%=BasicField.GetUserUrl("basic/ShareMain")%>">分享收益</a></li>
                <li><a href="<%=BasicField.GetUserUrl("basic/MyWithdraw")%>">收益详情</a></li>
                <li><a href="<%=BasicField.GetUserUrl("basic/MyWithdrawLog")%>">提现记录</a></li>

            </ul>
        </div>
    </div>
    <style type="text/css">
        .tt {
            margin-left: 10px;
        }
    </style>
   <div class="top-sort clearfix">
       
        <div class="earnings ">
            <ul>
                <li>
                    <span><%=Kesion.APPCode.Public.ReturnShopPrice(SumReturn) %>元</span>
                    <em>总收益</em>
                </li>
                <li>
                    <i><%=Kesion.APPCode.Public.ReturnShopPrice(doneWithdraw) %>元</i>
                    <em>已提现</em>
                </li>
                <li>
                    <i><%=Kesion.APPCode.Public.ReturnShopPrice(canWithdraw) %>元</i>
                    <em>可提现</em>
                </li>
                <li>
                    <input type="submit" value="去提现" onclick="withdraw()">
                </li>
            </ul>
        </div>

       
    </div>
    <div id="share">
        &nbsp;&nbsp;&nbsp;&nbsp;分享任务
     <table id="tablist" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
         <tr align="center" class="title">
             <td align="center">课程</td>
             <td align="center">收益类型</td>
             <td align="center">操作</td>
             <%
                 using (DataTable dt = DataFactoryUser.GetPage("*", "KS_Course WHERE 1=1 " + param, "courseid", "order by courseid desc", CurrPage, PageSize, out recordcount, out pagecount))
                 {
                     if (dt.Rows.Count > 0)
                     {
                         foreach (DataRow dr in dt.Rows)
                         {
             %>
         <tr>
             <td align="center"><%=dr["Name"].ToString() %> </td>
             <td align="center" style="text-align: center"><%=type=="0"?"转发收益":"浏览收益" %> </td>
             <!-- href="\plus\plus.sharereturn\index.aspx?action=share&courseid=<%=dr["courseid"].ToString() %>" -->
             <td align="center"><a class="button" href="javascript:;" onclick="openWin('分享链接','/plus/Plus.ShareReturn/ks.showpage.aspx?action=share&courseid=<%=dr["courseid"].ToString() %>')">去分享</a></td>
         </tr>
         <%
                     }
                 }
                 else
                 {
                     KSCMS.W(@" <tr class=""tdbg "">
                <td class=""splittd"" style=""border:0px;"" colspan=""10"">
                  <div class=""empty"">没有课程记录！<div>
                  <style>.cz_btn{display:none;}</style>
                </td>
           </tr> ");
                 }
             }

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

    <div style="display: none" id="tixian">
        <form name="myform" action="{$GetCurrUrl}" method="post" id="myform">
            <input type="hidden" name="action" value="dosave" />
            <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
                <tr class="tdbg">
                    <td class="lefttd">提现金额：</td>
                    <td class="tag_left">
                        <input type="text" name="BlogName" id="BlogName" class="textbox" />
                       <%-- 账户收益余额到达<%=money %>元可以提现，每天限额<%=withdrawdaily %>元，--%>
                        <span class="tips">Tips:账户收益余额到达<%=money %>元可以提现，每天限额<%=withdrawdaily %>元，提现记录审核后才能进行下次提现！</span>
                    </td>
                </tr>
            </table>
            <div class="cz_btn t-a-center">
                <input type="submit" id="Button1" name="withdraw" class="green-bt bt-w-auto d-inline-block" onclick="return (checkform())" value="提交申请" />
                <input type="button" class="green-bt bt-w-auto d-inline-block" onclick="show()" value="返回" />
            </div>
        </form>
    </div>
</div>
<script>
    //校验是否为数字
    function isNumber(val) {

        var regPos = /^\d+(\.\d+)?$/; //非负浮点数
        var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
        if (regPos.test(val) || regNeg.test(val)) {
            return true;
        } else {
            return false;
        }
    }

    function withdraw() {
        document.getElementById("tixian").style.display = "block";
        document.getElementById("share").style.display = "none";
    }
    function show() {
        document.getElementById("tixian").style.display = "none";
        document.getElementById("share").style.display = "block";
    }
    var box = null;
    var isreload = false;
    function openWin(title, url, reload, width, height) {
        if (width == null) width = 760;
        if (height == null) height = 450;
        isreload = reload;
        box = $.dialog.open(url, {
            id: 'topbox', lock: true, title: title, width: width, height: height, close: function () {
                if (isreload) {
                    top.frames['main'].location.reload();
                }
            }
        });
    }
</script>
