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_teachers_pg_ExamResaultSet : System.Web.UI.Page
{
    //*********************************
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    //*********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "تعیین نتایج آزمون";
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                int examId = Int32.Parse(Request.QueryString["examId"].ToString());
                int classId = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Exams", "classId", "examId", examId);
                SqlDataSource1.SelectCommand = "SELECT t_Students.studentId, t_Students.fullName, t_Students.melliCode, t_Students.studentPhotoUrl FROM t_ClassStudents INNER JOIN t_Students ON t_ClassStudents.studentId = t_Students.studentId WHERE (t_ClassStudents.classId = " + classId + ")";
                GridView1.DataBind();
            }
        }
        catch
        {
        }
    }

    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
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                int examId = Int32.Parse(Request.QueryString["examId"].ToString());
                cmd.CommandText = "delete from t_ExamResaults Where examId = " + examId;
                cmd.ExecuteNonQuery();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox checkPresent = (CheckBox)GridView1.Rows[i].FindControl("checkPresent");
                    bool present = (checkPresent.Checked ? true : false);
                    int studentId = Int32.Parse(GridView1.Rows[i].Cells[2].Text);
                    TextBox tbGrade = (TextBox)GridView1.Rows[i].FindControl("tbGrade");
                    int grade = (tbGrade.Text.Length > 0 ? Int32.Parse(tbGrade.Text) : -1);
                    DropDownList cmbStatus = (DropDownList)GridView1.Rows[i].FindControl("cmbStatus");
                    string status = (cmbStatus.SelectedIndex > 0 ? cmbStatus.SelectedItem.ToString() : "");
                    TextBox tbStatements = (TextBox)GridView1.Rows[i].FindControl("tbStatements");
                    string statements = tbStatements.Text;
                    cmd.CommandText = t_ResaulsInsertCommand(examId, studentId, grade, status, statements, present);
                    cmd.ExecuteNonQuery();
                }
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "ذخیره سازی با شکست مواجه شد.";
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "ذخیره سازی با شکست مواجه شد.";
        }
    }

    private string t_ResaulsInsertCommand(int ExamId, int StudentId, int Grade, string Status, string Statements, bool Present)
    {
        string res = "insert into t_ExamResaults (examId, studentId, grade, status, statements, present) values (" +
            ExamId + " , " +
            StudentId + " , " +
            Grade + " , N'" +
            Status + "', N'" +
            Statements + "', " +
            (Present ? 1 : 0) + " )";
        return res;
    }

    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 FindMaxGrade()
    {
        string res = "";
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        cmd = con.CreateCommand();
        try
        {
            con.Open();
            int examId = Int32.Parse(Request.QueryString["examId"].ToString());
            int maxGrade = _UseFull.C_Usefull.FindIntegerWithIntegerShart(con, cmd, "t_Exams", "maxGrade", "examId", examId);
            res = "از " + maxGrade;
        }
        catch
        {
        }
        finally
        {
            con.Close();
        }
        return res;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int grade = 0;
                bool present = true;
                string status = "", statements = "";
                int examId = Int32.Parse(Request.QueryString["examId"].ToString());
                int studentId = Int32.Parse(e.Row.Cells[2].Text);
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select grade, status, statements, present FROM t_ExamResaults Where examId = " + examId + " AND studentId = " + studentId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            grade = ( rdr.GetValue(0) != DBNull.Value ? rdr.GetInt32(0) : -1);
                            status = (rdr.GetValue (1) != DBNull.Value ? rdr.GetString(1) : "");
                            statements = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            present = (rdr.GetValue(3) != DBNull.Value ? rdr.GetBoolean(3) : true);
                        }
                    }
                    rdr.Close();
                    TextBox tbGrade = (TextBox)e.Row.FindControl("tbGrade");
                    tbGrade.Text = ( (grade != -1) && (grade != 0) ? grade.ToString():"");
                    DropDownList cmbStatus = (DropDownList)e.Row.FindControl("cmbStatus");
                    if (status.Length > 0)
                    {
                        ListItem itemStatus = new ListItem(status);
                        cmbStatus.SelectedIndex = cmbStatus.Items.IndexOf(itemStatus);
                    }
                    TextBox tbStatements = (TextBox)e.Row.FindControl("tbStatements");
                    tbStatements.Text = statements;
                    CheckBox checkPresent = (CheckBox)e.Row.FindControl("checkPresent");
                    checkPresent.Checked = ( present ? true : false);
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }

        }
        catch
        {
        }
    }
}
