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 = "", fullName = "", dateOfBirth = "", gender = "";
                try
                {
                    con.Open();
                    cmd.CommandText = "select fullName, gender, dateOfBirth, studentPhotoUrl from t_Students Where studentId = " + 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) : "");
                            dateOfBirth = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            photourl = (rdr.GetValue(3) != DBNull.Value ? rdr.GetString(3) : "");
                        }
                    }
                    rdr.Close();
                    if (File.Exists(Request.PhysicalApplicationPath + @"photoes\" + photourl))
                    {
                        imgUserPhoto.ImageUrl = "~/photoes/" + photourl;
                    }
                    else
                    {
                        imgUserPhoto.ImageUrl = "~/photoes/" + "noPersonalPhoto.gif";
                    }
                    lblFullName.Text = (fullName.Length > 0 ? fullName : "");
                    lblGender.Text = (gender.Length > 0 ? gender : "");
                    lblDateOfBirth.Text = (dateOfBirth.Length > 0 ? dateOfBirth : ""); 
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }

    protected void btnSendPhoto_Click(object sender, EventArgs e)
    {
        try
        {
            if (fuUserPhoto.HasFile)
            {
                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 studentId = 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_Students", "studentPhotoUrl", "userName", User.Identity.Name);
                        if (File.Exists(Request.PhysicalApplicationPath + @"photoes\" + currentUrl))
                            File.Delete(Request.PhysicalApplicationPath + @"photoes\" + currentUrl);
                        cmd.CommandText = "update t_Students SET studentPhotoUrl = N'" + userPhotoUrl + "' Where studentId = " + studentId;
                        cmd.ExecuteNonQuery();
                        imgUserPhoto.ImageUrl = "~/photoes/" + userPhotoUrl;
                    }
                    catch
                    {
                        File.Delete(Request.PhysicalApplicationPath + @"photoes\" + userPhotoUrl);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
        }
        catch
        {
            if (File.Exists(Request.PhysicalApplicationPath + @"photoes\" + userPhotoUrl))
                File.Delete(Request.PhysicalApplicationPath + @"photoes\" + userPhotoUrl);
        }
    }
    protected void btnBack_Click1(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Default.aspx");
        }
        catch
        {
        }
    }
}
