using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmChangpassword : Form
    {
        public frmChangpassword()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع مربوط به به روز رسانی تعویض کلمه عبور
        /// </summary>
        private void UDf_RefreshChangepass()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblUserInfos
                        orderby row.userID descending
                        select new
                        {
                            row.userID,
                            row.UserName
                        };
            Dgv_RefreshPw.DataSource = Query;
        }
        /// <summary>
        /// تابع مربوط به ویرایش کلمه عبور
        /// </summary>
        private void UDF_UpdateChangepass()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblUserInfo tblUser = db.tblUserInfos.Single(n => n.UserName == (Txt_UserName.Text.Trim()));
            tblUser.UserName = Txt_UserName.Text.Trim();
            if (Txt_Pwa.Text == tblUser.PassWord)
            {
                tblUser.PassWord = Txt_NewPwa.Text.Trim();
                Txt_NewPwa.Text = "";
                Txt_Pwa.Text = "";
                Txt_UserName.Text = "";
                db.SubmitChanges();
                UDf_RefreshChangepass();
            }
            else
            {
                Txt_NewPwa.Text = "";
                Txt_Pwa.Text = "";
                Txt_UserName.Text = "";
                MessageBox.Show("لطفا رمز عبور را صحیح وارد نمایید");
            }
        }
        private void frmChangpassword_Load(object sender, EventArgs e)
        {
            UDf_RefreshChangepass();
            Pnl_Changpassword2.Visible = false;
        }
        private void Btn_CustumersTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                UDF_UpdateChangepass();
                Pnl_Changpassword1.Visible = true;
                Pnl_Changpassword2.Visible = false;
                UDf_RefreshChangepass();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_CustumersLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_Changpassword1.Visible = true;
                Pnl_Changpassword2.Visible = false;
                UDf_RefreshChangepass();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Dgv_RefreshPw_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_RefreshPw.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_RefreshPw.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_UserName.Text = Dgv_RefreshPw.Rows[int.Parse(Dgv_RefreshPw.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                    }
                }
            }
        }
        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            if (Txt_UserName.Text == "" || Txt_Pwa.Text == "" || Txt_NewPwa.Text == "")
            {
                MessageBox.Show("مقادیر فیلدها نمی توانند خالی باشند");
            }
            else
            {
                Pnl_Changpassword1.Visible = false;
                Pnl_Changpassword2.Visible = true;
            }
        }
    }
}