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;

namespace Amozeshgah
{
    public partial class User : DevComponents.DotNetBar.Office2007Form
    {
        public User()
        {
            InitializeComponent();
        }

        private void User_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'amozeshgahDataSet1.Users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.amozeshgahDataSet1.Users);
            // TODO: This line of code loads data into the 'amozeshgahDataSet1.User' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.amozeshgahDataSet1.Users);
            btnUpdate.Text = "ویرایش";
            txtUser_name.Text = "";
            txtpass.Text = "";
            txtName.Text = "";
            txtFamily.Text = "";
            txtID.Text = "";
        }

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

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.CurrentRow == null)
                return;
            DialogResult dr;
           int row = dataGridViewX1.CurrentRow.Index;
           dr = MessageBox.Show("آیا می خواهید اطلاعات کاربری : " + dataGridViewX1[1, row].Value.ToString() + " " + dataGridViewX1[2, row].Value.ToString() + "  را حذف کنید ! ", "مدیریت آموزشگاه", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
                return;
            string val = dataGridViewX1[0, row].Value.ToString();
            C_User user = new C_User();
            user.ID = Int32.Parse(val);
            user.Delete();
            User_Load(null, null);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.CurrentRow == null)
                return;
            if (btnUpdate.Text == "ویرایش")
            {
                int row = dataGridViewX1.CurrentRow.Index;
                string val = dataGridViewX1[0, row].Value.ToString();
                //
                C_User user = new C_User();
                user.ID = Int32.Parse(val);
                user.GetOne();
                this.txtName.Text = user.name;
                this.txtFamily.Text = user.family;
                this.txtpass.Text = user.pass;
                this.txtUser_name.Text = user.User_name;
                this.txtID.Text = user.ID.ToString();
                btnUpdate.Text = "بروزرسانی";

            }
            else
            {
                if (txtUser_name.Text == "" || txtpass.Text == "" || txtName.Text == "" || txtFamily.Text == "")
                {
                    MessageBox.Show("اطلاعات وارد شده کامل نیست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                C_User user = new C_User();
                user.ID = Int32.Parse(txtID.Text);
                user.name = txtName.Text;
                user.family = txtFamily.Text;
                user.User_name = txtUser_name.Text;
                user.pass = txtpass.Text;
                user.Update();
                MessageBox.Show("مشخصات کاربری : "+ user.name +  " " + user.family + " با موفقیت ویرایش شد" , "مدیریت آموزشگاه",MessageBoxButtons.OK, MessageBoxIcon.Information);
                User_Load(null, null);
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (txtUser_name.Text == "" || txtpass.Text == "" || txtName.Text == "" || txtFamily.Text == "")
            {
                MessageBox.Show("اطلاعات وارد شده کامل نیست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            usersTableAdapter.Insert(txtName.Text, txtFamily.Text, txtUser_name.Text, txtpass.Text);
            User_Load(null, null);
        }
    }
}

