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 MyDataAccess;
using System.Data.SqlClient;
namespace Amozeshgah
{
    public partial class mDoros : Form
    {
        public mDoros()
        {
            InitializeComponent();
        }


        cDataAccess cData = new cDataAccess(clsPath.ConnectionString());
        static SqlConnection con = new SqlConnection(clsPath.ConnectionString());
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();

        private void txtSearching_TextChanged(object sender, EventArgs e)
        {
            string s;
            ds.Clear();
            if (rdbNameDoros.Checked)
            {
                s = "select * from tDoros where NameDars like N'%" + txtSearching.Text + "%'";
                da.SelectCommand.CommandText = s;

            }
            if (rdbMozo.Checked)
            {
                s = "select * from tDoros where Mozo like N'%" + txtSearching.Text + "%'";
                da.SelectCommand.CommandText = s;

            }
            da.Fill(ds, "ID");
            dgDoros.DataSource = ds.Tables["ID"];
        }

        private void mDoros_Load(object sender, EventArgs e)
        {
            dgDoros.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(50, 228, 125);
            dgDoros.DefaultCellStyle.SelectionBackColor = Color.FromArgb(99, 145, 152);
            dgDoros.DefaultCellStyle.WrapMode = DataGridViewTriState.False;
            dgDoros.GridColor = Color.FromArgb(214, 214, 214);
            dgDoros.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgDoros.MultiSelect = false;
            dgDoros.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgDoros.RowHeadersVisible = false;
            dgDoros.ReadOnly = true;
            dgDoros.AllowUserToResizeRows = false;
            DataSet ret = cData.ExecuteQuery("SELECT [ID],[NameDars],[Mozo],[TedadVahed],[NameOstad]FROM [tDoros]");
            dgDoros.DataSource = ret.Tables[0];
        }

        private void dgDoros_SizeChanged(object sender, EventArgs e)
        {

        }

        private void dgDoros_SelectionChanged(object sender, EventArgs e)
        {
            if (dgDoros.SelectedRows.Count > 0)
            {

                txtNameDars.Text = dgDoros.CurrentRow.Cells["NameDars"].Value.ToString();
                txtMozo.Text = dgDoros.CurrentRow.Cells["Mozo"].Value.ToString();
                txtTedadVahed.Text = dgDoros.CurrentRow.Cells["TedadVahed"].Value.ToString();
                txtOstad.Text = dgDoros.CurrentRow.Cells["NameOstad"].Value.ToString();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            cData.Sql_Command.Parameters.AddWithValue("@NameDars", txtNameDars.Text);
            cData.Sql_Command.Parameters.AddWithValue("@Mozo", txtMozo.Text);
            cData.Sql_Command.Parameters.AddWithValue("@TedadVahed", txtTedadVahed.Text);
            cData.Sql_Command.Parameters.AddWithValue("@NameOstad", txtOstad.Text);
            try
            {
                cData.Sql_Command.Parameters.AddWithValue("@ID", dgDoros.SelectedRows[0].Cells[0].Value.ToString());


                string sql = "UPDATE [tDoros] SET [NameDars]=@NameDars,[Mozo]=@Mozo,[TedadVahed]=@TedadVahed,[NameOstad]=@NameOstad  WHERE [ID]=@ID";

                cData.ExecuteNonQuery(sql);
                cData.Sql_Command.Parameters.Clear();
            }
            catch (Exception)
            {

                FarsiMessage.Show("درسی برای ویرایش کردن وجود ندارد", "اخطار سیستم", FarsiMessageBoxButtons.OK, FarsiMessageBoxIcons.Error);

            }
            
            mDoros_Load(sender, e);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
            string sql = "DELETE FROM [tDoros] WHERE ID=" + dgDoros.SelectedRows[0].Cells[0].Value.ToString();
            cData.ExecuteNonQuery(sql);

            }
            catch (Exception)
            {

                FarsiMessage.Show("درسی برای حذف کردن وجود ندارد", "اخطار سیستم", FarsiMessageBoxButtons.OK, FarsiMessageBoxIcons.Error);
            }
            
            mDoros_Load(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            mDoros_Load(sender, e);
            txtSearching.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
