<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.Components" %>
<script runat="server" type="text/C#">
    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
    bool isLoadPage = true;  //定义页面是否加载
    string param = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserLogin.Run();
    }

</script>
<script>
    function Timer(begin, end, obj) {
        if (begin == '' || end == '') return;
        var beginTime = StringToDate(begin);
        var endTime = StringToDate(end);
        var nowTime = new Date();
        var nMS = endTime.getTime() - nowTime.getTime() + difference;
        var myD = Math.floor(nMS / (1000 * 60 * 60 * 24));
        var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
        var myM = Math.floor(nMS / (1000 * 60)) % 60;
        var myS = Math.floor(nMS / 1000) % 60;
        if (myD >= 0) {
            var str = '';
            var left = '';
            if (myD > 0) str = "<span><font class='d'>" + myD + "</font>天</span>";
            if (myH >= 10) {
                str += "<span><font class='h'>" + myH + "</font>:</span>";
            } else {
                str += "<span><font class='h'>0" + myH + "</font>:</span>";
            }
            if (myM >= 10) {
                str += "<span><font class='h'>" + myM + "</font>:</span>";
            } else {
                str += "<span><font class='h'>0" + myM + "</font>:</span>";
            }
            if (myS >= 10) {
                str += "<span><font class='h'>" + myS + "</font></span>";
            } else {
                str += "<span><font class='h'>0" + myS + "</font></span>";
            }
            str = left + str;
        } else {
            var collage = document.getElementById(obj);
            collage.parentNode.removeChild(collage);
        }
        $("#" + obj).html(str);
        setTimeout(function () {
            Timer(begin, end, obj)
        }, 100)
    }
</script>

<%
    if (!isLoadPage) return;
%>

<div class="r_tybox t_tybox nomar clearfix">
    <div class="r_title tytitle  pd0">
        <div class="tabs" style="float: left">
            <ul>
                <li><a href="<%=BasicField.GetUserUrl("basic/BargainList")%>">砍价列表</a></li>
                <li class="puton"><a href="<%=BasicField.GetUserUrl("basic/MyBargain")%>">我的砍价</a></li>

            </ul>
        </div>
    </div>
    <div class="top-sort clearfix">
        <div class="top-sort-tab lf">
            <ul>
            </ul>
        </div>
        <div class="top-sort-option rf">
            <a class="curr" href='<%=BasicField.GetUserUrl("basic/mybargain") %>'>所有记录</a>
            <a href='<%=BasicField.GetUserUrl("basic/mybargain","status-1") %>'>支付成功</a>
            <a href='<%=BasicField.GetUserUrl("basic/mybargain","status-2") %>'>进行中</a>
            <a href='<%=BasicField.GetUserUrl("basic/mybargain","status-3") %>'>失效</a>
            <a href='<%=BasicField.GetUserUrl("basic/mybargain","status-4") %>'>砍价完成未支付</a>
        </div>
    </div>
    <div class="myCourse">
        <ul id="mainCourse">
            <%
                if (Utils.StrToInt(KSCMS.S("status")) > 0)
                {
                    param += " AND Status=" + KSCMS.S("status");
                }
                int recordcount, pagecount;
                int CurrPage = new BasicField().GetCurrPage;
                int PageSize = 10;
                string statusStr = string.Empty;
                string sqlParam = " KS_CosBargain a INNER JOIN KS_CosBargainDetail b ON a.CourseID=b.CourseID WHERE initiatorid=" + new TemporaryVar().UserID + " AND ParentID=0 ";
                string field = "a.ID AS Aid,a.CourseType,a.Name,a.CourseID,a.Price,a.Money,a.ActivityEndTime,a.LimitTime,b.SurplusPrice,b.Status,b.ID,b.AddDate";
                Kesion.HtmlTags.CommonFunc.InitBlogNav(UserManage.GetUserInfo().UserID, (new TemporaryVar()).UserName);

                #region 更新砍价项目状态
                using (DataTable dtt = DataFactory.GetDataTable("SELECT "+field+" FROM "+sqlParam+ param))
                {
                    if (dtt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtt.Rows)
                        {
                            if ((dr["Status"].ToString()=="2"||dr["Status"].ToString()=="4")&&(Utils.StrToDateTime(dr["ActivityEndTime"]) <= DateTime.Now||Utils.StrToDateTime(dr["AddDate"]).AddHours(Utils.StrToFloat(dr["LimitTime"]))<=DateTime.Now)||!DataFactory.Exists(dr["CourseType"].ToString() == "1" ? "KS_CourseFace" : "KS_Course", " CourseID=" + dr["CourseID"].ToString()))  //砍价活动失效
                            {
                                DataFactory.ExecuteNonQuery("UPDATE KS_CosBargain SET IsOpen=0 WHERE ID=" + dr["ID"]); //更新砍价课程为失效状态
                                DataFactory.ExecuteNonQuery("UPDATE KS_CosBargainDetail SET Status=3 WHERE CourseID=" + dr["CourseID"].ToString() + " AND Status IN(4,2)"); //更新砍价课程为失效状态
                            }
                        }
                    }
                }
                #endregion

                #region 绑定我的砍价项目
                using (DataTable dt = DataFactory.GetPage(field, sqlParam + param, "b.ID", "order by b.ID desc", CurrPage, PageSize, out recordcount, out pagecount))
                {
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            string courseType = dr["CourseType"].ToString();
                            string cosname = dr["Name"].ToString();
                            string courseid = dr["courseid"].ToString();
                            string costype = courseType == "0" ? "点播课程" : courseType == "1" ? "面授课程" : "直播课程";
                            decimal price = Utils.StrToDecimal(dr["Price"].ToString());             //课程原价
                            decimal surPrice = Utils.StrToDecimal(dr["surplusprice"].ToString());   //当前砍价剩余
                            decimal money = Utils.StrToDecimal(dr["Money"].ToString());             //砍价活动价格
                            decimal currentPrice = surPrice + money;                                //当前课程价格
                            decimal donePrice = price - surPrice - money;                           //已经砍了多少钱
                            statusStr = dr["Status"].ToString() == "2" ? "砍价进行中" : dr["Status"].ToString() == "3" ? "已失效" : dr["Status"].ToString() == "1" ? "已购买" : "砍价完成未购买";                                                                   // string errorpic = "this.src='/user/images/10/nopic_200_120.png'";
                            string learnUrl = MyCache.GetCacheConfig(5) + "/" + (courseType == "1" ? "face" : "course") + "/" + courseid;
                            string buttonStr = dr["Status"].ToString() == "2" ? "<div class='button'><a target='_blank' href='/index.aspx?c=coursebargain&a=bargainpage&courseid=" + courseid + "&id=" + dr["id"] + "'>查看砍价详情</a><a target='_blank' href='/index.aspx?c=coursebargain&a=bargainpage&courseid=" + courseid + "&id=" + dr["id"] + "'>立即购买</a></div>" : dr["Status"].ToString() == "1" ? "<div class='button'><a target='_blank' href='" + learnUrl + "'>去学习</a></div>" : "<div class='button'><a target='_blank' href='" + learnUrl + "'>重砍一次</a></div>";

                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat("<li>");
                            sb.AppendFormat("<div class='image'> <img src='" + DataFactory.GetOnlyValue("SELECT TOP 1 PhotoUrl FROM KS_CosBargain WHERE CourseID=" + dr["CourseID"]) + "' onerror='this.src = '/sysimg/nopic.gif'' alt=''></div>");
                            sb.AppendFormat("<div class='name'><a href='jsvascript:;' title='前台页'>{0}</a></div> ", cosname);
                            sb.AppendFormat("<div class='info'><span>{0}</span></div>", costype);
                            sb.AppendFormat(buttonStr);
                            sb.AppendFormat("<div class='link'>当前课程价格：￥" + Kesion.APPCode.Public.ReturnShopPrice(currentPrice) + "原课程价格：￥" + Kesion.APPCode.Public.ReturnShopPrice(price) + "，已砍￥" + Kesion.APPCode.Public.ReturnShopPrice(donePrice) + "，最低价格￥" + Kesion.APPCode.Public.ReturnShopPrice(money) + "</div><div>" + statusStr + "</div></li>");
                            KSCMS.W(sb.ToString());
                        }
                    }
                    else
                    {
                        KSCMS.W("<div class=\"empty\">暂无记录</div><style>.cz_btn{display:none;}</style>");
                    }
                }
                 #endregion
            %>
        </ul>

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