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 Base;
using UserInfo;
using Area;
using System.Data.SqlClient;

public partial class Search_SearchCompany : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            AreaBind("");
        }
    }

    public string GetVipGrade(string GradeID)
    {
        string str = "";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqldata"].ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT [Flag] FROM [U_VipGrade] WHERE ID=@ID", con);
        cmd.Parameters.AddWithValue("@ID", GradeID);
        con.Open();
        SqlDataReader sr = cmd.ExecuteReader();
        if (sr.Read())
        {
            str = sr["Flag"].ToString();
        }
        con.Close();

        int k = 0;
        string strc = "";
        for (int i = 0; i < str.Length; i++)
        {
            if (k > 0)
            {
                strc += str[i];
            }
            if (str[i].Equals('/'))
            {
                k++;
            }

        }
        return Base.BasePage.Config.WebURL + strc;
    }
    private void Bind()
    {
        
        string str = "";
        int page = 1;
        if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "")
        {
          str = Base.GetRequest.GetRequestParameters(Request, "GB2312")["keyword"];
          lblKey.Text += str;
        }
        if (Request.QueryString["strWhere"] != null && Request.QueryString["strWhere"] != "")
        {
            str = Request.QueryString["strWhere"].ToString();
        }
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
        {
            page = int.Parse(Request.QueryString["page"].ToString());
        }

        string strselect = "U_UserInfo.*";
        string table = "U_UserInfo";
        string strWhere = " CompanyName like '%" + str + "%'"; ;//1=1 and name like '%" + str + "%'
        DataTable dt = PageList.GetDataTable(strWhere, table, 0, "UserID", "*", 10, 1, 0);
        if (dt.Rows.Count == 0)
        {
            lblMessage.Visible = true;
            lblMessage.Text = "<font color='red' size='5pt'>暂无数据</font>";
        }
        else
        {
            lblMessage.Visible = false;
            lblCount.Text = " " + dt.Rows.Count + " ";
        }
        dlstCompany.DataSource = dt.DefaultView;
        dlstCompany.DataBind();
        lblPagination.Text = PageList.GoToPager(page, 10, "SearchCompany.aspx?page", strWhere, table);

    }


    /// <summary>
    /// 绑定地区
    /// </summary>
    /// <param name="str"></param>
    private void AreaBind(string str)
    {
        IArea area = new RArea();
        DataTable dt = area.GetDataTable(0);
        DropDownList provinceDL = (DropDownList)dlstCompany.Controls[0].Controls[0].FindControl("dropArea");
        provinceDL.DataSource = dt.DefaultView;
        provinceDL.DataBind();
        provinceDL.DataValueField = "Name";
        provinceDL.DataTextField = "Name";
        provinceDL.DataBind();
        ListItem li = new ListItem("所有地区", "");
        provinceDL.Items.Add(li);
        if (str == "")
            li.Selected = true;
        else
            provinceDL.SelectedValue = str;

    }


    protected void dropArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "";
        string strWhere = " 1=1";
        int page = 1;
        string strselect = " U_UserInfo.*";
        string table = " U_UserInfo";
        if (Request.QueryString["strWhere"] != null && Request.QueryString["strWhere"] != "")
        {
            str = Request.QueryString["strWhere"].ToString();
        }
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
        {
            page = int.Parse(Request.QueryString["page"].ToString());
        }
        string area = ((DropDownList)dlstCompany.Controls[0].FindControl("dropArea")).SelectedValue;

        if (((DropDownList)dlstCompany.Controls[0].FindControl("dropArea")).SelectedValue != "")
        {
            strWhere += " and Province like '%" + area + "%'"; // +"' and name lile '%" + str + "%'"; 

        }
        //else
        //{

        //    //strWhere += " and name like '%" + str + "%'";
        //}

        DataTable dt = PageList.GetDataTable(strWhere, table, 0, "UserID", "*", 10, 1, 0);
        dlstCompany.DataSource = dt.DefaultView;
        dlstCompany.DataBind();
        lblPagination.Text = PageList.GoToPager(page, 10, "SearchCompany.aspx?page", strWhere, table);
        AreaBind(area);
    }


}
