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 Library
{
    public partial class FormMembers : Form
    {
        DataAccess da = new DataAccess();
        public FormMembers()
        {
            InitializeComponent();
        }

        private void FormMembers_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            da.Connect();
            DataTable dt = new DataTable();
            dt = da.Select("select * from member");
            lblcount.Text = dt.Rows.Count.ToString();
            da.DisConnect();
            dataGridView1.DataSource = dt;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FormMain fe = new FormMain();
            this.Hide();
            fe.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount > 1)
            {
                DialogResult dr = MessageBox.Show("آیا مطمئن به حذف رکورد جاری می باشید ؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    string index = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
                    da.Connect();
                    string sql = "delete from member where mid='{0}'";
                    sql = string.Format(sql, index);
                    da.DoCommand(sql);
                    da.DisConnect();
                    FormMembers_Load(null, null);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("لطفا فیلد جستجو را پر نمائید!","پیام خطا",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }            
            DataTable dt = new DataTable();
            string sql;
            da.Connect();
            switch (comboBox1.Text)
            {
                case "شماره":
                    sql = "select * from member where mid='{0}'";
                    sql = string.Format(sql,txtsearch.Text);                    
                    dt = da.Select(sql);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("هیچ رکوردی با این شماره یافت نشد","پیام خطا",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        da.DisConnect();
                        return;
                    }
                    dataGridView1.DataSource = dt;
                    break;
                case "نام":
                    sql = "select * from member where mname='{0}'";
                    sql = string.Format(sql,txtsearch.Text);
                    dt = da.Select(sql);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("هیچ رکوردی با این نام یافت نشد", "پیام خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        da.DisConnect();
                        return;
                    }
                    dataGridView1.DataSource = dt;
                    break;
                case "نام خانوداگی":
                    sql = "select * from member where mfamily='{0}'";
                    sql = string.Format(sql, txtsearch.Text);
                    dt = da.Select(sql);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("هیچ رکوردی با این نام خانوادگی یافت نشد", "پیام خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        da.DisConnect();
                        return;
                    }
                    dataGridView1.DataSource = dt;
                    break;
            }
            da.DisConnect();            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            FormMembers_Load(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormMember_Add fe = new FormMember_Add();
            fe.ShowDialog();
            FormMembers_Load(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string index = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
            da.Connect();
            DataTable dt = new DataTable();
            string sql = "select * from member where mid = '{0}'";
            sql = string.Format(sql,index);
            dt = da.Select(sql);
            da.DisConnect();
            FormMember_Edit fe = new FormMember_Edit();
            fe.txtname.Text = dt.Rows[0]["mname"].ToString();
            fe.txtfamily.Text = dt.Rows[0]["mfamily"].ToString();
            fe.txtmid.Text = dt.Rows[0]["mid"].ToString();
            fe.txttell.Text = dt.Rows[0]["mtell"].ToString();
            fe.txtaddress.Text = dt.Rows[0]["maddress"].ToString();
            fe.txtuser.Text = dt.Rows[0]["muser"].ToString();
            fe.txtpass.Text = dt.Rows[0]["mpass"].ToString();
            if (dt.Rows[0]["mpic"].ToString() != "")
            {
                fe.pictureBox1.Load(dt.Rows[0]["mpic"].ToString());
                fe.pic = dt.Rows[0]["mpic"].ToString();
            }
            fe.ShowDialog();
            FormMembers_Load(null, null);
        }
    }
}
