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

        DbGymEntities2 database = new DbGymEntities2();


        public FrmForooshGah()
        {
            InitializeComponent();
        }

        private void searching()
        {
            var query = (from kala in database.TblForooshgahs where kala.KalaName.Contains(TxtSearchName.Text) select kala);
            GrdList.DataSource = query.ToList();
        }




        private void FrmForooshGah_Load(object sender, EventArgs e)
        {

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



        }

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

        private void BtnEdit_Click(object sender, EventArgs e)
        {

            if (GrdList.Rows.Count == 0)
            {
                FrmMessage fm = new FrmMessage();
                fm.Text = "ویرایش کالا";
                fm.LblText.Text = "کالایی برای ویرایش وجود ندارد";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }


            FrmEditKala fe = new FrmEditKala();
            fe.TxtName.Text = GrdList.CurrentRow.Cells["KalaName"].Value.ToString();
            fe.TxtPrice.Text = GrdList.CurrentRow.Cells["Price"].Value.ToString();
            fe.TxtTedad.Text = GrdList.CurrentRow.Cells["Mojudi"].Value.ToString();
            fe.code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
            fe.ShowDialog();


            database = fe.database;
            GrdList.DataSource = database.TblForooshgahs.ToList();






        }

        private void GrdList_DoubleClick(object sender, EventArgs e)
        {

            BtnEdit_Click(null, null);


        }

        private void BtnDelete_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;

            }
            
            
            
            
            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 kala in database.TblForooshgahs where kala.ID == Code select kala).SingleOrDefault();
                database.TblForooshgahs.Remove(query);
                database.SaveChanges();

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

           }

        }


        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }






        private void BtnPrint_Click(object sender, EventArgs e)
        {
            var query = (from kala in database.TblForooshgahs where kala.KalaName.Contains(TxtSearchName.Text) select kala);


            DataTable dt = new DataTable();
            dt = ConvertToDataTable(query.ToList());

            stiReport1.RegData(dt);
            stiReport1.Load("stiReportKala.mrt");
            stiReport1.Show();


        }
    }
}
