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 rl_admins_pg_EditStudent : System.Web.UI.Page
{
    //***********************************
    SqlConnection con;
    SqlTransaction tr;
    static string defUrl = "";
    //***********************************
    protected void Page_Load(object sender, EventArgs e)
    {
       try
       {
           ScriptManager sm = ScriptManager.GetCurrent(this.Page);
           sm.RegisterPostBackControl(btnOk);
           string conString = RamanConnectionString.LocalConnectionString();
           con = new SqlConnection(conString);
           C_myClass.MenueItemsCreate(User, panelMenues);
           Label lblTitle = (Label)this.Master.FindControl("lblTitle");
           lblTitle.Text = "ویرایش دانشجو";
           if (!Page.IsPostBack)
           {
               string fullName = "", gender = "", tahsilat = "", stuBirth = "", stuMelliCode = "";
               int studentId = Int32.Parse(Request.QueryString["studentId"]);
               con.Open();
               SqlCommand cmd = con.CreateCommand();
               cmd.CommandText = "select fullName, gender, tahsilat, dateOfBirth, melliCode ,studentPhotoUrl FROM t_Students Where studentId = " + studentId;
               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) : "");
                       tahsilat = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                       stuBirth = (rdr.GetValue(3) != DBNull.Value ? rdr.GetString(3) : "");
                       stuMelliCode = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                       defUrl = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                   }
               }
               rdr.Close();
               //////////////////////////////////// set page controls :
               tbFullName.Text = fullName;
               tbBirthDate.Text = stuBirth;
               tbMelliCode.Text = stuMelliCode;
               ListItem tahsilatItem = new ListItem(tahsilat);
               cmbTahsilat.SelectedIndex = cmbTahsilat.Items.IndexOf(tahsilatItem);
               rbMale.Checked = (gender == "مرد" ? true : false);
               rbFemale.Checked = (gender == "زن" ? true : false);
               string photoesFolderPath = Request.PhysicalApplicationPath + @"photoes\";
               if (File.Exists(photoesFolderPath + defUrl))
               {
                   imgPhoto.ImageUrl = "~/photoes/" + defUrl;
               }
               else
               {
                   imgPhoto.BorderStyle = BorderStyle.None;
                   imgPhoto.ImageUrl = "~/photoes/noPersonalPhoto.gif";
               }
           }
       }
       catch
       {
       }
       finally
       {
           con.Close();
       }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            int studentId = Int32.Parse(Request.QueryString["studentId"]);
            bool isValid = false;
            string fileName = "", fullName = "", gender = "", tahsilat = "", dateOfBirth = "", melliCode = "",photoUrl = defUrl;
            if (FileUpload1.HasFile)
            {
                fileName = FileUpload1.FileName;
                isValid = C_myClass.IsValidFileType(fileName);
            }
            fullName = tbFullName.Text;
            gender = (rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : ""));
            tahsilat = (cmbTahsilat.SelectedIndex > 0 ? cmbTahsilat.SelectedItem.ToString() : "");
            dateOfBirth = tbBirthDate.Text;
            melliCode = tbMelliCode.Text;
            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();
                photoUrl = fileName + @"." + ext;
                FileUpload1.SaveAs(Request.PhysicalApplicationPath + @"photoes\" + photoUrl);
            }
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            con.Open();
            tr = con.BeginTransaction();
            SqlCommand cmd = con.CreateCommand();
            cmd.Transaction = tr;
            cmd.CommandText = t_StudentsUpdateCommand(studentId, fullName, gender, tahsilat, dateOfBirth, melliCode, photoUrl);
            cmd.ExecuteNonQuery();
            tr.Commit();
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "با موفقیت ذخیره شد.";
        }
        catch (Exception ex)
        {
            try
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "عملیات ذخیره سازی با شکست مواجه شد ، لطفاً مجدداً سعی نمایید.";
                tr.Rollback();
            }
            catch
            {
            }
        }
        finally
        {
            con.Close();
        }
    }

    private string t_StudentsUpdateCommand(int StudentId, string FullName, string Gender, string Tahsilat, string DateOfBirth, string MelliCode, string StudentPhotoUrl)
    {
        string comm = "update t_Students SET " +
            "fullName = N'" + FullName + "' , " +
            "gender = N'" + Gender + "' , " +
            "tahsilat = N'" + Tahsilat + "' , " +
            "dateOfBirth = N'" + DateOfBirth + "' , " +
            "melliCode = N'" + MelliCode + "' , " +
            "studentPhotoUrl = N'" + StudentPhotoUrl + "' " +
            "Where studentId = " + StudentId;
        return comm;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string ret = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(ret);
        }
        catch
        {
        }
    }
}
