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;
using System.Globalization;



namespace GymSystem
{
    public partial class FrmSell : Form
    {

        DbGymEntities2 database = new DbGymEntities2();

        public FrmSell()
        {
            InitializeComponent();
        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }

        private void FrmSell_Load(object sender, EventArgs e)
        {
            GrdListKala.DataSource = database.TblForooshgahs.ToList();
            GrdListUsers.DataSource = database.TblUsers.ToList();
        }

        private void TxtSearchKalaName_TextChanged(object sender, EventArgs e)
        {
            var query = (from kala in database.TblForooshgahs where kala.KalaName.Contains(TxtSearchKalaName.Text) select kala);
            GrdListKala.DataSource = query.ToList();
        }


        private void SearchingUser()
        {
            if (TxtSearchUserCode.Text != "")
            {
                int code = Convert.ToInt32(TxtSearchUserCode.Text);


                var query = (from ozvvha in database.TblUsers where ozvvha.Name.Contains(TxtSeatrchUserName.Text) && ozvvha.ID == code select ozvvha);
                GrdListUsers.DataSource = query.ToList();
            }
            else
            {
                var query = (from ozvvha in database.TblUsers where ozvvha.Name.Contains(TxtSeatrchUserName.Text) select ozvvha);
                GrdListUsers.DataSource = query.ToList();
            }

        }



        private void TxtSearchUserCode_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtSearchUserCode);
            SearchingUser();
        }

        private void TxtSeatrchUserName_TextChanged(object sender, EventArgs e)
        {
            SearchingUser();
        }

        private void TxtSearchUserCode_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)
            {

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

        private void GrdListKala_SelectionChanged(object sender, EventArgs e)
        {

            try
            {
                LblKalaName.Text = GrdListKala.CurrentRow.Cells["KalaName"].Value.ToString();
                LblPrice.Text = GrdListKala.CurrentRow.Cells["Price"].Value.ToString();

                if (TxtTedad.Text != "")
                {
                    LblTotal.Text = (Convert.ToInt32(LblPrice.Text) * Convert.ToInt32(TxtTedad.Text)).ToString();
                }

                if (GrdListKala.RowCount == 0)
                {
                    LblKalaName.Text = "";
                    LblPrice.Text = "";
                    LblTotal.Text = "";
                }

            }
            catch
            {
                LblKalaName.Text = "";
                LblPrice.Text = "";
                LblTotal.Text = "";
            }




        }

        private void GrdListUsers_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                LblCustomerName.Text = GrdListUsers.CurrentRow.Cells["UserName"].Value.ToString();

                if (GrdListUsers.RowCount == 0)
                {
                    LblCustomerName.Text = "";
                }


            }
            catch
            {
                LblCustomerName.Text = "";
            }
           
        }

        private void TxtTedad_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)
            {

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

        private void TxtTedad_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtTedad);

            if (TxtTedad.Text != "")
            {
                LblTotal.Text = (Convert.ToInt32(LblPrice.Text) * Convert.ToInt32(TxtTedad.Text)).ToString();
            }
            else
            {
                LblTotal.Text = "";
            }
        }

        private void GrdListKala_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GrdListUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void BtnOk_Click(object sender, EventArgs e)
        {

            FrmMessage fm = new FrmMessage();


            if (TxtTedad.Text == "")
            {
                fm.Text = "فروش کالا";
                fm.LblText.Text = "تعداد کالا وارد نشده است";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;
            }

            if (Convert.ToInt32(TxtTedad.Text) > Convert.ToInt32(GrdListKala.CurrentRow.Cells["Mojudi"].Value.ToString()))
            {
                fm.Text = "فروش کالا";
                fm.LblText.Text = "تعداد وارد شده بیشتر از تعداد موجودی است";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;
            }





            TblTarakonesh tbl = new TblTarakonesh();

            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();

            string TDate = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
            tbl.TarakoneshDate = TDate;
            tbl.TarakoneshKind = "خرید از فروشگاه";
            tbl.Price = LblTotal.Text;
            tbl.CustomerID = GrdListUsers.CurrentRow.Cells["UserID"].Value.ToString();
            tbl.CustomerName = LblCustomerName.Text;

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



            int kalacode = Convert.ToInt32(GrdListKala.CurrentRow.Cells[0].Value.ToString());
            var query = (from Kala in database.TblForooshgahs where Kala.ID == kalacode select Kala).SingleOrDefault();

            query.Mojudi = (Convert.ToInt32(GrdListKala.CurrentRow.Cells["Mojudi"].Value.ToString()) - Convert.ToInt32(TxtTedad.Text)).ToString();

            database.SaveChanges();



            GrdListKala.DataSource = database.TblForooshgahs.ToList();


            fm.Text = "فروش کالا";
            fm.LblText.Text = "فروش با موفقیت ثبت شد";
            fm.PicOk.Visible = true;
            fm.PicKhabar.Visible = true;

            fm.ShowDialog();








        }
    }
}
