<%@ 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" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web.UI" %>
<script runat="server" type="text/C#">
    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
    private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
    bool isLoadPage = true;  //定义页面是否加载
    protected void Page_Load(object sender, EventArgs e)
    {
        string DoMethod = BasicField.GetParam("do");
        if (string.IsNullOrEmpty(DoMethod))
        {
            DoMethod = KSCMS.S("action");
        }
        switch (DoMethod)
        {
            case "search":
                LearnCards();
                break;
        }
        CheckUserLogin.Run();
    }
    public static string GetShiYong(string limit, int contenttf, int examtf, int coursetf, int packid)
    {
        #region 使用条件
        string str = string.Empty;
        string sql = string.Empty;

        if (Utils.StrToInt(limit) == 0)
        {
            switch (contenttf)
            {
                case 1:
                    str += " <p><span class=\"used_info_tj\">文章模块拥有所有免费查看权限</span></p></br>";
                    break;
                case 2:
                    str += " <p><span class=\"used_info_tj\">部分文章栏目下拥有免费查看权限</span><a style=\"color:red\" href=\"javascript:SeeInfo('" + packid + "')\" ></a></p></br>";
                    break;
            }
            switch (examtf)
            {
                case 1:
                    str += " <p><span class=\"used_info_tj\">考试模块拥有所有免费做题权限</span></p></br>";
                    break;
                case 2:
                    str += " <p><span class=\"used_info_tj\">部分考试栏目下拥有免费做题权限</span><a style=\"color:red\" href=\"javascript:SeeInfo('" + packid + "')\" ></a></p></br>";
                    break;
                case 3:
                    str += " <p><span class=\"used_info_tj\">指定试卷免费</span><a style=\"color:red\" href=\"javascript:SeeInfo('" + packid + "')\" ></a></p></br>";
                    break;
            }
            switch (coursetf)
            {
                case 1:
                    str += " <p><span class=\"used_info_tj\">课程模块拥有所有免费观看权限</span></p>";
                    break;
                case 2:
                    str += " <p><span class=\"used_info_tj\">部分课程栏目下拥有免费观看权限</span><a style=\"color:red\" href=\"javascript:SeeInfo('" + packid + "')\" ></a></p></br>";
                    break;
                case 3:
                    str += " <p><span class=\"used_info_tj\">指定课程免费</span><a style=\"color:red\" href=\"javascript:SeeInfo('" + packid + "')\" ></a></p></br>";
                    break;
            }

        }
        else if (limit == "1")
        {
            str = @" <p><span class=""used_info_tj"">可以查看本站的所有收费项目</span></p>";
        }


        return str;
        #endregion
    }
    protected void LearnCards()
    {
        //LimitType
        var couponNum = Utils.ChkSQL(KSCMS.S("TxtCouponNum"));

        if (string.IsNullOrEmpty(couponNum))
        {
            Msg.Alert("营销卡号必须输入！", KSCMS.GetComeUrl());
            isLoadPage = false;
            return;
        }
        var password = Utils.ChkSQL(KSCMS.S("TxtPassWord"));

        if (string.IsNullOrEmpty(password))
        {
            Msg.Alert("营销卡密码必须输入！", KSCMS.GetComeUrl());
            isLoadPage = false;
            return;
        }
        string sqlStr = "select top 1 u.UseFlag,u.username,u.PassWord, c.* From KS_UserFeeSell u inner join KS_UserFeePackages C on u.packid=c.id where  BeginDate<getdate() and EndDate>getdate() and c.Status=1 and cardNum='" + couponNum + "'";
        using (DataTable dt = DataFactory.GetDataTable(sqlStr))
        {
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                if (Utils.StrToInt(dr["UseFlag"].ToString()) == 0)
                {
                    #region 未使用
                    int LimitType = Utils.StrToInt(dr["coursetf"].ToString());
                    string LimitCourseIds = dr["cosids"].ToString();
                    if (dr["PassWord"].ToString().Equals(password))
                    {
                        if (!string.IsNullOrEmpty(dr["username"].ToString()))
                        {
                            Msg.Alert("对不起，该营销卡已被添加！", KSCMS.GetComeUrl());
                            isLoadPage = false;
                            return;
                        }
                        else
                        {

                            Public.ActiveUserFee(dr, __user.UserID, couponNum, password);   ////激活营销卡号，并做逻辑处理

                            Msg.Alert("恭喜，营销卡添加成功!", KSCMS.GetComeUrl());
                            isLoadPage = false;
                            return;
                        }
                    }
                    else
                    {
                        Msg.Alert("对不起，您输入的密码不正确!", KSCMS.GetComeUrl());
                        isLoadPage = false;
                        return;
                    }
                    #endregion
                }
                else
                {
                    Msg.Alert("对不起，您输入的套餐卡已被使用过!", KSCMS.GetComeUrl());
                    isLoadPage = false;
                    return;

                }
            }
            else
            {
                Msg.Alert("对不起，您输入的营销卡不存在!", KSCMS.GetComeUrl());
                isLoadPage = false;
                return;
            }
        }
    }
    public static void addToRechargeRecords(string orderid, double paymoney, double realmoney, string remark, int platId, int OrderType = 0, int packId = 0)
    {
        if (!string.IsNullOrEmpty((new TemporaryVar()).UserName) && paymoney > 0)
        {
            Hashtable ht = new Hashtable();
            ht.Add("username", (new TemporaryVar()).UserName);
            ht.Add("orderid", orderid);
            ht.Add("paymoney", paymoney);
            ht.Add("realmoney", realmoney);
            ht.Add("adddate", DateTime.Now);
            ht.Add("paytime", DateTime.Now);
            ht.Add("remark", remark);
            ht.Add("platid", platId);
            ht.Add("packId", packId);
            ht.Add("status", 1);
            ht.Add("OrderType", OrderType);  //1课程支付,
            ht.Add("platName", PlatForm.GetPlatFormById(platId).PlatName);
            //    ht.Add("userinfo", Utils.RemoveXss(KSCMS.S("userinfo")));
            CommonDataUser.Insert("KS_RechargeRecords", ht);
        }
    }
</script>
  <script>
  	var box = '';
  	function SeeInfo(packid,feeid) {
  	    box = jQuery.dialog.open("/index.aspx?c=user&a=basic&frame=no&f=MyFeePackageDetail&feeid=" + feeid + "&packid=" + packid, { id: "loginbox", lock: true, fixed: true, title: "<div style='text-align:left'>营销卡使用详情</div>", width: 900, height: 500, max: false, min: false });
  	}
  </script>
    <style>
     .my_coupon li.study_card{float:left; background:none;padding:0; width:auto; height:auto;}
     .my_coupon li.study_card .study_card_in{padding: 50px 8px 10px;background: url(/user/img/studycard.png) no-repeat top center;background-size: 100% 100%;width: 319px;height: 174px;}
     	.my_coupon li.study_card{float:left; background:none;padding:0; width:auto; height:auto;}
		.my_coupon li.study_card .study_card_in{padding: 50px 8px 10px;background: url(/user/img/studycard.png) no-repeat top center;background-size: 100% 100%;width: 319px;height: 174px;}
		.table {display: table; width: 100%; border-collapse: collapse;}
		.table-tr {display: table-row; height: 40px;}
		.table-trs {display: table-row; height: 60px; border-bottom:solid 1px #e5e9ed;}
		.table-th {display: table-cell;font-weight: bold;height:40px;text-align: center;vertical-align: middle;background-color:#F9F9F9; font-size:15px;}
		.table-td {display: table-cell; height: 60px;text-align: center;vertical-align: middle; font-size:14px; color: #8A8A8A;}
		.td-le{text-align:left; width:200px; text-align:left;}
		.tan-box{width:600px; height:300px; background-color:white; z-index:1000; position: fixed; top:50%;left:50%;margin-top:-150px;margin-left:-300px; display:none;}
		.zhezhao{width:100%;height:100%;background-color:#000;opacity:0.2;position:fixed;left:0;top:0;z-index:990;filter:alpha(opacity=0.2); -moz-opacity:0.2; -khtml-opacity:0.2; display:none; }
  </style>
  <%
    if (!isLoadPage) return;
%>

  <div class="r_tybox t_tybox nomar clearfix disting">
  		<div class="r_title tytitle"><h4 class="float-none disting_show">我的营销卡</h4>
			
        </div>
        <div class="top-sort clearfix">
            <div class="top-sort-tab ">
                    <ul>  
                        <li <%if ( BasicField.GetParam("show") == "") Response.Write(" class='puton'"); %>><a href="<%=BasicField.GetUserUrl("basic/MyFeePackages") %>">我的营销卡</a></li>
                        <li <%if (BasicField.GetParam("show") == "1") Response.Write(" class='puton'"); %>><a href="<%=BasicField.GetUserUrl("basic/MyFeePackageBuy","show-1") %>">购买营销卡</a></li>
                        <li <%if (BasicField.GetParam("show") == "2") Response.Write(" class='puton'"); %>><a href="<%=BasicField.GetUserUrl("basic/MyFeePackages","show-2") %>">线下激活</a></li>
                    </ul>
            </div>
        </div>
        <script>
        	$(function () {
        		if ($(".tabs li").length == 0) {
        			$(".tabs").hide();
        		};
        	});

		</script>
				<%if(Utils.StrToInt(BasicField.GetParam("show"))==0){ %>
        
		<%} %>
		<%if(BasicField.GetParam("show")=="2"){ %>
         <script>
         	function check() {
         		if ($("#TxtCouponNum").val() == '') {
             		$.dialog.alert('请输入营销卡号!', function () {
             			$("#TxtCouponNum").focus();
                     });
				 	return false;
				 }
         		if ($("#TxtPassWord").val() == '') {
             		$.dialog.alert('请输入营销卡密码!', function () {
             			$("#TxtPassWord").focus();
                     });
				 	return false;
				 }
			 	return true;
			 };
   </script>

   <form name="myform" action="<%=BasicField.GetUserUrl("basic/MyFeePackages") %>" method="post">
   <input type="hidden" name="action" value="search" />
   <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="border"> 
            <tr class="tdbg">
                <td class="lefttd">营销卡号: </td>
                <td class="tag_left">
					<input type="text" name="TxtCouponNum" id="TxtCouponNum" class="textbox" />
                     <span style="color:Red">*</span>
                </td>
            </tr>
            <tr class="tdbg">
                <td class="lefttd">营销卡密码: </td>
                <td class="tag_left">
					<input type="text" name="TxtPassWord" id="TxtPassWord" class="textbox" />
                     <span style="color:Red">*</span>
                </td>
            
            </tr>
            <tr class="tr-nobor">
                <td class="lefttd"> </td>
                <td class="tag_left">
				  <input type="submit" class="green-bt green-bt-big button" value="确定激活" onclick="return (check())" />
                 </td>
            </tr>
      </table>
	  </form>


	  <%}else if(Utils.StrToInt(BasicField.GetParam("show"))==0) { %>

        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border ">
            <tr class="title">
                <td width="40%">营销卡名称</td>
                <td>激活时间</td>
                <td>使用期限</td>
                <td>使用条件</td>
                <td>价格</td>
                <td>操作</td>
            </tr>
 
      <%
		   int recordcount, pagecount;
		   int PageSize = 10;
		   int CurrPage = new BasicField().GetCurrPage;
		   string param = "KS_UserFeeSell a inner join KS_UserFeePackages b on a.PackID=b.id Where b.cardtype=0 and  b.Status=1 and a.UserName='" + UserManage.GetUserInfo().UserName + "'";
            if (KSCMS.S("show").ToString() == "2")
            {
                param = "KS_UserFeeSell a inner join KS_UserFeePackages b on a.PackID=b.id Where b.cardtype=0 and b.usetype=0 and b.Status=1 and a.UserName='" + UserManage.GetUserInfo().UserName + "'";
            }
			using (DataTable dt = DataFactory.GetPage("a.*,b.title,b.price", param, "a.id", "order by a.id desc", CurrPage, PageSize, out recordcount, out pagecount))
		   {
			   if (dt.Rows.Count > 0)
			   {
				   foreach (DataRow dr in dt.Rows)
				   {
                       KSCMS.W(" <tr><td class=\"tag_left\">" + dr["title"].ToString() + "</td> <td>"+Convert.ToDateTime(dr["packbegindate"].ToString()).ToString("yyyy-MM-dd")+"</td>"+
                           "<td>" + dr["packdays"].ToString() + "天"+(Utils.StrToInt(dr["GiveVIPDay"].ToString())==0?"":"(赠送VIP:"+Utils.StrToInt(dr["GiveVIPDay"].ToString())+"天)")+"</td><td>" + GetShiYong(dr["limit"].ToString(), Utils.StrToInt(dr["contenttf"].ToString()), Utils.StrToInt(dr["examtf"].ToString()), Utils.StrToInt(dr["coursetf"].ToString()), Utils.StrToInt(dr["packid"].ToString())) + " </td>" +
                        "<td>" + Public.ReturnShopPrice(dr["price"].ToString()) + "</td><td> <a class=\"green-bt ro_5\" onclick=\"SeeInfo(" + dr["packid"].ToString() + ","+dr["id"].ToString()+")\" href=\"#\">查看详情</a></td></tr>");
                       

				   }
			   }
			   else
			   {
				   KSCMS.S("<tr><td colspan=\"6\"><div class=\"empty\" >没有可用的营销卡</div></td></tr><style>.cz_btn{display:none;}</style>");
			   }
		   }
		
        %>

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

   <%}else{ %>

   <%} %>