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

        public int Code = 0;
        public DbGymEntities2 database = new DbGymEntities2();
        Boolean IsPicChanged = false;




        public FrmUserEdit()
        {
            InitializeComponent();
        }

        private void FrmUserEdit_Load(object sender, EventArgs e)
        {

            



        }

        private void PicCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PicSave_Click(object sender, EventArgs e)
        {

            if (TxtName.Text == "")
            {
                FrmMessage fm = new FrmMessage();
                fm.Text = "ویرایش اعضا";
                fm.LblText.Text = "نام و نام خانوادگی را وارد کنید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();



            }
            else
            {
                var query = (from ozvha in database.TblUsers where ozvha.ID == Code select ozvha).SingleOrDefault();

                query.Name = TxtName.Text;
                query.BirthDate = CmbyearBirth.Text + "/" + CmbmonthBirth.Text + "/" + CmbdayBirth.Text;
                query.OzviatDate = CmbYearSabt.Text + "/" + CmbMonthSabt.Text + "/" + CmbdaySabt.Text;
                query.EtebarDate = CmbYearEtebar.Text + "/" + CmbMonthEtebar.Text + "/" + CmbdayEtebar.Text;

                if (IsPicChanged == true)
                {
                    PicUserImage.Image.Save(Application.StartupPath + "\\Pics\\" + Code.ToString() + ".jpg");
                }




                database.SaveChanges();

                MessageBox.Show("ویرایش با موفقیت انجام شد");
                this.Close();

            }

        }

        private void PicBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "(*.*)|*.*";
                ofd.Title = "تصویر را انتخاب کنید";
                if (ofd.ShowDialog() == DialogResult.OK)
                {

                    if (ofd.FileName == Application.StartupPath + "\\Pics\\" + Code.ToString() + ".jpg")
                    {
                        MessageBox.Show("تصویر تکراری است");
                    }
                    else
                    {
                        PicUserImage.Load(ofd.FileName);
                        IsPicChanged = true;
                    }
                    




                }
            }
            catch
            {
                MessageBox.Show("فایل غیر مجاز است");
            }
        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }
    }
}
