using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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 UserInfo;
using Base;
public partial class _UserInfo : Base.BasePage
{
    private int pageSize = 12;//这里设置分页尺寸
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("User", "A1");
        if (!IsPostBack)
        {
            Bind();
        }
    }
    ///**************************************************
    /// <summary>
    /// depiction：实现对用户的绑定
    /// </summary>
    /// <param name="<参数名称>"><参数说明></param>
    /// <returns>
    ///<对方法返回值的说明，该说明必须明确说明返回的值代表什么含义>
    /// </returns>
    ///Writer：作者中文名
    ///Create Date：<方法创建日期，格式：YYYY-MM-DD>
    ///**************************************************

    private void Bind()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
        string strWhere = "1=1";
        string LoginName = txtLoginName.Text;
        string CompanyName = txtCompanyName.Text;

        if (LoginName != "")
            strWhere += " and LoginName like '%" + LoginName + "%'";
        if (CompanyName != "")
            strWhere += " and CompanyName like '%" + CompanyName + "%'";
        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_UserInfo]";
            PageNo1.FldName = "RegDate";
            PageNo1.StrGetFields = "*";
            DataTable table = PageNo1.BindViewDesc();
            grdUser.DataSource = table.DefaultView;
            grdUser.DataBind();
        }
        else
        {
            PageNo1.Pageindex = 1;
            PageNo1.PageSize = 12;
            PageNo1.Where = "1=1";
            PageNo1.Table = "[U_UserInfo]";
            PageNo1.FldName = "RegDate";
            PageNo1.StrGetFields = "*";
            DataTable table = PageNo1.BindViewDesc();
            grdUser.DataSource = table.DefaultView;
            grdUser.DataBind();
        }
    }
    public string GetShortDate(string dateStr)
    {
        if (dateStr != string.Empty)
            return Convert.ToDateTime(dateStr).ToShortDateString();
        else
            return string.Empty;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= grdUser.Rows.Count - 1; i++)
        {

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

                int UserID = Convert.ToInt32(grdUser.DataKeys[i].Value);
                IUserInfo RUser = new RUserInfo();
                int m = RUser.Delete(UserID);
            }
        }
        Bind();
    }
    protected void lbtnUpdate_Command(object sender, CommandEventArgs e)
    {

        TUserInfo TUser = new TUserInfo();
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        int userid = int.Parse(Content.Substring(0, index));
        if (index < -1 || index + 1 > Content.Length)
            return;

        TUser.UserID = userid;
        TUser.Verify = Convert.ToBoolean(Content.Substring(index + 1));
        IUserInfo RUser = new RUserInfo();
        RUser.UpdateVerify(TUser);
        Bind();
        //会员信息审核通过发送邮件通知会员
        LinkButton btn = sender as LinkButton;
        int rowIndex = (btn.NamingContainer as GridViewRow).RowIndex;
        int id = int.Parse(grdUser.DataKeys[rowIndex].Value.ToString());
        if (btn.Text != "已审")
        {
            string strFlag = Base.XMLProcess.Read("xml/EmailFlag.xml", "/Item/flag/mode[@type='userVerify']", "value");
            if (strFlag == "")
                strFlag = "us_verify";
            nspBase.Core.clsEmail.SendMail(strFlag, id);
        }

    }
    protected void grdUser_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)");
            LinkButton btn = (LinkButton)e.Row.FindControl("lbtnUpdate");
            LinkButton btnLock = (LinkButton)e.Row.FindControl("lbtnUpdateLockedup");
            if (btn.Text == "已审")
                btn.Attributes.Add("onclick", "javascript:return confirm('你要取消审核吗？')");
            else
                btn.Attributes.Add("onclick", "javascript:return confirm('你要让该会员通过审核吗？请注意该会员是否满足该等级的条件要求！通过审核后,将开通该会员服务功能？')");
            if (btnLock.Text == "已锁定")
                btnLock.Attributes.Add("onclick", "javascript:return confirm('你要取消锁定吗？')");
            else
                btnLock.Attributes.Add("onclick", "javascript:return confirm('你要锁定吗？')");

        }



    }
    protected void lbtnUpdateLockedup_Command(object sender, CommandEventArgs e)
    {
        TUserInfo TUser = new TUserInfo();
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        if (index < -1 || index + 1 > Content.Length)
            return;

        TUser.UserID = int.Parse(Content.Substring(0, index));
        TUser.Verify = Convert.ToBoolean(Content.Substring(index + 1));
        IUserInfo RUser = new RUserInfo();
        RUser.UpdateLocked(TUser);
        Bind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strWhere = "where 1=1";
        string LoginName = txtLoginName.Text.Trim();
        string CompanyName = txtCompanyName.Text.Trim();

        if (LoginName != "")
            strWhere += " and LoginName like '%" + LoginName + "%'";
        if (CompanyName != "")
            strWhere += " and CompanyName like '%" + CompanyName + "%'";
        IUserInfo RUser = new RUserInfo();
        DataTable dt = RUser.GetDataTable(strWhere);
        if (dt.Rows.Count == null)
        {
            lblMessage.Visible = true;
            lblMessage.Text = "<font color='red'>暂无数据</font>";
        }
        else
            lblMessage.Visible = false;
        grdUser.DataSource = dt.DefaultView;
        grdUser.DataBind();
    }


    protected void lnkdelete_Command(object sender, CommandEventArgs e)
    {
        int UserID = Convert.ToInt32(e.CommandArgument.ToString());
        IUserInfo RUser = new RUserInfo();
        int m = RUser.Delete(UserID);
        Bind();
    }
}
