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;

public partial class etwuser_Area_news : System.Web.UI.Page
{
    public int page = 1;
    public string sqlstr = string.Empty;
    public string sqlcount = string.Empty;
    public int mun = 1;
    public string Cutpage = string.Empty;
    public string Cutpage1 = string.Empty;
    public int pagecount = 1;
    public int PageSize = 20;
    public string Addressid = string.Empty;
    public string username = string.Empty;
    public string searchstr = string.Empty;
    public string lang = string.Empty;
    public string Where = string.Empty;
    public string tiaojian = string.Empty;
    public string tiaojian1 = string.Empty;
    public string usernameid = string.Empty;
    public string iszistyle = string.Empty;
    public string account = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie ChatCookie = Request.Cookies["cookChatuser"];
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookChatuser"] == null)
        {
            if (Request.Cookies["cookEtwuser"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                username = GetCookie["username"];
                DataSet ds = DbHelperSQL.Query("select userid from etw_user where username='" + username + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    usernameid = ds.Tables[0].Rows[0]["userid"].ToString();
                }
            }
        }
        else
        {
            username = ChatCookie["username"];
            iszistyle = "style=\"display:none; line-height:0px; overflow:hidden;\"";
            DataSet ds1 = DbHelperSQL.Query("select id,userid from chat_user where username='" + username + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                usernameid = ds1.Tables[0].Rows[0]["userid"].ToString();
            }
        }
        if (Request.QueryString["page"] != null)
        {
            page = Convert.ToInt32(Request.QueryString["page"].ToString());
        }
        if (Request.QueryString["search"] != null && Request.QueryString["lang"] != null && Request.QueryString["account"] != null)
        {
            searchstr = Request.QueryString["search"].ToString();
            account = Request.QueryString["account"].ToString();
            searchstr = searchstr.Trim().Replace("'", "").Replace("’", "");
            lang = Request.QueryString["lang"].ToString();
            if (lang == "0")
            {
                if (searchstr != "")
                {
                    Where = "(senddate like '%" + searchstr + "%' or email like '%" + searchstr + "%' or title like '%" + searchstr + "%' or definesubject like '%" + searchstr + "%') and ";
                }
                if (account != "0")
                {
                    Where += "childuserid=" + account + " and ";
                }
            }
            else
            {
                if (searchstr != "")
                {
                    Where = "(senddate like '%" + searchstr + "%' or email like '%" + searchstr + "%' or title like '%" + searchstr + "%' or definesubject like '%" + searchstr + "%') and language='" + lang + "' and ";
                }
                else
                {
                    Where = "language='" + lang + "' and ";
                }
                if (account != "0")
                {
                    Where += "childuserid=" + account + " and ";
                }
            }
            tiaojian = ",'&search=" + searchstr + "&lang=" + lang + "&account=" + account + "'";
            tiaojian1 = "'&search=" + searchstr + "&lang=" + lang + "&account=" + account + "'";
        }
        if (Request.QueryString["Addressid"] != null)
        {
            Addressid = Request.QueryString["Addressid"].ToString();
            if (!IsPostBack)
            {
                if (Addressid != "")
                {
                    DataUser();
                }
                datalang();
                Data();
                Fun.Funrecord(Request.Url.AbsoluteUri,Page.Request.UserHostAddress, usernameid);
            }
        }
    }
    public void Data()
    {
        DataSet ds = DbHelperSQL.Query("select * from etw_Address");
        if (ds.Tables[0].Rows.Count > 0)
        {
            Repeater2.DataSource = ds.Tables[0].DefaultView;
            Repeater2.DataBind();
        }
        DataSet ds1 = DbHelperSQL.Query("select id,name from etw_Intention  where  usernameid=0 or usernameid=" + usernameid + "");
        if (ds1.Tables[0].Rows.Count > 0)
        {
            Repeater3.DataSource = ds1.Tables[0].DefaultView;
            Repeater3.DataBind();
            Repeater6.DataSource = ds1.Tables[0].DefaultView;
            Repeater6.DataBind();
        }
        DataSet ds3 = DbHelperSQL.Query("select username,id,realname  from chat_user where userid =" + usernameid + "");
        if (ds3.Tables[0].Rows.Count > 0)
        {
            Repeater8.DataSource = ds3.Tables[0].DefaultView;
            Repeater8.DataBind();
        }
        DataSet ds2 = DbHelperSQL.Query("select * from etw_Customize where usernameid=" + usernameid + "");
        if (ds2.Tables[0].Rows.Count > 0)
        {
            Repeater4.DataSource = ds2.Tables[0].DefaultView;
            Repeater4.DataBind();
            Repeater7.DataSource = ds2.Tables[0].DefaultView;
            Repeater7.DataBind();
        }
    }
    public void datalang()
    {
        DataSet ds = DbHelperSQL.Query("select * from etw_language ");
        if (ds.Tables[0].Rows.Count > 0)
        {
            Repeater9.DataSource = ds.Tables[0].DefaultView;
            Repeater9.DataBind();
        }
        DataSet ds1 = DbHelperSQL.Query("select id,realname from chat_user where userid=" + usernameid + "");
        if (ds1.Tables[0].Rows.Count > 0)
        {
            Repeater10.DataSource = ds1.Tables[0].DefaultView;
            Repeater10.DataBind();
        }
    }
    public void DataUser()
    {
        mun = (page - 1) * PageSize;
        DataSet ds = DbHelperSQL.Query("select id,userid from chat_user where username='" + username + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (Addressid == "0")
            {
                sqlstr = "select top " + PageSize + " * from etw_onlines where " + Where + " yid!=-1 and  substring(address,1,2) not in(select shortname from etw_country) and id not in(select onlines_id from etw_repeat where usernameid=" + ds.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and childuserid=" + ds.Tables[0].Rows[0]["id"] + " and id not in(select top " + mun + " id from etw_onlines where  " + Where + " yid!=-1 and    substring(address,1,2) not in(select shortname from etw_country) and id not in(select onlines_id from etw_repeat where usernameid=" + ds.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and childuserid=" + ds.Tables[0].Rows[0]["id"] + " order by id desc) order by id desc";
                sqlcount = "select count(id) from etw_onlines where " + Where + "  yid!=-1 and  substring(address,1,2) not in (select shortname from etw_country) and childuserid=" + ds.Tables[0].Rows[0]["id"] + " and isdelete=0 ";

            }
            else
            {
                sqlstr = "select top " + PageSize + " * from etw_onlines where " + Where + " yid!=-1 and  substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and id not in(select onlines_id from etw_repeat where usernameid=" + ds.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and childuserid=" + ds.Tables[0].Rows[0]["id"] + " and id not in(select top " + mun + " id from etw_onlines where  " + Where + " yid!=-1 and    substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and id not in(select onlines_id from etw_repeat where usernameid=" + ds.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and childuserid=" + ds.Tables[0].Rows[0]["id"] + " order by id desc) order by id desc";
                sqlcount = "select count(id) from etw_onlines where " + Where + "  yid!=-1 and  substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and childuserid=" + ds.Tables[0].Rows[0]["id"] + " and isdelete=0 ";
            }
        }
        else
        {
            DataSet ds1 = DbHelperSQL.Query("select userid from etw_user where username='" + username + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                if (Addressid == "0")
                {
                    sqlstr = "select top " + PageSize + " * from etw_onlines where " + Where + "  yid!=-1 and   substring(address,1,2)  not in(select shortname from etw_country) and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " and id not in(select top " + mun + " id from etw_onlines where  " + Where + " yid!=-1 and    substring(address,1,2)  not in(select shortname from etw_country) and isdelete=0 and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ") and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " order by id desc) order by id desc";
                    sqlcount = "select count(id) from etw_onlines where " + Where + " yid!=-1 and   substring(address,1,2)  not in(select shortname from etw_country) and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " and isdelete=0 and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ")";

                }
                else
                {
                    sqlstr = "select top " + PageSize + " * from etw_onlines where " + Where + "  yid!=-1 and   substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ") and isdelete=0 and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " and id not in(select top " + mun + " id from etw_onlines where  " + Where + " yid!=-1 and    substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and isdelete=0 and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ") and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " order by id desc) order by id desc";
                    sqlcount = "select count(id) from etw_onlines where " + Where + " yid!=-1 and   substring(address,1,2) in(select shortname from etw_country where zid=" + Addressid + ") and usernameid=" + ds1.Tables[0].Rows[0]["userid"] + " and isdelete=0 and id not in(select onlines_id from etw_repeat where usernameid=" + ds1.Tables[0].Rows[0]["userid"] + ")";
                }
            }

        }
        if (!IsPostBack)
        {
            int totalOrders = (int)DbHelperSQL.GetSingle(sqlcount);
            if (totalOrders > PageSize)
            {
                if (totalOrders % PageSize == 0)
                {
                    pagecount = totalOrders / PageSize;
                }
                else
                {
                    pagecount = (totalOrders / PageSize) + 1;
                }
            }
            if (pagecount > 1)
            {
                if (page == 1)
                {
                    Cutpage = "<a class=\"txt-disabd\">首页</a><a  class=\"txt-disabd\">上一页</a><a  href=\"javascript:Paging(" + Convert.ToString(page + 1) + "," + Addressid + "" + tiaojian + ")\">下一页</a><a href=\"javascript:Paging(" + pagecount + "," + Addressid + "" + tiaojian + ")\">末页</a>";
                    Cutpage1 = Cutpage;
                }
                else if (page == pagecount)
                {
                    Cutpage = "<a href=\"javascript:Paging(1," + Addressid + "" + tiaojian + ")\">首页</a><a href=\"javascript:Paging(" + Convert.ToString(page - 1) + "," + Addressid + "" + tiaojian + ")\">上一页</a><a  class=\"txt-disabd\">下一页</a><a class=\"txt-disabd\">末页</a>";
                    Cutpage1 = Cutpage;
                }
                else
                {
                    Cutpage = "<a href=\"javascript:Paging(1," + Addressid + "" + tiaojian + ")\">首页</a><a href=\"javascript:Paging(" + Convert.ToString(page - 1) + "," + Addressid + "" + tiaojian + ")\">上一页</a><a href=\"javascript:Paging(" + Convert.ToString(page + 1) + "," + Addressid + "" + tiaojian + ")\">下一页</a><a href=\"javascript:Paging(" + pagecount + "," + Addressid + "" + tiaojian + ")\">末页</a>";
                    Cutpage1 = Cutpage;
                }
                Cutpage += "<select id=\"PageNumber\" onchange=\"Pagpage('" + Addressid + "'" + tiaojian + ");\">";
                Cutpage1 += "<select id=\"PageNumber1\" onchange=\"Pagpage1('" + Addressid + "'" + tiaojian + ");\">";
                for (int i = 1; i <= pagecount; i++)
                {
                    Cutpage += "<option value=\"" + i + "\" >" + i + "/" + pagecount + "</option>";
                    Cutpage1 += "<option value=\"" + i + "\">" + i + "/" + pagecount + "</option>";
                }
                Cutpage += "</select>";
                Cutpage1 += "</select>";
            }
        }
        DataSet dss = DbHelperSQL.Query(sqlstr);
        if (dss.Tables[0].Rows.Count > 0)
        {
            Repeater1.DataSource = dss.Tables[0].DefaultView;
            Repeater1.DataBind();
        }
    }
    public string CutTime(string str1)
    {
        string str = Convert.ToDateTime(str1).ToString("yyyy-MM-dd");
        return str;
    }
    public string CutAddress(string str)
    {
        string country = "未识别";
        string[] strid = str.Split(new char[] { ',' });
        if (strid[0] != "")
        {
            DataSet ds = DbHelperSQL.Query("select * from etw_country where shortname='" + strid[0] + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                country = ds.Tables[0].Rows[0]["country_cn"].ToString();
            }
        }
        return country;
    }
    public string Cutlang(string str)
    {
        string lang = string.Empty;
        DataSet ds = DbHelperSQL.Query("select language from etw_language where domain='" + str + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            lang = ds.Tables[0].Rows[0]["language"].ToString();
        }
        return lang;
    }
	public string Cutname(string Iid, string Iname)
    {
        string namestr = Iname;
        DataSet ds = DbHelperSQL.Query("select name from intention_news where usernameid=" + usernameid + " and Intenid=" + Iid + "");
        if (ds.Tables[0].Rows.Count > 0)
        {
            namestr = ds.Tables[0].Rows[0]["name"].ToString();
        }
        return namestr;
    }
    public string CutTitle(string subjectstr, string titlestr)
    {
        string deftitle = titlestr;
        if (subjectstr != "")
        {
            deftitle = subjectstr;
        }
        return deftitle;
    }
}

