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;
using worm;
using wormstrdo;

public partial class cusfiles : System.Web.UI.Page
{
    protected string cnames;
    protected string cpws;
    protected string csexs;
    protected string cqqs;
    protected string cmails;
    protected string caddresss;
    protected string cheads;
    protected string cinstroduces;
    protected string cdates;
    protected string cleis;
    protected int cjis;
    protected int cdengs;
    protected int csends;
    protected string sql;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = "SkinFile";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            resetfile();
            customerfile();
        }
        else 
        {
            resetfile();
            
            
        }
    }

    protected void customerfile()        //读取数据库获取用户资料
    {
        SqlConnection one = conn.sqlconn();
        int i;
        try
        {
            one.Open();
            sql = "select * from customer where cname='"+Session["ad"].ToString()+"' and cpw='"+Session["pw"].ToString()+"'";
            SqlCommand cmd = new SqlCommand(sql, one);  //建立SQL命令
            SqlDataReader rs = cmd.ExecuteReader();
            if (rs.Read())
            {
                cname.Text = rs["cname"].ToString();
                if (rs["csex"].ToString() == "男")
                {
                    csex.SelectedIndex = 0;
                }
                else
                { csex.SelectedIndex = 1; }
                cqq.Text = rs["cqq"].ToString();
                cmail.Text = rs["cmail"].ToString();
                caddress.Text = rs["caddress"].ToString();
                cinstroduce.Text = rs["cinstroduce"].ToString();
                clei.Text = rs["clei"].ToString();
                face.ImageUrl=rs["chead"].ToString();
                if (clei.Text == "Vip会员")
                {
                    uploadok();
                }
                cji.Text = level(Convert.ToInt32(rs["csend"]));
                csend.Text="你发表留言"+rs["csend"].ToString()+"条";
                cdeng.Text = "你登录了"+rs["cdeng"].ToString()+"次";
                for (i = 0; i <= 9; i++)
                {
                    if (rs["chead"].ToString() == headlist.Items[i].Value.ToString())
                    {
                        headlist.SelectedIndex = i;
                    }
                }
            }
            rs.Dispose();
            cmd.Dispose();
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString()+"1");
            //Response.Write("<meta http-equiv='refresh' content='5; url=Default.aspx'>");
        }
        finally
        {
            one.Close();
        }
    }
    
    protected void uploadok()    //验证用户是否可以上传自己的头像
    {
        up.Visible = true;
        fileup.Visible = true;
    }
    
    protected void resetfile()    //验证用户是否可以修改个人资料
    {
        if (Session["ad"].ToString() != "" && Session["pw"].ToString() != "" && Session["profile"].ToString() != "前台管理员")
        {
            SqlConnection one = conn.sqlconn();
            try
            {
                one.Open();
                sql = "select * from customer where cname='" + Session["ad"].ToString() + "'" + " and cpw='" + Session["pw"].ToString() + "'";
                SqlCommand cmd = new SqlCommand(sql, one);  //建立SQL命令
                SqlDataReader rs = cmd.ExecuteReader();
                if (rs.Read() == null)
                {
                    rs.Close();
                    Response.Write("<script>window.alert('你不是此用户，不能修改用户资料！')</script>");
                    Response.Write("<meta http-equiv='refresh' content='0; url=Default.aspx'>");
                }
                rs.Dispose();
                cmd.Dispose();
            }
            catch (System.Exception ee)
            {
                Response.Write(ee.Message.ToString() + "2");
                Response.Write("<meta http-equiv='refresh' content='5; url=Default.aspx'>");
            }
            finally
            {
                one.Close();
            }
        }
        else
        {
            Response.Write("<script>window.alert('你是游客，不能修改用户资料！')</script>");
            Response.Write("<meta http-equiv='refresh' content='0; url=Default.aspx'>");
        }
    }

    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;
            uploadok();
        }
        if (mylevel > 30 && mylevel <= 80)
        {
            star = 3;
            uploadok();
        }
        if (mylevel > 80)
        {
            star = 4;
            uploadok();
        }
        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 send_Click(object sender, EventArgs e)      //单击提交按钮，写入用户信息
    {
        if (yanzhengpw.IsValid == true)
        {
            SqlConnection one = conn.sqlconn();
            
            try
            {
                one.Open();
                cnames = this.cname.Text;
                cpws = this.cpw.Text;
                csexs = this.csex.Text;
                cqqs = this.cqq.Text;
                cmails = this.cmail.Text;
                caddresss = this.caddress.Text;
                cheads = this.face.ImageUrl;
                cinstroduces = this.cinstroduce.Text;

                //过滤字符串
                strreplace str = new strreplace();
                cnames = str.txtstr(cnames);
                cpws = str.txtstr(cpws);
                cqqs = str.txtstr(cqqs);
                cmails = str.txtstr(cmails);
                caddresss = str.msgstr(caddresss);
                cinstroduces = str.msgstr(cinstroduces);
                //End

                sql = "update customer set cname='" + cnames.Trim() + "',cpw='" + cpws.Trim() + "',csex='" + csexs + "',cqq='" + cqqs + "',cmail='" + cmails + "',caddress='" + caddresss + "',chead='" + cheads + "',cinstroduce='" + cinstroduces + "' where cname='" + cname.Text + "' and cpw='" + oldcpw.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, one);  //建立SQL命令
                int ok = cmd.ExecuteNonQuery();
                if (ok != 0)
                {
                    Session["ad"] = cnames;
                    Session["pw"] = cpws;
                    Response.Write("<script>window.alert('修改成功!')</script>");
                }
                cmd.Dispose();
            }
            catch (System.Exception ee)
            {
                Response.Write(ee.Message.ToString() + "3");
            }
            finally
            {
                one.Close();
            }
        }
    }

    
    
    protected void reset_Click(object sender, EventArgs e)
    {
        cname.Text = "";
        cpw.Text = "";
        cpw2.Text = "";
        cqq.Text = "";
        cmail.Text = "";
        caddress.Text = "";
        cinstroduce.Text = "";
        csex.SelectedIndex = 0;
        headlist.SelectedIndex = 0;
    }
    protected void pwcom(object sender, EventArgs e)
    {
        compw.Enabled = true;
    }

    protected void Verifypw(object source, ServerValidateEventArgs arguments)
    {
        SqlConnection one = conn.sqlconn();
       
        try
        {
            one.Open();
            sql = "select * from customer where cpw='" + oldcpw.Text + "' and cname='" + Session["ad"].ToString() + "'";
            SqlCommand cmd = new SqlCommand(sql, one);
            SqlDataReader same = cmd.ExecuteReader();
            if (same.Read())
            {
                arguments.IsValid = true;
                same.Dispose();
            }
            else
            {
                arguments.IsValid = false;
                same.Dispose();
            }
            cmd.Dispose();
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString() + "4");
        }
        finally
        {
            one.Close();
        }
    }

    //上传单击事件
    protected void up_Click(object sender, EventArgs e)
    {
        myupimg up = new myupimg();
        up._upfilecontrol = fileup;
        up._topath = "UpLoadFile\\head\\"+Session["ad"].ToString();
        up._tWidth = 40;
        up._tHeight = 40;
        up._setting = 2;
        string imgpath = up.upfile();
        imgpath = imgpath.Replace("|","").Trim();
        headlist.Items.Add(new ListItem(Convert.ToString("自定义头像"), Convert.ToString(imgpath)));
        headlist.SelectedIndex = 10;
        face.ImageUrl = imgpath;
    }
    protected void selecthead(object sender, EventArgs e)
    {
        face.ImageUrl=headlist.SelectedValue.ToString();
    }
}
