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;


public partial class admin_VipGrade_ManageVIPGrade : Base.BasePage
{
    static int Iid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("User", "B1");
        if (!IsPostBack)
        {
            //RoleBind();
            Bind();
        }

    }
    //绑定会员角色
    private void RoleBind()
    {
        dropMove.Items.Add(new ListItem("--+请选择等级名称+--", "0"));
        IVipGrade grade = new RVipGrade();
        DataSet ds = grade.GetDataTable();
        if (ds.Tables[0].Rows.Count != 0)
        {
            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {

                dropMove.Items.Add(new ListItem(dataRow["Name"].ToString(), dataRow["ID"].ToString()));
               
            }
            dropMove.Items.Remove(new ListItem(Session["name"].ToString(), Session["id"].ToString()));
        }
    }
    private void Bind()
    {
  
        radlGrade.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();
        }
    }
    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 = 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();
    }
    protected void radlRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radlGrade.SelectedValue == "删除")
        {
            IVipGrade grade = new RVipGrade();
            int k = grade.Delete(Iid, int.Parse(dropMove.SelectedValue), 0);
            Bind();
            plMove.Visible = false;
            radlGrade.Visible = false;
        }
        else
        {
            plMove.Visible = true;
            RoleBind();
        }
    }
    //转移会员等级
    protected void btnMove_Click(object sender, EventArgs e)
    {
        if (dropMove.SelectedValue == "0")
        {
            ShowMessage("请选择所属类别");
            return;
        }
        IVipGrade grade = new RVipGrade();
        int k = grade.Delete(Iid, int.Parse(dropMove.SelectedValue), 1);
        Bind();
        plMove.Visible = false;
    }
    protected void btnBank_Click(object sender, EventArgs e)
    {
        plMove.Visible = false;
    }
    protected void lbtnUpdate_Command(object sender, CommandEventArgs e)
    {
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        Iid = int.Parse(Content.Substring(0, index));
        if (index < -1 || index + 1 > Content.Length)
            return;
        string name = Content.Substring(index + 1);
        Session["id"] = Iid;
        Session["name"] = name;
        radlGrade.Visible = true;
    }
}
