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

namespace WindowsApplication1
{
    public partial class frmPhoneNumbers : Form
    {
        public frmPhoneNumbers()
        {
            InitializeComponent();
        }
        private void grdFill()
        {
            FirstClass aa = new FirstClass();
            dataGridView1.DataSource = aa.dbOut(@"SELECT     PhoneNumbers.PhoneNumberID AS [کد شماره], PhoneNumberTypes.PhoneNumberType AS [نوع شماره تلفن], 
                      CustumerCode.Name + N'  ' + CustumerCode.Sname AS [نام و نام خانوادگی], PhoneNumbers.PhoneNumber AS [شماره تلفن]
                        FROM         CustumerCode INNER JOIN PhoneNumbers ON CustumerCode.CustomerID = PhoneNumbers.CostumerID INNER JOIN
                      PhoneNumberTypes ON PhoneNumbers.PhoneNumberTypeID = PhoneNumberTypes.PhonesTypeID");
            cmb1Fill();
            cmb2Fill();
        }

        private void cmb1Fill()
        {
            FirstClass aa = new FirstClass();
            comboBox1.DataSource = aa.dbOut("SELECT PhonesTypeID, PhoneNumberType FROM PhoneNumberTypes");
            comboBox1.DisplayMember = "PhoneNumberType";
            comboBox1.ValueMember = "PhonesTypeID";
        }

        private void cmb2Fill()
        {
            FirstClass aa = new FirstClass();
            comboBox2.DataSource = aa.dbOut("SELECT     CustomerID, Name + N'  ' + Sname AS Expr1 FROM CustumerCode");
            comboBox2.DisplayMember = "Expr1";
            comboBox2.ValueMember = "CustomerID";
        }

        private void dbIns()
        {

            if (textBox1.Text == "")
            {
                MessageBox.Show("فیلد شماره تلفن باید وارد شود");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@PhoneNumberTypeID", SqlDbType.Int).Value = comboBox1.SelectedValue;
                db.cmd.Parameters.Add("@CostumerID", SqlDbType.Int).Value = comboBox2.SelectedValue;
                db.cmd.Parameters.Add("@PhoneNumber ", SqlDbType.NVarChar).Value = textBox1.Text.Trim();

                db.exeCommand("sp_PhoneNumbers_Insert");
                txtClear();
                MessageBox.Show("رکورد جدید درج شد");
                grdFill();
            }
        }

        private void dbEdt()
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("فیلد شماره تلفن باید وارد شود");
            }
            else if (textBox6.Text == "")
            {
                MessageBox.Show("لطفا کد شماره را وارد نمائید");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@PhoneNumberID", SqlDbType.Int).Value = int.Parse(textBox6.Text.Trim());
                db.cmd.Parameters.Add("@PhoneNumberTypeID", SqlDbType.Int).Value = comboBox1.SelectedValue;
                db.cmd.Parameters.Add("@CostumerID", SqlDbType.Int).Value = comboBox2.SelectedValue;
                db.cmd.Parameters.Add("@PhoneNumber ", SqlDbType.NVarChar).Value = textBox1.Text.Trim();


                db.exeCommand("sp_PhoneNumbers_Update");
                txtClear();
                MessageBox.Show("رکورد مورد نظر ویرایش شد");
                grdFill();
            }
        }

        private void dbDel()
        {
            if (textBox6.Text == "")
            {
                MessageBox.Show("لطفا کد شخص را وارد نمائید");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@PhoneNumberID", SqlDbType.Int).Value = int.Parse(textBox6.Text.Trim());

                db.exeCommand("sp_PhoneNumbers_DeleteRow");
                txtClear();
                MessageBox.Show("رکورد مورد نظر حذف شد");
                grdFill();
            }
        }

        private void txtClear()
        {
            textBox1.Text = "";
        }

        private void frmPhoneNumbers_Load(object sender, EventArgs e)
        {
            grdFill();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dbIns();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dbEdt();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dbDel();
        }
    }
}