using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class pg_JoinWebSite : System.Web.UI.Page
{
    //*************************************************
    SqlConnection con;
    SqlCommand cmd;
    static string userPhotoUrl = "";
    //*************************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "عضویت در سایت";
            if (!Page.IsPostBack)
            {
                userPhotoUrl = "";
                string userName = User.Identity.Name;
                lblUserName.Text = userName;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                string photourl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "userPhotoUrl", "userName", userName);
                if (File.Exists(Request.PhysicalApplicationPath + @"photoes\" + photourl))
                {
                    imgUserPhoto.ImageUrl = "~/photoes/" + photourl;
                }
                else
                {
                    imgUserPhoto.ImageUrl = "~/photoes/" + "noPersonalPhoto.gif";
                }
                string fullName = "", gender = "";
                try
                {
                    con.Open();
                    cmd.CommandText = "select fullName, gender FROM t_Users Where userId = " + Session["iden"].ToString();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            gender = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                        }
                    }
                    rdr.Close();
                    tbFullName.Text = fullName;
                    rbMale.Checked = ( gender == "مرد" ? true : false);
                    rbFemale.Checked = (gender == "زن" ? true : false);
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string fullName = tbFullName.Text;
            string gender = ( rbMale.Checked ? "مرد" : ( rbFemale.Checked ? "زن" : ""));
            int userId = Int32.Parse(Session["iden"].ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "update t_Users SET " +
                    "fullName = N'" + fullName + "' , " +
                    "gender = N'" + gender + "' " +
                    "Where userId = " + userId;
                cmd.ExecuteNonQuery();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch(Exception ex)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "خطا در ذخیره سازی.";
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }
    protected void btnSendPhoto_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = fuUserPhoto.PostedFile.FileName;
            bool isValid = C_myClass.IsValidFileType(fileName);
            if (isValid)
            {
                string ext = C_myClass.FileExtenssion(fileName);
                string photoesPath = Request.PhysicalApplicationPath + @"photoes\";
                if (File.Exists(photoesPath + _UseFull.C_Usefull.Serialize(fileName, 7).Trim() + @"." + ext))
                {
                    Random rnd = new Random();
                    int randomNum = rnd.Next(1, 9999999);
                    fileName += randomNum.ToString();
                }
                fileName = _UseFull.C_Usefull.Serialize(fileName, 7).Trim();
                userPhotoUrl = fileName + @"." + ext;
                //FileUpload1.SaveAs(Request.PhysicalApplicationPath + @"photoes\" + photoUrl);
                fuUserPhoto.SaveAs(Request.PhysicalApplicationPath + @"photoes\" + userPhotoUrl);
                int userId = Int32.Parse(Session["iden"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    string currentUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "userPhotoUrl", "userName", User.Identity.Name);
                    if (File.Exists(Request.PhysicalApplicationPath + @"photoes\" + currentUrl))
                        File.Delete(Request.PhysicalApplicationPath + @"photoes\" + currentUrl);
                    cmd.CommandText = "update t_Users SET userPhotoUrl = N'" + userPhotoUrl + "' Where userId = " + userId;
                    cmd.ExecuteNonQuery();
                    imgUserPhoto.ImageUrl = "~/photoes/" + userPhotoUrl;
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
    protected void btnBack_Click1(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Default.aspx");
        }
        catch
        {
        }
    }
}
