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

using System.Data.OleDb;

using Microsoft.VisualBasic;

namespace CourseReg
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        #region "Declarations"
        static string StrCon;
        OleDbConnection con;
        OleDbDataAdapter dAdapter;
        DataSet dSet;
        //OleDbConnection conPayMent;
        OleDbDataAdapter dAdapterPayMent;
        DataSet dSetPayMent;
        OleDbDataAdapter dAdapterCourseNew;
        DataSet dSetX;
        
        static string strGender;
        static int intMarks;
        static long longFees,longTotalFees;
        static string strDiscount;//strCourse,
        #endregion

        private bool Md_Dispose()
        {
            DialogResult dResult;
            dResult = MessageBox.Show("Do you want to close?", "Course Registration", 
                MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (dResult.ToString() == "Yes")
            {
                con.Close();
                return true;
            }
            else
                return false;
        }
                
        public void Md_CourseComboList()
        {
            /*string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Users\\Raviz\\Desktop\\Course_XMl.mdb";
            OleDbConnection Xcon = new OleDbConnection(strCon);
            Xcon.Open();
            OleDbDataAdapter XAdapter = new OleDbDataAdapter("select * from Table1", Xcon);
            XAdapter.Fill(XSet);*/
            try
            {
                
                Md_SettingsCourseNew();
                //MessageBox.Show(XSet.Tables[0].Rows[3].ItemArray[1].ToString());
                comboBoxCourse.Items.Clear();
                foreach (DataRow row in dSetX.Tables[0].Rows)
                {
                    comboBoxCourse.Items.Add(row.ItemArray[0].ToString());
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp,"Course Registration");
            }
        }
        private void Md_Settings()
        {
            try
            {
                
                //System.Reflection.Assembly.GetExecutingAssembly().Location;
                con = new OleDbConnection();
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + StrCon;
                con.Open();
                dAdapter = new OleDbDataAdapter("select * from 1StuDetail", con);
                dSet = new DataSet();
                dAdapter.Fill(dSet);
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }
        private void Md_SettingsPayMent()
        {
            try
            {
                
                //System.Reflection.Assembly.GetExecutingAssembly().Location;
                con = new OleDbConnection();
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + StrCon;
                con.Open();
                dAdapterPayMent = new OleDbDataAdapter("select * from 2PayMent", con);
                dSetPayMent = new DataSet();
                dAdapterPayMent.Fill(dSetPayMent);
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }
        private void Md_SettingsCourseNew()
        {
            try
            {
                //System.Reflection.Assembly.GetExecutingAssembly().Location;
                con = new OleDbConnection();
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + StrCon;
                con.Open();
                dAdapterCourseNew = new OleDbDataAdapter("select * from CourseNew", con);
                dSetX = new DataSet();
                dAdapterCourseNew.Fill(dSetX);
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }
        private void Md_ClearAll()
        {
            Md_AppNoCombo();
            Md_CourseComboList();
            textBoxAppNo.Text = "";
            textBoxName.Text = "";
            radioButtonFemale.Checked = false; radioButtonMale.Checked = false;
            textBoxAddress.Text = "";
            textBoxPhNo.Text = "";
            textBoxOccup.Text = "";
            textBoxMarks.Text = "";
            comboBoxG1AppNo.Focus();
        }

        
        //
       /* private void button1_Click(object sender, EventArgs e)
        {
            //Md_CourseComboList();
            Md_Settings();
            dSetX.WriteXml("D:\\Users\\Raviz\\Desktop\\Course.xml");
            DataRow trow;
            //DataColumn column = new DataColumn();
            //column = dSet.Tables["StuDetail"].Columns[1];
            dSet.Tables[0].PrimaryKey = (new DataColumn[] { dSet.Tables[0].Columns[0] });
            trow = dSet.Tables[0].Rows.Find(12);
            MessageBox.Show(trow["Name"].ToString());
        }*/
        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox3.Visible = false;
            groupBoxNewRegist.Visible = false;
            groupBoxCashMgt.Visible = false;
            StrCon = @System.IO.Directory.GetCurrentDirectory() + @"\Course_XML.mdb";
            Md_CourseComboList();
            groupBoxNewRegist.Left = 10; groupBoxNewRegist.Top = 30;
            groupBoxCashMgt.Left = 10; groupBoxCashMgt.Top = 30;
            groupBox3.Left = 10; groupBox3.Top = 30;
        }
        private void Md_FeeCalculation()
        {
            Md_SettingsCourseNew();
            dSetX.Tables[0].PrimaryKey=new DataColumn[]{dSetX.Tables[0].Columns["COURSE"]};
            DataRow cRow = dSetX.Tables[0].Rows.Find(comboBoxCourse.Text);
            longTotalFees=long.Parse(cRow.ItemArray[1].ToString());
            longFees = long.Parse(cRow.ItemArray[1].ToString());
            if (intMarks >= 90)
            {
                longFees = longFees - (longFees / 10);
                strDiscount="50%";
            }
            else if (intMarks >= 80)
            {
                longFees = longFees - (longFees / 20);strDiscount="40%";
            }
            else if (intMarks >= 70)
            {
                longFees = longFees - (longFees / 30);strDiscount="30%";
            }
            else if (intMarks >= 60)
            {
                longFees = longFees - (longFees / 40);strDiscount="20%";
            }
            else if (intMarks >= 50)
            {
                longFees = longFees - (longFees / 50); strDiscount = "10%";
            }
            else
            {
                strDiscount = "No Discount";
            }
        }
        private void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                //StuDetail table
                Md_Settings();
                OleDbCommand oleCom = new OleDbCommand();
                oleCom.CommandText = "insert into 1stuDetail Values(" + textBoxAppNo.Text + ",'" + textBoxName.Text + "','" +
                    strGender + "','" + dateTimePickerDOB.Text + "','" + textBoxAddress.Text + "','" + textBoxPhNo.Text + "','" +
                    textBoxOccup.Text + "'," + textBoxMarks.Text + ",'" + comboBoxCourse.Text + "','" + dateTimePickerDOJ.Text + "')";
                oleCom.Connection = con;                 
                oleCom.ExecuteNonQuery();
                
                //PayMent table
                //Md_SettingsPayMent();
                intMarks = Int32.Parse(textBoxMarks.Text);
                Md_FeeCalculation();////////////////
                oleCom.CommandText = "insert into 2PayMent values(" + textBoxAppNo.Text + "," + longTotalFees +
                    "," + longFees + ",0," + longFees + ",0,'" + strDiscount + "')";
                oleCom.ExecuteNonQuery();
                //oleCom.CommandText=" FEES@@@@@@@@@@@@@@@@@@
                MessageBox.Show("New Record Added ->" + oleCom.CommandText, "Course Registration");
                //
                Md_ClearAll();
                con.Close();
                textBoxAppNo.Focus();
               
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
                        
        }

        private void radioButtonFemale_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonFemale.Checked)
                strGender = "Female";
        }

        private void radioButtonMale_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonMale.Checked)
                strGender = "Male";
        }

        void editNewToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            Md_ClearAll();
            comboBoxG1AppNo.Visible = true;
            groupBoxNewRegist.Visible = true;
            groupBoxCashMgt.Visible = false;
            btnRegister.Visible = false;
            btnDelete.Visible = false;
            btnEditUpdate.Visible = true;
            textBoxAppNo.Visible = false;
            comboBoxG1AppNo.Focus();
            label10.Text = "Updating the Records";
            /////////
            

        } 

        private void newRegistrationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Md_ClearAll();
            groupBoxNewRegist.Visible = true;
            groupBoxCashMgt.Visible = false;
            textBoxAppNo.Visible = true;
            btnDelete.Visible = false;
            btnRegister.Visible = true;
            btnEditUpdate.Visible = false;
            comboBoxG1AppNo.Visible = false;
            textBoxAppNo.Focus();
            label10.Text = "Registering New Records";
        }

        private void deleteRecordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Md_ClearAll();
            textBoxAppNo.Visible = false;
            comboBoxG1AppNo.Visible = true;
            groupBoxNewRegist.Visible = true;
            groupBoxCashMgt.Visible = false;
            btnDelete.Visible = true;
            btnRegister.Visible = false;
            btnEditUpdate.Visible = false;
            comboBoxG1AppNo.Focus();
            label10.Text = "Deleting The Records";
        }

        private void mainPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBoxNewRegist.Visible = false;
            groupBoxCashMgt.Visible = false;
        }
        private void Md_AppNoCombo()
        {
            try
            {
                Md_Settings();
                comboBoxG1AppNo.Items.Clear();
                
                foreach (DataRow row in dSet.Tables[0].Rows)
                {
                    comboBoxG1AppNo.Items.Add(row.ItemArray[0].ToString());
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }
        private void btnEditUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Md_Settings();
                OleDbCommand oleCom = new OleDbCommand();
                oleCom.CommandText = "update 1StuDetail set NAME='" + textBoxName.Text + "',GENDER='" +
                    strGender + "',DOB='" + dateTimePickerDOB.Text + "',ADDRESS='" + textBoxAddress.Text + "',PHONENO='" + textBoxPhNo.Text + "',OCCUPATION='" +
                    textBoxOccup.Text + "',MARKS=" + textBoxMarks.Text + ",COURSE='" + comboBoxCourse.Text + "',DOJ='" + dateTimePickerDOJ.Text + "' where APPNO=" + comboBoxG1AppNo.Text;
                oleCom.Connection = con;
                oleCom.ExecuteNonQuery();
                //MessageBox.Show(oleCom.CommandText);
                MessageBox.Show("Record Updated", "Course Registration");
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

                Md_Settings();
                OleDbCommand oleCom = new OleDbCommand();
                oleCom.CommandText = "delete from 1StuDetail where APPNO=" + comboBoxG1AppNo.Text;
                oleCom.Connection = con;
                oleCom.ExecuteNonQuery();
                //MessageBox.Show(oleCom.CommandText);
                MessageBox.Show("Record Deleted", "Course Registraion");
                Md_AppNoCombo();
                Md_ClearAll();
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
        }

        private void paymentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Md_PayMent();
            comboBoxAppNo.Focus();
        }
        private void Md_PayMent()
        {
            try
            {
                Md_Settings();
                comboBoxAppNo.Items.Clear();
                foreach (DataRow row in dSet.Tables[0].Rows)
                {
                    comboBoxAppNo.Items.Add(row.ItemArray[0].ToString());
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error: " + exp, "Course Registration");
            }
            groupBoxNewRegist.Visible = false;
            groupBoxCashMgt.Visible = true;
            textBoxG2Name.Text = "";
            textBoxG2Course.Text = "";
            textBoxTotalAmount.Text = "";
            textBoxFee.Text = "";
            textBoxFeePaid.Text = "";
            textBoxDiscount.Text = "";
            textBoxBalance.Text = "";
            textBoxPayCount.Text = "";
            textBoxNewPayment.Text = "";
        }

        private void comboBoxAppNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                Md_Settings();
                DataRow trow;
                //DataColumn column = new DataColumn();
                //column = dSet.Tables["StuDetail"].Columns[1];
                dSet.Tables[0].PrimaryKey = (new DataColumn[] { dSet.Tables[0].Columns[0] });
                trow = dSet.Tables[0].Rows.Find(comboBoxAppNo.Text);
                textBoxG2Name.Text = trow["NAME"].ToString();
                textBoxG2Course.Text = trow["COURSE"].ToString();
                //MessageBox.Show(trow["Name"].ToString());
                Md_SettingsPayMent();
                dSetPayMent.Tables[0].PrimaryKey = (new DataColumn[] { dSetPayMent.Tables[0].Columns[0] });
                DataRow prow;
                prow = dSetPayMent.Tables[0].Rows.Find(comboBoxAppNo.Text);
                textBoxTotalAmount.Text = prow["TOTALAMOUNT"].ToString();
                textBoxFee.Text = prow["FEES"].ToString();
                textBoxFeePaid.Text = prow["FEESPAID"].ToString();
                textBoxBalance.Text = prow["BALANCE"].ToString();
                textBoxPayCount.Text = prow["COUNTPAY"].ToString();
                textBoxDiscount.Text = prow["DISCOUNT"].ToString();
                textBoxNewPayment.Text = "";
                if (long.Parse(prow["BALANCE"].ToString()) == 0)
                    textBoxNewPayment.Enabled = false;
                else
                    textBoxNewPayment.Enabled = true;
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error" + exp, "Course Registration");
            }
        }

        private void comboBoxG1AppNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Md_Settings();
            DataRow trow;
            //DataColumn column = new DataColumn();
            //column = dSet.Tables["StuDetail"].Columns[1];
            dSet.Tables[0].PrimaryKey = (new DataColumn[] { dSet.Tables[0].Columns[0] });
            trow = dSet.Tables[0].Rows.Find(comboBoxG1AppNo.Text);
            textBoxName.Text = trow["NAME"].ToString();
            if (trow["GENDER"].ToString() == "Male")
                radioButtonMale.Checked = true;
            else
                radioButtonFemale.Checked = true;
            dateTimePickerDOB.Text = trow["DOB"].ToString();
            textBoxAddress.Text = trow["ADDRESS"].ToString();
            textBoxPhNo.Text = trow["PHONENO"].ToString();
            textBoxOccup.Text = trow["OCCUPATION"].ToString();
            textBoxMarks.Text = trow["MARKS"].ToString();
            comboBoxCourse.Text = trow["COURSE"].ToString();
            dateTimePickerDOJ.Text = trow["DOJ"].ToString();
            //textBoxCourse.Text = trow["COURSE"].ToString();

            //MessageBox.Show(trow["Name"].ToString());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Md_Cancel();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Dispose(bool disposing)
            this.Close();
        }

        private void btnFeeUpdate_Click(object sender, EventArgs e)
        {
            //Md_SettingsPayMent();
            Md_UpdateFees();
        }
        private void Md_UpdateFees()
        {
            if (textBoxNewPayment.Text != "")
            {
                try
                {
                    int intCount;
                    long longFee, longFeesPaid, longBalance;
                    DataRow Brow;
                    Brow = dSetPayMent.Tables[0].Rows.Find(comboBoxAppNo.Text);

                    intCount = int.Parse(Brow["COUNTPAY"].ToString());
                    longFee = long.Parse(Brow["FEES"].ToString());
                    longFeesPaid = long.Parse(Brow["FEESPAID"].ToString());
                    longBalance = long.Parse(Brow["BALANCE"].ToString());
                    if ((longBalance >= long.Parse(textBoxNewPayment.Text)))
                    {
                        if (longBalance != 0 && longBalance > 1)
                        {
                            intCount += 1;
                            longFeesPaid = longFeesPaid + int.Parse(textBoxNewPayment.Text);
                            longBalance = longFee - longFeesPaid;

                            OleDbCommand comd = new OleDbCommand();
                            string s = comboBoxAppNo.Text;
                            comd.CommandText = "update 2PayMent set FEES=" + longFee +
                                ",FEESPAID=" + longFeesPaid + ",BALANCE=" + longBalance +
                                ",COUNTPAY=" + intCount + " where APPNO=" + comboBoxAppNo.Text;
                            comd.Connection = con;
                            comd.ExecuteNonQuery();
                            //MessageBox.Show(comd.CommandText);
                            MessageBox.Show("Fees Updated", "Course Registration");
                            Md_PayMent();
                            comboBoxAppNo.Text = s;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Amount is out of limit", "Course Registration");
                        textBoxNewPayment.Text = "";
                        textBoxNewPayment.Focus();
                    }

                }
                catch (Exception exp)
                {
                    MessageBox.Show("ERROR:" + exp, "Course Registration");
                    textBoxNewPayment.Focus();
                }
            }
        }
        private void addCourseToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void btnAddCourse_Click(object sender, EventArgs e)
        {
            if (textBoxG3Couse.Text != "" && textBoxCourseFees.Text != "")
            {
                try
                {

                    Md_SettingsCourseNew();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = con;

                    command.CommandText = "insert into CourseNew values('" +
                        textBoxG3Couse.Text + "'," + textBoxCourseFees.Text + ")";
                    command.ExecuteNonQuery();
                    Md_AddCourse();
                }
                catch (Exception exp)
                {
                    MessageBox.Show("ERROR:" + exp, "Course Registration");
                }
            }
        }

        private void btnRemoveCourse_Click(object sender, EventArgs e)
        {
            try
            {

                Md_SettingsCourseNew();
                OleDbCommand command = new OleDbCommand();
                command.Connection = con;

                command.CommandText = "delete from CourseNew where COURSE='" + comboBox1.Text + "'";
                command.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                MessageBox.Show("ERROR:" + exp, "Course Registration");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                Md_SettingsCourseNew();
                /**/
                dSetX.Tables[0].PrimaryKey = (new DataColumn[] { dSetX.Tables[0].Columns[0] });
                DataRow trow;
                trow = dSetX.Tables[0].Rows.Find(comboBox1.Text);
                textBoxCourseFees.Text = trow["TOTALFEES"].ToString();
            }
            catch (Exception exp)
            {
                MessageBox.Show("ERROR:" + exp, "Course Registration");
            }
        }

        private void deleteCourseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Md_SettingsCourseNew();
            Md_RemoveCourse();
        }
        private void Md_Cancel()
        {
            groupBox3.Visible = false;
            groupBoxCashMgt.Visible = false;
            groupBoxNewRegist.Visible = false;
        }
        private void Md_RemoveCourse()
        {
            comboBox1.Items.Clear();
            foreach (DataRow row in dSetX.Tables[0].Rows)
            {
                comboBox1.Items.Add(row.ItemArray[0].ToString());
            }
            groupBox3.Visible = true;
            btnRemoveCourse.Visible = true;
            groupBoxCashMgt.Visible = false;
            groupBoxNewRegist.Visible = false;
            btnAddCourse.Visible = false;
            comboBox1.Visible = true;
            textBoxG3Couse.Visible = false;
            label26.Text = "Removing Course";
            textBoxG3Couse.Text = "";
            textBoxCourseFees.Text = "";
        }
        private void Md_AddCourse()
        {
            groupBox3.Visible = true;
            btnRemoveCourse.Visible = false;
            groupBoxCashMgt.Visible = false;
            groupBoxNewRegist.Visible = false;
            btnAddCourse.Visible = true;
            comboBox1.Visible = false;
            textBoxG3Couse.Visible = true;
            label26.Text = "New Course Editor";
            textBoxG3Couse.Text = "";
            textBoxCourseFees.Text = "";
        }
        private void addCourseToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Md_AddCourse();
        }

        private void mainPageToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Md_Cancel();
        }

        private void btnG2Cancel_Click(object sender, EventArgs e)
        {
            Md_Cancel();
        }

        private void btnG3Cancel_Click(object sender, EventArgs e)
        {
            Md_Cancel();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Md_PayMent();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Md_ClearAll();
            groupBoxNewRegist.Visible = true;
            groupBoxCashMgt.Visible = false;
            textBoxAppNo.Visible = true;
            btnDelete.Visible = false;
            btnRegister.Visible = true;
            btnEditUpdate.Visible = false;
            comboBoxG1AppNo.Visible = false;
            textBoxAppNo.Focus();
            label10.Text = "Registering New Records";
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            Md_PayMent();
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout frm2 = new frmAbout();
            frm2.ShowDialog();
        }

        void textBoxNewPayment_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar.GetHashCode().ToString() == "851981")
                Md_UpdateFees();
            //SendKeys.Send(Keys.Enter);
        }
        
    }
}
