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

namespace WindowsFormsApp1
{
    public partial class EmployeeForm : Form
    {

        protected readonly ZkContext context;
        protected Employees employee;


        public EmployeeForm(ZkContext context)
        {

            this.context = context;

            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {

            if (employee == null)
            {
                await CreateEmployee();
            }
            else
            {
                await UpdateEmployee();
            }

        }


        private async Task CreateEmployee()
        {
            Employees employee = Employees.Create(textid.Text,
                textuser.Text,
                textsername.Text
                );

            await context.Employees.AddAsync(employee);
            await context.SaveChangesAsync();
            DialogResult = DialogResult.OK;
        }

        private async Task UpdateEmployee()
        {
            employee.Update(textid.Text,
                textuser.Text,
                textsername.Text
                );

            await context.SaveChangesAsync();
            DialogResult = DialogResult.OK;
   
        }


        public Employees Employee
        {
            get { return employee; }
            set
            {
                employee = value;

                textid.Text = employee.id;
                textuser.Text = employee.name;
                textsername.Text = employee.sername;
               
                textid.Enabled = false;
            }
        }


        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
