using System;
using System.Web.UI;
using System.Data;
using System.Drawing;

namespace CH78
{
    public partial class Display : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string studentID = "";
            string gender;

            if (!IsPostBack)
            {
                // retrieve the querystring
                if (String.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    // not the right entry point
                    Response.Redirect("Home.aspx");
                }

                else if (Request.QueryString["ID"].Trim().ToUpper() == "NEW")
                {
                    // do nothing
                }

                else if (Request.QueryString["type"].Trim().ToUpper() == "VIEW") // display
                {
                    GetStudent(Request.QueryString["ID"].Trim(), Request.QueryString["type"].Trim().ToUpper());
                }

                else if (Request.QueryString["type"].Trim().ToUpper() == "EDIT") // edit
                {
                    GetStudent(Request.QueryString["ID"].Trim(), Request.QueryString["type"].Trim().ToUpper());
                }

                else // anything else
                {
                    Response.Redirect("Home.aspx");
                }
            }
        }

        public void GetStudent(string studentid, string type)
        {
            // the right record
            StudentDataTier stuDT = new StudentDataTier();
            DataSet ds = new DataSet();
            string gender;
            ds = StudentDataTier.SearchStudents("","",studentid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtStuID.Text = ds.Tables[0].Rows[0]["student_id"].ToString();
                txtFNAME.Text = ds.Tables[0].Rows[0]["fname"].ToString();
                txtLNAME.Text = ds.Tables[0].Rows[0]["lname"].ToString();
                // TODO complete other fields

                gender = ds.Tables[0].Rows[0]["gender_desc"].ToString().Trim();
                if (gender.ToUpper() == "F")
                {
                    txtGender.SelectedItem.Text = "FEMALE";
                }
                else
                {
                    txtGender.SelectedItem.Text = "MALE";
                }

                txtGender.Enabled = false;
                //mysalary = Decimal.Parse(ds.Tables[0].Rows[0]["salary"])
                //txtSalary.Text = mysalary.ToString("C")
                //txtSalary.Enabled = False

                txtState.DataSource = StudentDataTier.GetStates();
                txtState.DataTextField = "FullAndAbbrev";
                txtState.DataValueField = "abbreviation";
                txtState.SelectedValue = "PA";
                txtState.DataBind();

                txtState.SelectedValue = ds.Tables[0].Rows[0]["state"].ToString();
                if (type.ToUpper() == "VIEW")
                {
                    txtStuID.Enabled = false;
                    txtFNAME.Enabled = false;
                    txtLNAME.Enabled = false;
                    btnGoStu.Visible = false;
                    btnCancelStu.Visible = false;
                }
                else if (type.ToUpper() == "EDIT")
                {
                    txtStuID.Enabled = false;
                    btnGoStu.Visible = true;
                    btnCancelStu.Visible = true;
                    btnGoStu.Text = "UPDATE";
                    btnCancelStu.Text = "REFRESH";
                    btnGoStu.ForeColor = Color.Red;
                    btnCancelStu.ForeColor = Color.Blue;
                }
            }
        }

        protected void btnCancelStu_OnClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void btnGoStu_OnClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}