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 GymSystem
{
    public partial class FrmAddKomod : Form
    {
        public DbGymEntities2 database = new DbGymEntities2();


        public FrmAddKomod()
        {
            InitializeComponent();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            if (TxtNum.Text != "")
            {

                var query = (from komod in database.TblKomods where komod.KomodNum == TxtNum.Text select komod);
                
                if (query.Count() == 0)
                {
                    TblKomod tbl = new TblKomod();
                    tbl.KomodNum = TxtNum.Text;
                    tbl.State = "خالی";
                    tbl.Customer = "بدون مشتری";
                    tbl.CustomerID = "بدون مشتری";

                    database.TblKomods.Add(tbl);
                    database.SaveChanges();


                    lblpm.Text = "کمد " + TxtNum.Text + " با موفقیت ثبت گردید";
                    lblpm.ForeColor = Color.Green;
                    lblpm.Visible = true;

                    TxtNum.Text = "";
                    TxtNum.Focus();
                }
                else
                {
                    lblpm.Text = "شماره کمد " + TxtNum.Text + " قبلا ثبت شده است";
                    lblpm.ForeColor = Color.Red;
                    lblpm.Visible = true;
                    TxtNum.Text = "";
                    TxtNum.Focus();
                }

                

            }
            else
            {
                lblpm.Text = "شماره کمد را وارد نکرده اید";
                lblpm.ForeColor = Color.Red;
                lblpm.Visible = true;
                TxtNum.Text = "";
                TxtNum.Focus();
            }




        }

        private void TxtNum_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtNum);
        }

        private void TxtNum_KeyPress(object sender, KeyPressEventArgs e)
        {
           
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                BtnOk_Click(null, null);
            }


            if (Convert.ToInt32(e.KeyChar) >= 48 && Convert.ToInt32(e.KeyChar) <= 57 || Convert.ToInt32(e.KeyChar) >= 1776 && Convert.ToInt32(e.KeyChar) <= 1785 || Convert.ToInt32(e.KeyChar) == 8 || Convert.ToInt32(e.KeyChar) == 13)
            {

            }
            else
            {
                e.KeyChar = Convert.ToChar(Keys.None);
            }
        }

        private void FrmAddKomod_Load(object sender, EventArgs e)
        {

        }
    }
}
