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 Spaces;
using Spaces.Controls;
public partial class Manage_EditGroup : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
        Message.Text = string.Empty;
        GroupView.RowEditing += new GridViewEditEventHandler(GroupView_RowEditing);
        GroupView.RowCancelingEdit += new GridViewCancelEditEventHandler(GroupView_RowCancelingEdit);
        GroupView.RowUpdating += new GridViewUpdateEventHandler(GroupView_RowUpdating);
        GroupView.RowDeleting += new GridViewDeleteEventHandler(GroupView_RowDeleting);
      
        if (!IsPostBack)
            BindGroup();    
    }

    void GroupView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lID = GroupView.Rows[e.RowIndex].FindControl("GroupID") as Label;
        string sql = string.Format("SELECT top 1 GroupID FROM User_UserConfig where GroupID={0}" ,lID.Text);
        if (Config.GetConfig().DefaultProvider == "MySql")
        {
            sql = string.Format("SELECT   GroupID FROM User_UserConfig where GroupID={0} limit 0,1", lID.Text);
        }
        DataRow dr = SpacesDataProvider.Instance().GetDataRow(sql);
        if (dr != null && dr["GroupID"] != DBNull.Value)
        {
　
            Message.Text = Msg.GetMsg("已有用户加入该组,请选更改该用户组！");
            return;
        }
        sql = string.Format("DELETE FROM User_Group WHERE GroupID={0}",lID.Text);
        SpacesDataProvider.Instance().ExecuteNonQuery(sql);
        GroupView.EditIndex = -1;
        BindGroup();
 
        Message.Text = Msg.GetMsg("成功删除一个组,请检查有无默认组,没有请设置一个,否则用户将无法注册！"); ;
    }

    void GroupView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label groupID = GroupView.Rows[e.RowIndex].FindControl("GroupID") as Label;
        TextBox groupName = GroupView.Rows[e.RowIndex].FindControl("GroupName") as TextBox;
        TextBox totalSize = GroupView.Rows[e.RowIndex].FindControl("TotalSize") as TextBox;
        CheckBox DefaultGroup = GroupView.Rows[e.RowIndex].FindControl("DefaultGroup") as CheckBox;
        if (DefaultGroup.Checked)
        {

            SpacesDataProvider.Instance().ExecuteNonQuery("UPDATE User_Group SET  DefaultGroup=0");
        }
        string sql = string.Format(@"UPDATE User_Group
SET  GroupName='{0}', TotalSize={1}, MonthPrice=0, DefaultGroup={2}, Active=1
WHERE GroupID={3}",Utils.SqlInput(groupName.Text),totalSize.Text.ToLower().Replace("m",""),DefaultGroup.Checked?1:0, groupID.Text);
        SpacesDataProvider.Instance().ExecuteNonQuery(sql);
        GroupView.EditIndex = -1;
        BindGroup();
    }

    void GroupView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GroupView.EditIndex = -1;
        BindGroup();
    }

    void GroupView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GroupView.EditIndex = e.NewEditIndex;
        BindGroup();
    }

    private void BindGroup()
    {
        string sql = "SELECT * FROM User_Group";
        DataView dv = SpacesDataProvider.Instance().GetDataView(sql);
        this.GroupView.DataSource = dv;
        GroupView.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string groupName = Utils.SqlInput(txtGroupName.Text);
        int totalSize = int.Parse(txtTotalSize.Text.ToLower().Replace("m",""));
        int defaultGroup = chkDefaultGroup.Checked ? 1 : 0;
        string sql = string.Empty;
        if (defaultGroup == 1)
        {
            sql = "UPDATE User_Group SET  DefaultGroup=0";
            SpacesDataProvider.Instance().ExecuteNonQuery(sql);
        }
        sql = string.Format(@"INSERT INTO User_Group( GroupName, TotalSize, MonthPrice, DefaultGroup, Active)
VALUES('{0}', {1}, 0, {2}, 1)", groupName, totalSize, defaultGroup);
        SpacesDataProvider.Instance().ExecuteNonQuery(sql);
        GroupView.EditIndex = -1;
        BindGroup();
    }
}
