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 System.Data.SqlClient;

public partial class wormadmin_customer_manage : System.Web.UI.Page
{
    sqloper sqloper = new sqloper();
    SqlConnection one = conn.sqlconn();
    public string txname;
    public string txpw;
    public string txhead;
    public string txsex;
    public string txaddress;
    public string txqq;
    public string txmail;
    public string txinstroduct;
    public string txdate;
    public string txlei;
    public int txdeng;
    public int txsend;
    public string txlevel;
    string connect_sql;

    //int gvEditIndex = -1;
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["superad"] == null || Session["superpw"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Page.Theme = "SkinFile";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        cusmsg.Visible = false;
        myGrid.SelectedIndex = -1;
        connect_sql = tsql.Text;
        if (!IsPostBack)
        {
            if (Request.QueryString["search"] == "1")
            {
                connect_sql = req();
                tsql.Text = connect_sql;
            }
            gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
        }
    }

    //接受搜索页面的值,然后进行sql语句合拼处理
    protected string req()
    {
        string reqstr = "";
        string txtname = Request["cname"];
        string txtlei = Request["clei"];
        string txtsex;
        if (Request["csex"]!=null)
        {
           txtsex = Request["csex"];
        }
        else
        {
           txtsex = "";
        }
        string date1 = Request["date1"];
        string date2 = Request["date2"];
        string temptitle = Request["temptitle"];
        append_sql apsql = new append_sql();
        reqstr = apsql.sql_str(temptitle, txtname, txtlei, txtsex, "", date1,date2, 0);
        return reqstr;
    }

    //短日期处理
    protected string short_time(object times)
    {
        stringdo shortime = new stringdo();
        return shortime.short_time(times);
    }

    //数据绑定GridView
    //sql:数据库查询语句,conn:数据库连接,gvid:gridview控件的id,pssize:一页中数据的多少
    protected void gvbind(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        PagedDataSource ps = new PagedDataSource();
        try
        {
            conn.Open();
            adapter.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;              //ps分页数据源设置
            ps.AllowPaging = true;                                       //允许分页
            ps.PageSize = pssize;                                       //页面数据大小
            ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;    //当前页面指针
            AspNetPager1.RecordCount = ds.Tables["order"].Rows.Count;  //记录总行数赋值给控件AspNetPager1

            gvid.DataSource = ps;
            gvid.DataBind();
        }
        catch (System.Exception ee)
        {
            Response.Write("绑定出错:"+ee.Message.ToString());
        }
        finally
        {
            ds.Dispose();
            adapter.Dispose();
            conn.Close();
        }
    }

    //gridview编辑链接
    protected void myedit(object sender, GridViewEditEventArgs e)
    {
        //用NewEidIndex取得当前编辑的行号，然后获取gridviewrow对象

        myGrid.EditIndex = e.NewEditIndex;
        gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
        try
        {
            one.Open();
            string clei = "";
            string sql = "select clei from customer where id=" + myGrid.DataKeys[myGrid.EditIndex].Value;
            SqlCommand cmd = new SqlCommand(sql, one);
            SqlDataReader rs = cmd.ExecuteReader();
            if (rs.Read())
            {
               clei=rs["clei"].ToString();
               rs.Dispose();
               cmd.Dispose();
            }
            int i = 0;
            int dli = ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).Items.Count;
            for (i = 1; i < dli; i++)
            {
                if (((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).Items[i].ToString() == clei)
                    {
                        ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).SelectedIndex = i;
                    }
            }

        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            one.Close();
        }
        
    }

    //gridview更新数据事件
    protected void myupdate(object sender, GridViewUpdateEventArgs e)
    {
        string clei = ((DropDownList)myGrid.Rows[e.RowIndex].FindControl("dlclass")).SelectedValue;
        //更新customer表
        string sql = "update customer set clei='" + clei + "' where id=" + myGrid.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        myGrid.EditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新成功!');</script>");
        gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);

    }

    //gridview取消编辑事件
    protected void mycancel(object sender, GridViewCancelEditEventArgs e)
    {
        myGrid.EditIndex = -1;
        gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //翻页数据绑定事件
    protected void pagego(object sender, EventArgs e)
    {
        gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //选中删除按钮事件
    protected void btdel_Click(object sender, EventArgs e)
    {
        //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>confirm('是否确认删除此留言?');</script>");
        //Response.Write("<SCRIPT LANGUAGE='javascript'>if(confirm('是否确认删除此留言?')){return true;}return false;</SCRIPT>");
        int i;
        string sql;
        string sql2;
        string myKeyid;
        CheckBox mycheck = new CheckBox();
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            //string dclasstemp = ((Label)myGrid.Rows[i].FindControl("dclasstemp")).Text;
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                //删除classdai表的分类
                sql = "DELETE FROM customer where id=" + myKeyid;
                sqloper.dodata(sql, one);

            }
        }
        gvbind("select * from customer " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //全选checkbox按钮
    protected void delall_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked == true)
            {
                ch.Checked = true;
            }
            else
            {
                ch.Checked = false;
            }
        }

    }

    //全选button按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (ch.Checked == false)
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = true;
                ch.Checked = true;
            }
            else
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = false;
                ch.Checked = false;
            }
        }
    }

    protected void myselect(object sender, GridViewSelectEventArgs e)
    {
        cusmsg.Visible = true;
        string sql = "select * from customer where id=" + myGrid.DataKeys[e.NewSelectedIndex].Value;
        SqlDataReader rs=sqloper.readsql(sql, one);
        if(rs.Read())
        {
           txhead = "../"+rs["chead"].ToString();
           txname=rs["cname"].ToString();
           txpw = rs["cpw"].ToString();
           txsex = rs["csex"].ToString();
           txaddress = rs["caddress"].ToString();
           txqq=rs["cqq"].ToString();
           txmail = rs["cmail"].ToString();
           txinstroduct = rs["cinstroduce"].ToString();
           txdate = rs["cdate"].ToString();
           txlei = rs["clei"].ToString();
           txdeng = Convert.ToInt16(rs["cdeng"]);
           txsend = Convert.ToInt16(rs["csend"]);
           txlevel=level(txsend);
        }
        rs.Dispose();
    }

    public string level(int mylevel)          //用户级别【判断函数】
    {
        int star = 0;
        int i = 0;
        string imglevel = "";
        if (mylevel <= 10 && mylevel >= 0)
        {
            star = 1;
        }
        if (mylevel > 10 && mylevel <= 30)
        {
            star = 2;
        }
        if (mylevel > 30 && mylevel <= 80)
        {
            star = 3;
        }
        if (mylevel > 80)
        {
            star = 4;
        }
        imglevel = imglevel +star+ "级";
        for (i = 1; i <= star; i++)
        {
            imglevel = imglevel + "<img src='../images/icon/level2.gif' width='13' height='12'alt='等级" + star + "' style='cursor:hand;'/>";
        }
        return imglevel;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        cusmsg.Visible = false;
    }
}
