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_PresenceClassSet : System.Web.UI.Page
{
    //*************************************
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    //*************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                string sessionInfo = "", sessionDate = "", sessionTime = "";
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "حضور غیاب جلسه";
                int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select sessionDate, sessionTime FROM t_ClassSessions Where sessionId = " + sessionId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            sessionDate = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            sessionTime = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                        }
                    }
                    rdr.Close();
                    sessionInfo = "تاریخ برگزاری جلسه : " + sessionDate + " ساعت " + sessionTime;
                    lblSessionInfo.Text = sessionInfo;
                    //////////////////////////////////////////////////
                    int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_ClassSessions", "classId", "sessionId", sessionId);
                    SqlDataSource1.SelectCommand = "SELECT t_Students.studentId, t_Students.fullName, t_Students.gender, t_Students.tahsilat, t_Students.dateOfBirth, t_Students.melliCode, t_Students.studentPhotoUrl, t_Students.userName, t_Students.userCreater, t_ClassStudents.classId FROM t_Students INNER JOIN t_ClassStudents ON t_Students.studentId = t_ClassStudents.studentId Where t_ClassStudents.classId = " + classId;
                    GridView1.DataBind();
                    //////////////////////////////////////////////////
                    imgTeacherPhoto.ImageUrl = FindTeacherImageUrl();
                    lblTeacherName.Text = FindTeacherName();
                    ////////////////////////////////////////////////// set current :
                    SetTeacherCurrentPresence();

                }
                catch(Exception ex)
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected string FindImageUrl(object StudentId)
    {
        string url = "~/photoes/";
        string conString = RamanConnectionString.LocalConnectionString();
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = con.CreateCommand();
        string picUrl = "";
        try
        {
            int studentId = Int32.Parse(StudentId.ToString());
            con.Open();
            cmd.CommandText = "select studentPhotoUrl from t_Students Where studentId = " + studentId;
            picUrl = cmd.ExecuteScalar().ToString();
            string photoesFolderPath = Request.PhysicalApplicationPath + @"photoes\";
            if (!File.Exists(photoesFolderPath + picUrl))
            {
                picUrl = url + "noPersonalPhoto.gif";
            }
            else
            {
                picUrl = url + picUrl;
            }
        }
        catch
        {
            picUrl = "";
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
        return picUrl;
    }

    protected string FindTeacherImageUrl()
    {
        string picUrl = "";
        try
        {
            string rootUrl = "~/photoes/"; 
            string photoesFolderPath = Request.PhysicalApplicationPath + @"photoes\";
            int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
            int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_ClassSessions", "classId", "sessionId", sessionId);
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            int teacherId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Classes", "teacherId", "classId", classId);
            picUrl = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Teachers", "teacherPhotoUrl", "teacherId", teacherId);
            if (!File.Exists(photoesFolderPath + picUrl))
            {
                picUrl = rootUrl + "noPersonalPhoto.gif";
            }
            else
            {
                picUrl = rootUrl + picUrl;
            }
        }
        catch(Exception ex)
        {
            picUrl = ex.Message;
        }
        return picUrl;
    }

    private string FindTeacherName()
    {
        string res = "";
        try
        {
            int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
            int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_ClassSessions", "classId", "sessionId", sessionId);
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            int teacherId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Classes", "teacherId", "classId", classId);
            res = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Teachers", "fullName", "teacherId", teacherId);
        }
        catch (Exception ex)
        {
        }
        return res;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandText = "delete from t_StudentsPresence Where sessionId = " + sessionId;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from t_TeachersPresence Where sessionId = " + sessionId;
                cmd.ExecuteNonQuery();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    int studentId = Int32.Parse(GridView1.Rows[i].Cells[1].Text);
                    DropDownList cmbPresence = (DropDownList)GridView1.Rows[i].FindControl("cmbPresence");
                    string status = (cmbPresence.SelectedIndex > 0 ? cmbPresence.SelectedItem.ToString() : "حاضر");
                    cmd.CommandText = "insert into t_StudentsPresence (sessionId, studentId, status) values (" +
                        sessionId + " , " +
                        studentId + " , N'" +
                        status + "')";
                    cmd.ExecuteNonQuery();
                }
                int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_ClassSessions", "classId", "sessionId", sessionId);
                int teacherId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Classes", "teacherId", "classId", classId);
                string tStatus = (cmbTeacherPresence.SelectedIndex > 0 ? cmbTeacherPresence.SelectedItem.ToString() : "حاضر");
                cmd.CommandText = "insert into t_TeachersPresence (teacherId, sessionId, status) values (" +
                    teacherId + " , " +
                    sessionId + " , N'" +
                    tStatus + "')";
                cmd.ExecuteNonQuery();
                /////////////////////////////////////////
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch(Exception ex)
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.Message;
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }

    private void SetTeacherCurrentPresence()
    {
        try
        {
            string status = "";
            int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_ClassSessions", "classId", "sessionId", sessionId);
            int teacherId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Classes", "teacherId", "classId", classId);
            try
            {
                con.Open();
                cmd.CommandText = "select status from t_TeachersPresence Where sessionId = " + sessionId + " AND teacherId = " + teacherId;
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        status = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                    }
                }
                rdr.Close();
                ListItem itemStatus = new ListItem(status);
                cmbTeacherPresence.SelectedIndex = cmbTeacherPresence.Items.IndexOf(itemStatus);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string status = "";
                int studentId = Int32.Parse(e.Row.Cells[1].Text);
                int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select status from t_StudentsPresence Where sessionId = " + sessionId + " AND studentId = " + studentId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            status = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                        }
                    }
                    rdr.Close();
                    ListItem itemStatus = new ListItem(status);
                    DropDownList cmbStatus = (DropDownList)e.Row.FindControl("cmbPresence");
                    cmbStatus.SelectedIndex = cmbStatus.Items.IndexOf(itemStatus);
                }
                catch(Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
}
