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

        DbGymEntities2 database = new DbGymEntities2();
        public Boolean IsSelection = false;
        public string OzvName = "";
        public int OzvCode = 0;






        public FrmUserManagement()
        {
            InitializeComponent();
        }



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


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







        private void FrmUserManagement_Load(object sender, EventArgs e)
        {
            GrdList.DataSource = database.TblUsers.ToList();
        }

        private void TxtSearchCode_TextChanged(object sender, EventArgs e)
        {
            Searching();
        }

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

        private void BtnDetails_Click(object sender, EventArgs e)
        {

            try
            {
                FrmDetails fd = new FrmDetails();
                fd.LblCode.Text = GrdList.CurrentRow.Cells["ID"].Value.ToString();
                fd.LblName.Text = GrdList.CurrentRow.Cells["UserName"].Value.ToString();
                fd.LblTT.Text = GrdList.CurrentRow.Cells["BirthDate"].Value.ToString();
                fd.LblTE.Text = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString();
                fd.LblTS.Text = GrdList.CurrentRow.Cells["OzviatDate"].Value.ToString();
                fd.LblKomod.Text = GrdList.CurrentRow.Cells["Komod"].Value.ToString();

                fd.PicUserImage.Load(Application.StartupPath + "\\Pics\\" + GrdList.CurrentRow.Cells[0].Value.ToString() + ".jpg");

                fd.ShowDialog();
            }
            catch
            {
                MessageBox.Show("در نمایش مشخصات کاربر خطا وجود دارد");
            }






        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            string sindex = "";


            FrmMessage fm = new FrmMessage();

            if (GrdList.RowCount == 0)
            {
                
                fm.Text = "ویرایش عضو";
                fm.LblText.Text = "رکوردی برای ویرایش وجود ندارد";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();

                return;

            }


            FrmUserEdit fue = new FrmUserEdit();


            int i;
            for (i = 1; i <= 31; i++)
            {
                if (i < 10)
                    fue.CmbdayBirth.Items.Add("0" + i.ToString());
                else
                    fue.CmbdayBirth.Items.Add(i.ToString());
            }

            for (i = 1; i <= 12; i++)
            {
                if (i < 10)
                    fue.CmbmonthBirth.Items.Add("0" + i.ToString());
                else
                    fue.CmbmonthBirth.Items.Add(i.ToString());
            }

            for (i = 1300; i <= 1400; i++)
            {
                fue.CmbyearBirth.Items.Add(i.ToString());
            }



            for (i = 1; i <= 31; i++)
            {
                if (i < 10)
                    fue.CmbdaySabt.Items.Add("0" + i.ToString());
                else
                    fue.CmbdaySabt.Items.Add(i.ToString());
            }

            for (i = 1; i <= 12; i++)
            {
                if (i < 10)
                    fue.CmbMonthSabt.Items.Add("0" + i.ToString());
                else
                    fue.CmbMonthSabt.Items.Add(i.ToString());
            }

            for (i = 1300; i <= 1400; i++)
            {
                fue.CmbYearSabt.Items.Add(i.ToString());
            }



            for (i = 1; i <= 31; i++)
            {

                if (i < 10)
                    fue.CmbdayEtebar.Items.Add("0" + i.ToString());
                else
                    fue.CmbdayEtebar.Items.Add(i.ToString());
            }

            for (i = 1; i <= 12; i++)
            {
                if (i < 10)
                    fue.CmbMonthEtebar.Items.Add("0" + i.ToString());
                else
                    fue.CmbMonthEtebar.Items.Add(i.ToString());
            }

            for (i = 1300; i <= 1400; i++)
            {
                fue.CmbYearEtebar.Items.Add(i.ToString());
            }

            fue.CmbdayBirth.SelectedIndex = 0;
            fue.CmbdayEtebar.SelectedIndex = 0;
            fue.CmbdaySabt.SelectedIndex = 0;
            fue.CmbmonthBirth.SelectedIndex = 0;
            fue.CmbMonthEtebar.SelectedIndex = 0;
            fue.CmbMonthSabt.SelectedIndex = 0;
            fue.CmbyearBirth.SelectedIndex = 0; ;
            fue.CmbYearEtebar.SelectedIndex = 0;
            fue.CmbYearSabt.SelectedIndex = 0;



            fue.Code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());

            //----------------------------

            fue.TxtName.Text = GrdList.CurrentRow.Cells["UserName"].Value.ToString();

            sindex = GrdList.CurrentRow.Cells["BirthDate"].Value.ToString().Substring(0, 4);
            fue.CmbyearBirth.SelectedIndex = Convert.ToInt32(sindex) - 1300;

            sindex = GrdList.CurrentRow.Cells["BirthDate"].Value.ToString().Substring(5, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbmonthBirth.SelectedIndex = Convert.ToInt32(sindex) - 1;

            sindex = GrdList.CurrentRow.Cells["BirthDate"].Value.ToString().Substring(GrdList.CurrentRow.Cells["BirthDate"].Value.ToString().Length - 2, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbdayBirth.SelectedIndex = Convert.ToInt32(sindex) -1 ;

            //---------------------------------

            sindex = GrdList.CurrentRow.Cells["OzviatDate"].Value.ToString().Substring(0, 4);
            fue.CmbYearSabt.SelectedIndex = Convert.ToInt32(sindex) - 1300;

            sindex = GrdList.CurrentRow.Cells["OzviatDate"].Value.ToString().Substring(5, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbMonthSabt.SelectedIndex = Convert.ToInt32(sindex) - 1;

            sindex = GrdList.CurrentRow.Cells["OzviatDate"].Value.ToString().Substring(GrdList.CurrentRow.Cells["OzviatDate"].Value.ToString().Length - 2, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbdaySabt.SelectedIndex = Convert.ToInt32(sindex) - 1;

            //---------------------------------

            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(0, 4);
            fue.CmbYearEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1300;

            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(5, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbMonthEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1;

            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Length - 2, 2);
            sindex = sindex.Replace("/", "");
            fue.CmbdayEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1;

            //------------------------------------


            


            fue.PicUserImage.Load(Application.StartupPath + "\\Pics\\" + GrdList.CurrentRow.Cells[0].Value.ToString() + ".jpg");



            fue.ShowDialog();


            database = fue.database;


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


            

        }

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

                try
                {
                    System.IO.File.Delete(Application.StartupPath + "\\Pics\\" + Code + ".jpg");
                }
                catch
                {

                }

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


                    


            }





        }

        private void BtnShahrie_Click(object sender, EventArgs e)
        {
            string sindex = "";
            FrmTamdid ft = new FrmTamdid();



            int i;

            for (i = 1; i <= 31; i++)
            {
                ft.CmbdayEtebar.Items.Add(i.ToString());
            }

            for (i = 1; i <= 12; i++)
            {
                ft.CmbMonthEtebar.Items.Add(i.ToString());
            }

            for (i = 1300; i <= 1400; i++)
            {
                ft.CmbYearEtebar.Items.Add(i.ToString());
            }

            ft.CmbdayEtebar.SelectedIndex = 0;
            ft.CmbMonthEtebar.SelectedIndex = 0;
            ft.CmbYearEtebar.SelectedIndex = 0;
           


            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(0, 4);
            ft.CmbYearEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1300;

            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(5, 2);
            sindex = sindex.Replace("/", "");
            ft.CmbMonthEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1;

            sindex = GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Substring(GrdList.CurrentRow.Cells["EtebarDate"].Value.ToString().Length - 2, 2);
            sindex = sindex.Replace("/", "");
            ft.CmbdayEtebar.SelectedIndex = Convert.ToInt32(sindex) - 1;


            ft.Code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
            ft.CName = GrdList.CurrentRow.Cells["UserName"].Value.ToString();

            ft.ShowDialog();

            database = ft.database;


            GrdList.DataSource = database.TblUsers.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 BtnPrintCard_Click(object sender, EventArgs e)
        {

            int Code = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
            var query = (from ozvha in database.TblUsers where ozvha.ID == Code select ozvha);


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

            DataTable dtp = new DataTable();
            dtp.Columns.Add("Pic");
            dtp.Columns.Add("BarCode");
            dtp.Rows.Add();

            dtp.Rows[0][0] = Application.StartupPath + "\\Pics\\" + GrdList.CurrentRow.Cells[0].Value.ToString() + ".jpg";
            dtp.Rows[0][1] = Application.StartupPath + "\\Pics\\" + GrdList.CurrentRow.Cells[0].Value.ToString() + "_Barcode.jpg";


            DataSet ds = new DataSet();
            ds.Merge(dtm);
            ds.Tables[0].TableName = "dtm";

            ds.Merge(dtp);
            ds.Tables[1].TableName = "dtp";





            stiReport1.RegData(ds);
            stiReport1.Load("stiReport1.mrt");
            stiReport1.Show();



        }

        private void TxtSearchCode_TextChanged_1(object sender, EventArgs e)
        {

            Class1.ChangeNum(TxtSearchCode);
            Searching();

        }

        private void TxtSearchCode_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 GrdList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GrdList_DoubleClick(object sender, EventArgs e)
        {
            if (IsSelection == true)
            {
                OzvName = GrdList.CurrentRow.Cells["UserName"].Value.ToString();
                OzvCode = Convert.ToInt32(GrdList.CurrentRow.Cells["ID"].Value.ToString());
                this.Close();
            }
        }
    }
}
