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 VipGrade;
using System.Data.SqlClient;
using SqlOperate;
using Base;

public partial class admin_VipGrade_ManageVIPGrade : Base.BasePage
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("User", "B1");
        if (!IsPostBack)
        {
            Bind();
        }
    }
    ///**************************************************
    /// <summary>
    /// depiction：实现对会员等级的绑定
    /// </summary>
    /// <param name="<参数名称>"><参数说明>
    /// </param>
    ///**************************************************
    private void Bind()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx";
        PageNo1.Strurl = strURl;
        if (Request.QueryString["page"] != null)
        {
            PageNo1.Pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
            PageNo1.PageSize = 12;
            if (Session["strWhere"] != null)
            {
                PageNo1.Where = Session["strWhere"].ToString();
            }
            else
            {
                PageNo1.Where = "1=1";
            }
            PageNo1.Table = "U_VipGrade";
            PageNo1.FldName = "ID";
            PageNo1.StrGetFields = "*";
            DataTable table = PageNo1.BindView();
            gdvVipGrade.DataSource = table.DefaultView;
            gdvVipGrade.DataBind();
        }
        else
        {
            PageNo1.Pageindex = 1;
            PageNo1.PageSize = 12;
            PageNo1.Where = "1=1";
            PageNo1.Table = "U_VipGrade";
            PageNo1.FldName = "ID";
            PageNo1.StrGetFields = "*";
            DataTable table = PageNo1.BindView();
            gdvVipGrade.DataSource = table.DefaultView;
            gdvVipGrade.DataBind();
        }
    }
    
    ///**************************************************
    /// <summary>
    /// depiction：实现对会员等级的删除
    /// </summary>
    /// <param name="<参数名称>"><参数说明>
    /// </param>
    ///**************************************************
  protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= gdvVipGrade.Rows.Count - 1; i++)
        {

            CheckBox cbox = (CheckBox)gdvVipGrade.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                int UserID = Convert.ToInt32(gdvVipGrade.DataKeys[i].Value);
                IVipGrade RUser = new RVipGrade();
                int m = RUser.Delete(UserID);
            }
        }
        Bind();
    }
    protected void gdvVipGrade_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvVipGrade.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void gdvVipGrade_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(gdvVipGrade.DataKeys[e.RowIndex].Value.ToString());
        IVipGrade RVip = new RVipGrade();
        RVip.Delete(id);
        Bind();
    }
    
    protected void gdvVipGrade_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");
            e.Row.Attributes.Add("onMouseOut", "SetOldColor(this)");
        }
    }

    protected void btnSort_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rows in this.gdvVipGrade.Rows)
        {
            TextBox txt = rows.FindControl("txtSort") as TextBox;
            Label iId = rows.FindControl("lblId") as Label;
            int txtd = 0;
            if (GetRequest.isNum(txt.Text.ToString()))
                txtd = Convert.ToInt32(txt.Text.ToString());
            int id = Convert.ToInt32(iId.Text.ToString());
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
                SqlCommand cmd = new SqlCommand("UPDATE [U_VipGrade] SET [Sort]=@Sort WHERE ID=@ID",con);
                cmd.Parameters.AddWithValue("@Sort", txtd);
                cmd.Parameters.AddWithValue("@ID", id);
                using (con)
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
        Bind();
    }
}
