using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace TelBook
{
    public partial class fmsettings : Form
    {
        public fmsettings()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DATA.ACCDB;JET OLEDB:DATABASE PASSWORD=●●●●●●●●●●●●;");

        private bool IsFull()
        {
            foreach (Control ctl in tabPage1.Controls)
            {
                if (ctl is TextBox)
                {
                    TextBox tmp;
                    tmp = (TextBox)ctl;
                    if (tmp.Text == "") return false;
                }

            } return true;
        }

        private void fmsettings_Load(object sender, EventArgs e)
        {
            try
            {
                string d;
                OleDbDataAdapter adap;
                DataSet ds = new DataSet();
                d = "select * from users";
                adap = new OleDbDataAdapter(d, con);
                adap.Fill(ds, "users");
                dgv.DataSource = ds.Tables["users"];
                dgv.Columns["id"].HeaderText = "کد";
                dgv.Columns["user1"].HeaderText = "کاربر";
                dgv.Columns["usern1"].HeaderText = "نام کاربری";
                dgv.Columns["pass1"].HeaderText = "کلمه ی عبور ";
                dgv.Columns["id"].Width = 60;
                dgv.Columns["user1"].Width = 110;
                dgv.Columns["usern1"].Width = 110;
                dgv.Columns["pass1"].Width = 110;
                txuser.Text = dgv.CurrentRow.Cells[1].Value.ToString();
                txusername.Text = dgv.CurrentRow.Cells[2].Value.ToString();
                txpass.Text = dgv.CurrentRow.Cells[3].Value.ToString();
                txrepeat.Text = txpass.Text;
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txpass.Text != txrepeat.Text)
                {
                    MessageBox.Show("رمز عبور با تکرارش مطابقت ندارد", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txpass.Focus();
                }
                else
                {
                    con.Open();
                    string updateq = "update users set user1='" + txuser.Text + "' , usern1='" + txusername.Text + "' , pass1='" + txpass.Text + "'where id=" + dgv.CurrentRow.Cells[0].Value.ToString() + "";
                    OleDbCommand uq = new OleDbCommand(updateq, con);
                    uq.ExecuteNonQuery();
                    MessageBox.Show("عملیات ویرایش انجام شد\nبرای اعمال تغیرات نرم افزار مجدد راه اندازی میشود ", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Information,MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign);
                    Application.Restart();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txuser_TextChanged(object sender, EventArgs e)
        {

        }

        private void txusername_TextChanged(object sender, EventArgs e)
        {

        }

        private void txpass_TextChanged(object sender, EventArgs e)
        {

        }

        private void txrepeat_TextChanged(object sender, EventArgs e)
        {

        }

        private void fmsettings_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void tabControl1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
        }

        private void fmsettings_KeyUp(object sender, KeyEventArgs e)
        {
            button1.Enabled = IsFull();

        }


    }
}
