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 FrmKomod : Form
    {

        DbGymEntities2 database = new DbGymEntities2();
        string komodstate = "";



        public FrmKomod()
        {
            InitializeComponent();
        }

        private void groupPanel2_Click(object sender, EventArgs e)
        {

        }



        private void searching ()
        {
           
            var query = (from komod in database.TblKomods where komod.KomodNum.Contains(TxtSearchNum.Text) && komod.Customer.Contains(TxtSearchOzv.Text) && komod.State.Contains(komodstate) select komod);
            GrdList.DataSource = query.ToList();


        }




        private void FrmKomod_Load(object sender, EventArgs e)
        {
            CmbSearch.SelectedIndex = 0;


            GrdList.DataSource = database.TblKomods.ToList();



        }

        private void label3_Click(object sender, EventArgs e)
        {
            PicVagozari_Click(null, null);
        }

        private void PicAdd_Click(object sender, EventArgs e)
        {
            FrmAddKomod fak = new FrmAddKomod();
            fak.ShowDialog();

            database = fak.database;
            searching();







        }

        private void LblAdd_Click(object sender, EventArgs e)
        {
            PicAdd_Click(null, null);
        }

        private void CmbSearch_SelectedIndexChanged(object sender, EventArgs e)
        {

            if(CmbSearch.SelectedIndex == 0)
            {
                komodstate = "";
            }
            else if( CmbSearch.SelectedIndex == 1)
            {
                komodstate = "خالی";
            }
            else
            {
                komodstate = "پر";
            }

            searching();


        }

        private void TxtSearchOzv_TextChanged(object sender, EventArgs e)
        {
            searching();
        }

        private void TxtSearchNum_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtSearchNum);
            searching();
        }

        private void TxtSearchNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            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 PicDelete_Click(object sender, EventArgs e)
        {


            FrmMessage fm = new FrmMessage();


            if (GrdList.RowCount == 0)
            {

                fm.Text = "حذف کمد";
                fm.LblText.Text = "کمدی برای حذف وجود ندارد";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }


            if (GrdList.CurrentRow.Cells["State"].Value.ToString() == "پر")
            {
                fm.Text = "حذف کمد";
                fm.LblText.Text = "این کمد خالی نیست و اجازه حذف آن را ندارید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }





            fm.Text = "حذف کمد";
            fm.LblText.Text = "آیا از حذف کمد جاری دارید؟";
            fm.PicYes.Visible = true;
            fm.PicNo.Visible = true;
            fm.PicSoal.Visible = true;

            fm.ShowDialog();


            if (fm.MResult == true)
            {
                int code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
                var query = (from komod in database.TblKomods where komod.ID == code select komod).SingleOrDefault();
                database.TblKomods.Remove(query);
                database.SaveChanges();

                searching();
            }
          


        }

        private void LblDelete_Click(object sender, EventArgs e)
        {
            PicDelete_Click(null, null);
        }

        private void PicVagozari_Click(object sender, EventArgs e)
        {

            FrmMessage fm = new FrmMessage();



            if (GrdList.CurrentRow.Cells["State"].Value.ToString() == "پر")
            {
                
                fm.Text = "واگذاری کمد";
                fm.LblText.Text = "این کمد خالی نیست و اجازه واگذاری آن را ندارید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }


            FrmUserManagement fum = new FrmUserManagement();
            fum.Height = 370;
            fum.Text = "عضو مورد نظر را با دابل کلیک انتخاب نمایید";
            fum.IsSelection = true;
            fum.ShowDialog();

            int code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
            var query = (from komod in database.TblKomods where komod.ID == code select komod).SingleOrDefault();
            query.State = "پر";
            query.Customer = fum.OzvName;
            query.CustomerID = fum.OzvCode.ToString();
            database.SaveChanges();


            var query2 = (from users in database.TblUsers where users.ID == fum.OzvCode select users).SingleOrDefault();
            query2.Komod = GrdList.CurrentRow.Cells["KomodNum"].Value.ToString();
            database.SaveChanges();



            
            fm.Text = "واگذاری کمد";
            fm.LblText.Text = " کمد  " + GrdList.CurrentRow.Cells["KomodNum"].Value.ToString() + " به عضو با نام  " + fum.OzvName + " واگذار گردید " ;
            fm.PicOk.Visible = true;
            fm.PicKhabar.Visible = true;

            fm.ShowDialog();


            searching();

        }

        private void Pictakhlie_Click(object sender, EventArgs e)
        {
            FrmMessage fm = new FrmMessage();


            if (GrdList.CurrentRow.Cells["State"].Value.ToString() == "خالی")
            {

                fm.Text = "خالی کردن کمد";
                fm.LblText.Text = "کمد خالی است";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }



            int usercode = Convert.ToInt32(GrdList.CurrentRow.Cells["CustomerID"].Value.ToString());
            var query2 = (from users in database.TblUsers where users.ID == usercode select users).SingleOrDefault();
            query2.Komod = "ندارد";
            database.SaveChanges();

            int code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
            var query = (from komod in database.TblKomods where komod.ID == code select komod).SingleOrDefault();
            query.State = "خالی";
            query.Customer = "بدون مشتری";
            query.CustomerID = "بدون مشتری";
            database.SaveChanges();

          

            fm.Text = "خالی کردن کمد";
            fm.LblText.Text = "کمد تخلیه شد";
            fm.PicOk.Visible = true;
            fm.PicKhabar.Visible = true;

            fm.ShowDialog();


            searching();




        }

        private void Lbltakhlie_Click(object sender, EventArgs e)
        {
            Pictakhlie_Click(null, null);
        }
    }
}
