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 Email.Subscription;
using nspBase.Core;
using nspEmail.Lables;
using SqlOperate;

public partial class Admin_Subscription_SubscriptionNewsList : Base.BasePage
{
    public string strEmail;
    public string strbody;
    public string strTitle;
    string strInfoMessage;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Bind();
        }

    }
    private void Bind()
    {
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx";
        PageNo1.Strurl = strURl;
        PageNo1.FldName = "temps.UserID";
        //PageNo1.Table = "U_SubscriptionNews as a inner join W_ConsultativeType b on a.Type=b.ID inner join U_UserInfo c on a.UserID=c.UserID inner join U_SubscriptionEmail d on a.UserID=d.UserID"; 
        // PageNo1.StrGetFields = "a.ID,a.UserID,a.Type,a.AddDate,b.Name,c.LoginName,d.Email";
        PageNo1.Table = "(select UserID from U_SubscriptionNews group by UserID) temps inner join U_UserInfo b on temps.UserID=b.UserID inner join U_SubscriptionEmail c on temps.UserID=c.UserID";
        PageNo1.StrGetFields = "temps.UserID,b.LoginName,c.Email";

        PageNo1.PageSize = 12;
        if (Request.QueryString["page"] != "" && Request.QueryString["page"] != null)
        {
            PageNo1.Pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
            if (Session["strWhere"] != null)
            {
                PageNo1.Where = Session["strWhere"].ToString();
            }
            else
            {
                PageNo1.Where = "1=1";
            }
            DataTable dt = PageNo1.BindViewDesc();
            if (dt.DefaultView.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
            {
                lblMessage.Visible = false;
            }
            dlstSubscription.DataSource = dt.DefaultView;
            dlstSubscription.DataBind();
        }
        else
        {
            Session["strWhere"] = "1=1";
            PageNo1.Where = "1=1";
            PageNo1.Pageindex = 1;
            DataTable dt = PageNo1.BindViewDesc();
            if (dt.DefaultView.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
            {
                lblMessage.Visible = false;
            }
            dlstSubscription.DataSource = dt.DefaultView;
            dlstSubscription.DataBind();
        }
    }
    protected void dlstSubscription_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int ID = int.Parse(dlstSubscription.DataKeys[e.Item.ItemIndex].ToString());
       // int ID = int.Parse(((Label)dlstSubscription.FindControl("lblUserId")).Text);
        SubscriptionNews product = new SubscriptionNews();
        product.Delete(ID);
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx?page=1";
        Response.Redirect(strURl);
        Response.End();
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string strInfo = "";
        string sql = "select Content,Title from W_EmailTemplate  where Flag='us_SubScriptionNews'";
        DataTable dt = SqlHelper.ExecuteSqlQuery(sql);
        if (dt.Rows.Count != 0)
        {
            string strModes = dt.Rows[0]["Content"].ToString();
            strTitle = dt.Rows[0]["Title"].ToString();
            dt.Clear();
            dt.Dispose();

            for (int i = 0; i < dlstSubscription.Items.Count; i++)
            {
                CheckBox cbx = (CheckBox)dlstSubscription.Items[i].FindControl("CheckBox1");
                if (cbx.Checked)
                {
                    EmailTempinc.strUserName = ((Label)dlstSubscription.Items[i].FindControl("lblLoginName")).Text;
                    strEmail = ((Label)dlstSubscription.Items[i].FindControl("lblEmail")).Text;
                    string strUserID = ((Label)dlstSubscription.Items[i].FindControl("lblUserId")).Text;
                    string strSql = "select Type from U_SubscriptionNews where UserID="+strUserID;
                    DataTable tables = SqlHelper.ExecuteSqlQuery(strSql);
                    for (int j = 0; j < tables.Rows.Count; j++)
                    {
                        EmailTempinc.strTypeId = tables.Rows[j]["Type"].ToString();
                        strbody = new EmailTempinc().Hope_htmlAll(strModes);  
                        if (strbody.ToUpper() != "NOT FIND")
                        {
                            System.Threading.Thread.Sleep(1000);
                            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(dowork));
                            thread.Start();
                        }
                    }
                }
            }
        }
    }
    public void dowork()
    {
        strInfoMessage = clsEmail.SendMail(strEmail, strTitle, strbody, true);

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dlstSubscription.Items.Count; i++)
        {
            CheckBox cbx = (CheckBox)dlstSubscription.Items[i].FindControl("CheckBox1");
            if (cbx.Checked)
            {
                int id =int.Parse(((Label)dlstSubscription.Items[i].FindControl("lblUserId")).Text);
                SubscriptionNews news = new SubscriptionNews();
                news.Delete(id);
            }
        }
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx?page=1";
        Response.Redirect(strURl);
        Response.End();
    }
    protected void dlstSubscription_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string strUserId = ((Label)e.Item.FindControl("lblTypeInfo")).Text;
            if (strUserId != null)
                strUserId = strUserId.ToString().Trim();
            string sql = "select Name from W_ConsultativeType where ID in(select Type from U_SubscriptionNews where UserID='" + strUserId + "') ";
            string strtype = "";
            DataTable dt = SqlHelper.ExecuteSqlQuery(sql);
            for (int i = 0; i < dt.DefaultView.Count; i++)
            {
                if (i == dt.DefaultView.Count - 1)
                    strtype += dt.Rows[i]["Name"].ToString();
                else
                    strtype += dt.Rows[i]["Name"].ToString() + ",";
            }
            ((Label)e.Item.FindControl("lblTypeInfo")).Text = strtype;
            dt.Clear();
            dt.Dispose();
        }
    }
    protected void lbtnDelete_Command(object sender, CommandEventArgs e)
    {
        string ID =e.CommandArgument.ToString();
        SubscriptionNews product = new SubscriptionNews();
        product.Delete(ID);
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx?page=1";
        Response.Redirect(strURl);
        Response.End();
    }
}
