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

namespace LibraryMembers
{
    public partial class frmRegister : Form
    {

        public void dbConnection()
        {
            clsGrideview dbCon1 = new clsGrideview();
            dataGridView1.DataSource = dbCon1.dbConnection(@"SELECT MemberList1.* FROM dbo.MemberList1");
        }

        public void dbConnection2()
        {
            clsForCombo cmbP2 = new clsForCombo();
            comboBox1.DataSource =  cmbP2.dbConnection2(@"SELECT StateID, MemberState FROM dbo.MemberState");
            comboBox1.ValueMember = "StateID";
            comboBox1.DisplayMember = "MemberState";
        }
        public void insSql()
        {
            clsAdding insTodb = new clsAdding();
            insTodb.insSql("insert into Members (MemBerID, Name, Sname, RegDate, ExprDate, melliCode, memberState, Notes)values('" + textBox1.Text + "' , '" + textBox2.Text + "' , '" + textBox3.Text + "' , '" + textBox4.Text + "' , '" + textBox5.Text + "' , '" + textBox6.Text + "' , '" + comboBox1.SelectedValue + "' , '" + textBox7.Text + "')");
            dbConnection();
        }
        
        public frmRegister()
        {
            InitializeComponent();
        }

        private void btnReg_Click(object sender, EventArgs e)
        {
                    if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || comboBox1.Text == "")
                    {
                        MessageBox.Show("تمامی فیلدها بایستی دارای مقدار باشند");
                    }
                    else
                    {
                        insSql();
                    }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dbConnection();
            dbConnection2();
            
        }

        protected void txtClear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {

                    ((TextBox)c).Clear();

                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("کد عضویت وارد نشده");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@MemBerID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db.exeCommand("sp_Members_DeleteRow");
                dbConnection();
                dbConnection2();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(textBox3.Text == "")
            {
                MessageBox.Show("نام خانوادگی و یا بخشی از نام خانوادگی وارد نشده");
            }
            else
            {
                clsGrideview dbCon1 = new clsGrideview();
                                                                
                dataGridView1.DataSource = dbCon1.dbConnection(@"SELECT     TOP 100 PERCENT MemBerID, Name, Sname, RegDate, ExprDate, melliCode, MemberState FROM MemberList1 WHERE     (Sname LIKE N'%"+ textBox3.Text +"%') ORDER BY MemBerID");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("تمامی فیلدها بایستی دارای مقدار باشند");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@MemBerID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db.cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
                db.cmd.Parameters.Add("@Sname", SqlDbType.NVarChar).Value = textBox3.Text.Trim();
                db.cmd.Parameters.Add("@RegDate", SqlDbType.NVarChar).Value = textBox4.Text.Trim();
                db.cmd.Parameters.Add("@ExprDate", SqlDbType.NVarChar).Value = textBox5.Text.Trim();
                db.cmd.Parameters.Add("@melliCode", SqlDbType.NVarChar).Value = textBox5.Text.Trim();
                db.cmd.Parameters.Add("@memberState", SqlDbType.Int).Value = comboBox1.SelectedValue;
                db.cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = textBox7.Text.Trim();

                db.exeCommand("sp_Members_Update");
                dbConnection();
                dbConnection2();
                txtClear();
            }
        }
    }
}