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 FrmSabteNam : Form
    {
        DbGymEntities2 database = new DbGymEntities2();


        public FrmSabteNam()
        {
            InitializeComponent();
        }

        private void FrmSabteNam_Load(object sender, EventArgs e)
        {
            int i;
            for (i = 1 ; i <= 31 ; i++)
            {
                if (i<10)
                    CmbdayBirth.Items.Add("0" + i.ToString());
                else
                    CmbdayBirth.Items.Add(i.ToString());
            }
           
            for (i = 1 ; i <= 12 ; i++)
            {
                if (i < 10)
                    CmbmonthBirth.Items.Add("0" + i.ToString());
                else
                    CmbmonthBirth.Items.Add(i.ToString());
            }

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



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

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

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



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

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

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

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









        }

        private void PicAdd_Click(object sender, EventArgs e)
        {

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

            int daynum = pc.GetDayOfMonth(today);
            CmbdaySabt.SelectedIndex = daynum - 1;


            int monthnum = pc.GetMonth(today);
            CmbMonthSabt.SelectedIndex = monthnum - 1;

            int yearnum = pc.GetYear(today);
            CmbYearSabt.SelectedIndex = yearnum - 1300;












            PicUserImage.Load(Application.StartupPath + "\\Pics\\NoPic.jpg");
            groupPanel3.Enabled = true;
            groupPanel1.Enabled = true;
            PicAdd.Visible = false;
            PicAddN.Visible = true;
            PicSave.Visible = true;
            PicCancel.Visible = true;
            PicSaveN.Visible = false;
            PicCancelN.Visible = false;
            

            TxtName.Text = "";
            TxtName.Focus();



        }

        private void PicSave_Click(object sender, EventArgs e)
        {

            if (TxtName.Text == "")
            {
                MessageBox.Show("لطفا نام و نام خانوادگی را وارد کنید");
            }
            else
            {
                TblUser tbl = new TblUser();
                tbl.Name = TxtName.Text;
                tbl.BirthDate = CmbyearBirth.Text + "/" + CmbmonthBirth.Text + "/" + CmbdayBirth.Text;
                tbl.OzviatDate = CmbYearSabt.Text + "/" + CmbMonthSabt.Text + "/" + CmbdaySabt.Text;
                tbl.EtebarDate = CmbYearEtebar.Text + "/" + CmbMonthEtebar.Text + "/" + CmbdayEtebar.Text;
                tbl.Komod = "ندارد";

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





                PicUserImage.Image.Save(Application.StartupPath + "\\Pics\\" + tbl.ID.ToString() + ".jpg");

                barCodeCtrl1.BarCode = tbl.ID.ToString();
                barCodeCtrl1.SaveImage(Application.StartupPath + "\\Pics\\" + tbl.ID.ToString() + "_Barcode.jpg");








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

                
                groupPanel3.Enabled = false;
                groupPanel1.Enabled = false;
                PicAdd.Visible = true;
                PicAddN.Visible = false;
                PicSave.Visible = false;
                PicCancel.Visible = false;
                PicSaveN.Visible = true;
                PicCancelN.Visible = true;
                






            }







        }

        private void PicCancel_Click(object sender, EventArgs e)
        {
            
            groupPanel3.Enabled = false;
            groupPanel1.Enabled = false;
            PicAdd.Visible = true;
            PicAddN.Visible = false;
            PicSave.Visible = false;
            PicCancel.Visible = false;
            PicSaveN.Visible = true;
            PicCancelN.Visible = true;
        }

        private void CmbdaySabt_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEtebar();



        }


        private void SetEtebar()
        {
            CmbdayEtebar.SelectedIndex = CmbdaySabt.SelectedIndex;

            if (CmbMonthSabt.SelectedIndex == 11)
            {
                CmbMonthEtebar.SelectedIndex = 0;
                CmbYearEtebar.SelectedIndex = CmbYearSabt.SelectedIndex + 1;
            }
            else
            {
                CmbMonthEtebar.SelectedIndex = CmbMonthSabt.SelectedIndex + 1;
                CmbYearEtebar.SelectedIndex = CmbYearSabt.SelectedIndex;
            }
            

        }


        private void CmbMonthSabt_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEtebar();
        }

        private void CmbYearSabt_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEtebar();
        }

        private void PicBrowse_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "(*.*)|*.*";
                ofd.Title = "تصویر را انتخاب کنید";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    PicUserImage.Load(ofd.FileName);
                }
            }
            catch
            {
                MessageBox.Show("فایل غیر مجاز است");
            }



        }

        private void PicAddN_Click(object sender, EventArgs e)
        {

        }

        private void PicSaveN_Click(object sender, EventArgs e)
        {

        }

        private void PicCancelN_Click(object sender, EventArgs e)
        {

        }

    }
}
