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 FrmAdmin : Form
    {
        DbGymEntities2 database = new DbGymEntities2();
        public FrmAdmin()
        {
            InitializeComponent();
        }

        private void PicAdd_Click(object sender, EventArgs e)
        {
            GrpTexts.Enabled = true;
            GrpChecks.Enabled = true;
            PicAdd.Enabled = false;
            LblAdd.Enabled = false;
            PicDelete.Enabled = false;
            LblDelete.Enabled = false;
            PicSave.Enabled = true;
            LblSave.Enabled = true;
            PicCancel.Enabled = true;
            LblCancel.Enabled = true;
            GrdList.Enabled = false;

            TxtUserName.Text = "";
            TxtUserPass.Text = "";
            TxtUserPass2.Text = "";
            TxtUserPass2.Visible = true;
            label3.Visible = true;

            TxtUserName.Focus();


        }

        private void LblAdd_Click(object sender, EventArgs e)
        {
            PicAdd_Click(null, null);
        }

        private void PicSave_Click(object sender, EventArgs e)
        {

            FrmMessage fm = new FrmMessage();

            if (TxtUserName.Text == "")
            {

                fm.Text = "ثبت کاربر";
                fm.LblText.Text = "نام کاربری را وارد کنید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();
                return;
            }


            if (TxtUserPass.Text == "")
            {

                fm.Text = "ثبت کاربر";
                fm.LblText.Text = " رمز عبور را وارد کنید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();
                return;
            }


            if (TxtUserPass.Text != TxtUserPass2.Text)
            {
                fm.Text = "ثبت کاربر";
                fm.LblText.Text = "رمز عبور و تکذاذش با هم مطابقت ندارند";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();
                return;
            }



            TblLogin tbl = new TblLogin();
            tbl.UserName = TxtUserName.Text;
            tbl.UserPass = TxtUserPass.Text;
            tbl.AdminManagement = Convert.ToBoolean(ChbAdminManagement.Checked);
            tbl.DoBackUp = Convert.ToBoolean(ChbBackUp.Checked);
            tbl.DoRestore = Convert.ToBoolean(ChbRestore.Checked);
            tbl.Foroosh = Convert.ToBoolean(ChbForoosh.Checked);
            tbl.ForooshgahManage = Convert.ToBoolean(ChbForooshgahManage.Checked);
            tbl.komod = Convert.ToBoolean(ChbKomod.Checked);
            tbl.ModiriatAza = Convert.ToBoolean(ChbModiriatAza.Checked);
            tbl.SabteNam = Convert.ToBoolean(ChbSabteNam.Checked);
            tbl.Savabeq = Convert.ToBoolean(ChbSavabeq.Checked);

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



            fm.Text = "ثبت کاربر";
            fm.LblText.Text = "کاربر با موفقیت ثبت گردید";
            fm.PicOk.Visible = true;
            fm.PicKhabar.Visible = true;
            fm.ShowDialog();


            GrpTexts.Enabled = false;
            GrpChecks.Enabled = false;
            PicAdd.Enabled = true;
            LblAdd.Enabled = true;
            PicDelete.Enabled = true;
            LblDelete.Enabled = true;
            PicSave.Enabled = false;
            LblSave.Enabled = false;
            PicCancel.Enabled = false;
            LblCancel.Enabled = false;
            GrdList.Enabled = true;
            TxtUserPass2.Visible = false;
            label3.Visible = false;

            FrmAdmin_Load(null, null);








            







        }


        private void FillInfo()
        {

            try
            {
                TxtUserName.Text = GrdList.CurrentRow.Cells["UserName"].Value.ToString();
                TxtUserPass.Text = GrdList.CurrentRow.Cells["UserPass"].Value.ToString();
                ChbSabteNam.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["SabteNam"].Value);
                ChbAdminManagement.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["AdminManagement"].Value);
                ChbBackUp.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["DoBackUp"].Value);
                ChbForoosh.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["Foroosh"].Value);
                ChbForooshgahManage.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["ForooshgahManage"].Value);
                ChbKomod.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["komod"].Value);
                ChbModiriatAza.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["ModiriatAza"].Value);
                ChbRestore.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["DoRestore"].Value);
                ChbSavabeq.Checked = Convert.ToBoolean(GrdList.CurrentRow.Cells["Savabeq"].Value);
            }
            catch
            {
                TxtUserName.Text ="";
                TxtUserPass.Text = "";
                ChbSabteNam.Checked =false;
                ChbAdminManagement.Checked = false;
                ChbBackUp.Checked = false;
                ChbForoosh.Checked = false;
                ChbForooshgahManage.Checked = false;
                ChbKomod.Checked = false;
                ChbModiriatAza.Checked = false;
                ChbRestore.Checked = false;
                ChbSavabeq.Checked = false;
            }






        }





        private void FrmAdmin_Load(object sender, EventArgs e)
        {

            GrdList.DataSource = database.TblLogins.ToList();
            FillInfo();

        }

        private void GrdList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void PicCancel_Click(object sender, EventArgs e)
        {
            GrpTexts.Enabled = false;
            GrpChecks.Enabled = false;
            PicAdd.Enabled = true;
            LblAdd.Enabled = true;
            PicDelete.Enabled = true;
            LblDelete.Enabled = true;
            PicSave.Enabled = false;
            LblSave.Enabled = false;
            PicCancel.Enabled = false;
            LblCancel.Enabled = false;
            GrdList.Enabled = true;
            TxtUserPass2.Visible = false;
            label3.Visible = false;

            FillInfo();
        }

        private void LblCancel_Click(object sender, EventArgs e)
        {
            PicCancel_Click(null, null);
        }

        private void LblSave_Click(object sender, EventArgs e)
        {
            PicSave_Click(null, null);
        }

        private void GrdList_SelectionChanged(object sender, EventArgs e)
        {
            FillInfo();
        }

        private void PicDelete_Click(object sender, EventArgs e)
        {
            FrmMessage fm = new FrmMessage();

            if (GrdList.RowCount == 1)
            {
                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 users in database.TblLogins where users.ID == code select users).SingleOrDefault();
                database.TblLogins.Remove(query);
                database.SaveChanges();

                fm.Text = "حذف کاربر";
                fm.LblText.Text = "حذف کاربر با موفقیت انجام شد";
                fm.PicYes.Visible = false;
                fm.PicNo.Visible = false;
                fm.PicSoal.Visible = false;
                fm.PicOk.Visible = true;
                fm.PicKhabar.Visible = true;
                fm.ShowDialog();

                FrmAdmin_Load(null, null);

            }


        }
    }
}
