using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Consultative;
using UserInfo;
using Email.Subscription;
using SqlOperate;


public partial class Member_Subscription_SubscriptionNewsAdd : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                TypeBind();
                Bind();
            }
        }
        else
            Response.Redirect("../../login/login.aspx");
    }
    private void TypeBind()
    {
        IConsultativeType icon = new RConsultativeType();
        DataTable dt = icon.GetDataTable();
        dropType.DataSource = dt.DefaultView;
        dropType.DataTextField = "Name";
        dropType.DataValueField = "ID";
        dropType.DataBind();
        string strsql = "select Email from U_SubscriptionEmail where UserID=" + GetCookie("UserID");
        DataTable dtable = SqlHelper.ExecuteSqlQuery(strsql);
        if (dtable.Rows.Count > 0)
        {
            lblEmail.Text = dtable.Rows[0]["Email"].ToString();
        }
        else
        {
            lblEmail.Text = "还没有邮箱";
        }
        dtable.Clear();
        dtable.Dispose();
    }
    private void Bind()
    {
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            int id = int.Parse(Request.QueryString["ID"].ToString());
            SubscriptionNews news = new SubscriptionNews();
            SubscriptionNews ne = news.GetItem(id);
            dropType.SelectedValue = ne.Type;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int userid = int.Parse(GetCookie("UserID"));
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            int id = int.Parse(Request.QueryString["ID"].ToString());
            try
            {
                SubscriptionNews news = new SubscriptionNews();
                news.ID = id;
                news.Type = dropType.SelectedValue;
                news.Update(news);
                ShowMessage("订阅商业资讯成功！", "Subscription/SubscriptionNewsList.aspx");
            }
            catch (Exception ex)
            {
                ShowMessage("订阅商业资讯失败！", "Subscription/SubscriptionNewsList.aspx");
            }
        }
        else
        {
            try
            {
                SubscriptionNews news = new SubscriptionNews();
                news.UserID = userid;
                news.Type = dropType.SelectedValue;
                news.Insert(news);
                ShowMessage("订阅商业资讯成功！", "Subscription/SubscriptionNewsList.aspx");
            }
            catch (Exception ex)
            {
                ShowMessage("订阅商业资讯失败！", "Subscription/SubscriptionNewsList.aspx");
            }
        }
    }
}
