using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Self_Project
{
    public partial class ChangeStudent : Form
    {
        
        public ChangeStudent()
        {
            InitializeComponent();
        }
        public string STDNUM, CARTNUM = "";
        Student Std = new Student();
        private void ChangeStudent_Load(object sender, EventArgs e)
        {
            SearchStudent FF = new SearchStudent();
            FF.ShowDialog();
            STDNUM = FF.STDNUM;
            if (STDNUM == "" || STDNUM==null) {return;}
            //----------------------------------------------
            StudentType Model = new StudentType();
            Model.ShowModels(txt_Student_Type);
            //----------------------------------------------
            Std.ShowInfo(STDNUM);
            txt_Student_ID.TEXT = Std.ID.ToString();
            txt_Cart_ID.TEXT = Std.CartID.ToString();
            CARTNUM = Std.CartID.ToString();
            txt_Student_Name.TEXT = Std.Name;
            txt_Student_Family.TEXT = Std.Family;
            txt_Student_Price.TEXT = Std.Price.ToString();
            txt_Student_Type.SelectedValue = Std.Type;
        }

        private void ChangeStudent_Activated(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (STDNUM == "" || STDNUM == null) { this.Close(); return; }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (txt_Student_ID.TEXT == "" || txt_Cart_ID.TEXT == "" || txt_Student_Name.TEXT == "" || txt_Student_Family.TEXT == "")
            {
                MessageBox.Show("لطفا اطلاعات را کامل وارد نمایید", "اطلاعات ناقص");
                if (txt_Student_Family.TEXT == "") txt_Student_Family.Focus();
                if (txt_Student_Name.TEXT == "") txt_Student_Name.Focus();
                if (txt_Cart_ID.TEXT == "") txt_Cart_ID.Focus();
                if (txt_Student_ID.TEXT == "") txt_Student_ID.Focus();
                return;
            }
            //----------------------------------------------------------------
            Student Std = new Student();
            try
            {
                Std.ID = Convert.ToInt32(txt_Student_ID.TEXT);
                Std.CartID = Convert.ToInt32(txt_Cart_ID.TEXT);
                Std.Name = txt_Student_Name.TEXT;
                Std.Family = txt_Student_Family.TEXT;
                Std.Price = Convert.ToInt32(txt_Student_Price.TEXT);
                Std.Type = Convert.ToSByte(txt_Student_Type.SelectedValue);
            }
            //------------
            catch (Student_Exceptions_ID ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Student_ID.Focus();
                return;
            }
            //------------
            catch (Student_Exceptions_Name ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Student_Name.Focus();
                return;
            }
            //------------
            catch (Student_Exceptions_Family ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Student_Family.Focus();
                return;
            }
            //------------
            catch (Student_Exceptions_CartID ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Cart_ID.Focus();
                return;
            }
            //------------
            catch (Student_Exceptions_Price ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Student_Price.Focus();
                return;
            }
            //------------
            catch (Student_Exceptions_Type ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Student_Type.Focus();
                return;
            }
            //------------
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            //----------------------------------------------------------------
            if(Std.ID != Convert.ToInt32(STDNUM))
                if (Std.IsDuplicate(Student.ControlType.Student_ID))
                {
                    MessageBox.Show("شماره دانشجویی وارد شده، تکراری است", "اطلاعات تکراری", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txt_Student_ID.Focus();
                    return;
                }
            //-----------------------------------------------------------------
            if(Std.CartID != Convert.ToInt32(CARTNUM))
                if (Std.IsDuplicate(Student.ControlType.Cart_ID))
                {
                    MessageBox.Show("شماره کارت وارد شده، تکراری است", "اطلاعات تکراری", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txt_Cart_ID.Focus();
                    return;
                }
            //-----------------------------------------------------------------
            try
            {
                Std.ChangeInfo(Student.ChangeType.Student, STDNUM);
                MessageBox.Show("اطلاعات با موفقیت تغییر یافت", "تغییر اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "خطا در عملیات", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}