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.SqlClient;

namespace Entity_FrameWork
{
    public partial class User : Form
    {
        public User()
        {
            InitializeComponent();
          this.MinimumSize= new System.Drawing.Size(745, 382);
         
        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        bool op = false;
        private void User_Load(object sender, EventArgs e)
        {
            dataGridViewUser.DataSource = MyModel.Table_User;
            dataGridViewUser.Columns[0].HeaderText = "كد سيستمي";
            dataGridViewUser.Columns[1].HeaderText = "نام كاربري";
            dataGridViewUser.Columns[2].HeaderText = "پسورد";
        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            if (textBox_User.Text == "" && textBox_Password.Text == "")
            {
                MyModel.Sp_Insertuser(dataGridViewUser.CurrentRow.Cells[1].Value.ToString(), dataGridViewUser.CurrentRow.Cells[2].Value.ToString());
                FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
                dataGridViewUser.DataSource = MyModel.Table_User;
            }
            else
            {

                using (Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5())
                {
                    Table_User User = new Table_User()
                     {
                         User = textBox_User.Text,
                         Password = textBox_Password.Text
                     };
                    MyModel.AddToTable_User(User);
                    MyModel.SaveChanges();
                    FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
                    dataGridViewUser.DataSource = MyModel.Table_User;
                }





            }
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            Table_User user = new Table_User();
            int Rowdata = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
            MyModel.Table_User.First(Row=>Row.id==Rowdata);
            user.User = textBox_User.Text;
            user.Password = textBox_Password.Text;
            FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
            MyModel.SaveChanges();
            dataGridViewUser.DataSource = MyModel.Table_User.ToList();


        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            Table_User user = new Table_User();
            int Rowdata = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
           user= MyModel.Table_User.First(row=>row.id==Rowdata);
           MyModel.DeleteObject(user);
           FMessegeBox.FarsiMessegeBox.Show("حذف شد");
           MyModel.SaveChanges();
           dataGridViewUser.DataSource = MyModel.Table_User.ToList();



        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            stiReport1.Load(Application.StartupPath + @"\Report User.mrt");
            tableUserBindingSource.DataSource = MyModel.Table_User.ToList();
            stiReport1.RegReportDataSources();            
            stiReport1.Show();
        }

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

        private void dataGridViewUser_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox_User.Text = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
            textBox_Password.Text = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
        }



        int i;

        private void dataGridViewUser_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            try
            {

                if (FMessegeBox.FarsiMessegeBox.Show("آياسطر جاري حذف شود؟", "پيام سيستم", FMessegeBox.FMessegeBoxButtons.YesNo, FMessegeBox.FMessegeBoxIcons.Question) == DialogResult.Yes)
                {
                    Table_User user = new Table_User();
                    int Rowdata = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
                    user = MyModel.Table_User.First(row => row.id == Rowdata);
                    MyModel.DeleteObject(user);
                    FMessegeBox.FarsiMessegeBox.Show("حذف شد");
                    MyModel.SaveChanges();
                    dataGridViewUser.DataSource = MyModel.Table_User;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch
            {
            }
            
        }

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

        private void dataGridViewUser_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
           
          
           
        }
            
       
     

        private void dataGridViewUser_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                textBox_User.Text = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
                textBox_Password.Text = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
            }
            catch
            {
            }

        }

        private void dataGridViewUser_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewUser.CurrentRow.Cells[0].Value.ToString()!="0")
            {
                Table_User user = new Table_User();
                int Rowdata = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
                MyModel.Table_User.First(Row => Row.id == Rowdata);
                user.User = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
                user.Password = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
                FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
                MyModel.SaveChanges();
                dataGridViewUser.DataSource = MyModel.Table_User;
            }

           
           
              
           
            //  dataGridViewUser.DataSource = MyModel.Table_User;




            // if (op == false)
            // {

            //    Table_User user = new Table_User();
            //    int Rowdata = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
            //   MyModel.Table_User.First(Row => Row.id == Rowdata);
            //   user.User = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
            ///   user.Password = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
            //   FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
            //  MyModel.SaveChanges();
            //  dataGridViewUser.DataSource = MyModel.Table_User;
        }
            

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

        private void dataGridViewUser_AllowUserToAddRowsChanged(object sender, EventArgs e)
        {
            dataGridViewUser.AllowUserToAddRows = true;
        }





        public Table_User user { get; set; }

        public Table_User usertable { get; set; }

        private void dataGridViewUser_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void dataGridViewUser_NewRowNeeded(object sender, DataGridViewRowEventArgs e)
        {
            
        }

        private void dataGridViewUser_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridViewUser_KeyUp(object sender, KeyEventArgs e)
        {
            
             

                 
        }

        private void dataGridViewUser_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
        }

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

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

        private void dataGridViewUser_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
          
        }

        private void dataGridViewUser_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
          
        }

        private void dataGridViewUser_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
          
        }

        private void dataGridViewUser_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          
        }

        private void dataGridViewUser_NewRowNeeded_1(object sender, DataGridViewRowEventArgs e)
        {
            
        }

        private void dataGridViewUser_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            
        }

        private void dataGridViewUser_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
          
        }
        }
    }

