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 Javaher
{
    public partial class Form4 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=javaher;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        public static DataGridView dv;
        DataTable dt = new DataTable();

        public Form4()
        {
            InitializeComponent();
        }

        void showgrid()
        {
            dt.Clear();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select * from [customer]";
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtname.Text == "" || txttel.Text == "")
            {
                MessageBox.Show("لطفا فیلدهای ستاره دار را پر نمایید", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtname.Focus();
            }
            else
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "insert into [customer] (name,fathername,iid,mellicode,tel,adress) values ('"+txtname.Text.Trim()+"','"+txtfather.Text.Trim()+"','"+txtiid.Text.Trim()+"','"+txtmellicode.Text.Trim()+"','"+txttel.Text.Trim()+"','"+txtadress.Text.Trim()+"')";
                cmd.ExecuteNonQuery();
                MessageBox.Show("ثبت با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();
                showgrid();
                txtadress.Text = txtfather.Text = txtiid.Text = txtmellicode.Text = txtname.Text = txttel.Text = "";
                txtname.Focus();
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            dv = dataGridView1;
            showgrid();
            dataGridView1.Columns[0].HeaderText = "کد مشتری";
            dataGridView1.Columns[1].HeaderText = "نام";
            dataGridView1.Columns[2].HeaderText = "نام پدر";
            dataGridView1.Columns[3].HeaderText = "ش-ش";
            dataGridView1.Columns[4].HeaderText = "کد ملی";
            dataGridView1.Columns[5].HeaderText = "تلفن";
            dataGridView1.Columns[6].HeaderText = "آدرس";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtcode.Text == "")
            {
                MessageBox.Show("لطفا کد مشتری را وارد نمایید", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtcode.Focus();
            }
            else
            {
                ///////////////////
                con.Open();
                SqlCommand cmdread = new SqlCommand();
                cmdread.Connection = con;
                cmdread.CommandText = "select * from [customer] where id='"+txtcode.Text.Trim()+"'";
                SqlDataReader dr = cmdread.ExecuteReader();
                if (dr.Read() == false)
                {
                    MessageBox.Show("کد مشتری وارده معتبر نیست", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtcode.Text = "";
                    txtcode.Focus();
                }
                else
                {
                    dr.Close();
                    cmd.Connection = con;
                    cmd.CommandText = "delete from [customer] where id='" + txtcode.Text.Trim() + "'";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("حذف با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtcode.Text = "";
                    txtcode.Focus();
                }
                con.Close();
                //////////////////
                showgrid();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            /////
            string code = dataGridView1[0, row].Value.ToString();
            string name = dataGridView1[1, row].Value.ToString();
            string father = dataGridView1[2, row].Value.ToString();
            string iid = dataGridView1[3, row].Value.ToString();
            string melli = dataGridView1[4, row].Value.ToString();
            string tel = dataGridView1[5, row].Value.ToString();
            string adress = dataGridView1[6, row].Value.ToString();
            /////
            EditCustomer edc = new EditCustomer();
            edc.txtcode.Text = code;
            edc.txtname.Text = name;
            edc.txtfather.Text = father;
            edc.txtiid.Text = iid;
            edc.txtmellicode.Text = melli;
            edc.txttel.Text = tel;
            edc.txtadress.Text = adress;
            edc.ShowDialog();
        }
    }
}
