using ETSZemun.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ETSZemun
{
    public partial class frmStudent : Form
    {
        BindingSource _bsStudenti;
        public frmStudent()
        {
            InitializeComponent();
            _bsStudenti = new BindingSource();
            _bsStudenti.CurrentChanged += _bsStudenti_CurrentChanged;
            lstStudenti.DataSource = _bsStudenti;
        }

        private void _bsStudenti_CurrentChanged(object sender, EventArgs e)
        {
            var s=(Student)_bsStudenti.Current;
            if (s == null)
            {
                return;
            }
            txtIme.Text = s.Ime;
            txtPrezime.Text=s.Prezime;
            txtSmer.Text=s.Smer;
            txtBroj.Text=s.Broj;
            txtGodinaUpisa.Text=s.Godina_Upisa;
        }

        private void ocistiPolja() {
            txtIme.Text = "";
            txtPrezime.Clear();
            txtSmer.Clear();
            txtBroj.Clear();
            txtGodinaUpisa.Clear();
        }

        private void btnNovi_Click(object sender, EventArgs e)
        {
            ocistiPolja();
        }

        private void btnUnesi_Click(object sender, EventArgs e)
        {
            Student s = new Student()
            {
                Ime = txtIme.Text,
                Prezime = txtPrezime.Text,
                Smer = txtSmer.Text,
                Broj = txtBroj.Text,
                Godina_Upisa = txtGodinaUpisa.Text
            };
            _bsStudenti.Add(s);
        }

        private void btnIzmeni_Click(object sender, EventArgs e)
        {
            var s = (Student)_bsStudenti.Current;
            s.Ime= txtIme.Text;
            s.Prezime= txtPrezime.Text;
            s.Smer= txtSmer.Text;
            s.Broj= txtBroj.Text;
            s.Godina_Upisa= txtGodinaUpisa.Text;
            _bsStudenti.ResetBindings(true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var s = (Student)_bsStudenti.Current;
            _bsStudenti.Remove(s);
            _bsStudenti.ResetBindings(true);
        }
    }
}
