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;
using FMessegeBox;

namespace WindowsFormsApplication8
{
    public partial class frmAdd : Form
    {

        SqlConnection con;
        SqlDataAdapter ada = new SqlDataAdapter();
        SqlDataAdapter ada2 = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataSet ds2 = new DataSet();

        bool check = false;

        public frmAdd()
        {
            InitializeComponent();
        }

        private void frmAdd_Load(object sender, EventArgs e)
        {
            MinimizeBox = false;
            MaximizeBox = false;
            
            ConnectionInfo info = new ConnectionInfo();
            info.ConnectDB_Info();
            con = new SqlConnection(ConnectionInfo.ConnectionString);
            ada.SelectCommand = new SqlCommand("select * from Telltbl where id=-1", con);

            SqlCommandBuilder b = new SqlCommandBuilder(ada);
            ada.InsertCommand = b.GetInsertCommand();
            ada.DeleteCommand = b.GetDeleteCommand();
            ada.UpdateCommand = b.GetUpdateCommand();

            ada.Fill(ds);
            
            comboTitle.SelectedItem = "آقا";
            
            txtName.DataBindings.Add(new Binding("Text", ds.Tables[0], "TName"));
            txtLname.DataBindings.Add(new Binding("Text", ds.Tables[0], "TLname"));
            txtCompany.DataBindings.Add(new Binding("Text", ds.Tables[0], "TCompany"));
            txtTell.DataBindings.Add(new Binding("Text", ds.Tables[0], "TTell"));
            txtMobile.DataBindings.Add(new Binding("Text", ds.Tables[0], "TMobile"));
            txtFax.DataBindings.Add(new Binding("Text", ds.Tables[0], "TFax"));
            txtAdress.DataBindings.Add(new Binding("Text", ds.Tables[0], "TAdress"));
            comboTitle.DataBindings.Add(new Binding("SelectedItem", ds.Tables[0], "TTitle"));
            txtEmail.DataBindings.Add(new Binding("Text", ds.Tables[0], "Email"));

            BindingContext[ds.Tables[0]].AddNew();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (txtLname.Text != "")
            {
                check = true;
                
                ada2.SelectCommand = new SqlCommand("Select * from TellTbl where TLname='" + txtLname.Text + "' and (TName='" + txtName.Text + "'or TName='.')", con);
                ada2.Fill(ds2);
                if (ds2.Tables[0].Rows.Count >= 1)
                {
                    DialogResult dr= FarsiMessegeBox.Show("تلفن فرد مورد نظر ثبت شده است . آیا مایلید دوباره ذخیره شود؟", "", FMessegeBoxButtons.YesNo, FMessegeBoxIcons.Question);
                    if (dr == DialogResult.No)
                    {
                        check = false;
                    }
                    
                }
            }
            else
            {
                errorProvider1.SetError(txtLname, ".نام خانوادگی خالی است");
            }

            if (txtName.Text == "")
            {
                txtName.Text = ".";
            }

            if (txtEmail.Text == "")
            {
                txtEmail.Text = ".";
            }
            else
            {
                if (!ClsSetting.validatingEmail(txtEmail.Text))
                {
                    errorProvider1.SetError(txtEmail, "لطفا ایمیل صحیح را وارد کنید");
                    return;
                }
            }

            if (txtTell.Text == "")
            {
                txtTell.Text = ".";
            }
            if (txtMobile.Text == "")
            {
                txtMobile.Text = ".";
            }
            if (txtFax.Text == "")
            {
                txtFax.Text = ".";
            }
            if (txtAdress.Text == "")
            {
                txtAdress.Text = ".";
            }
            if (txtCompany.Text == "")
            {
                txtCompany.Text = ".";
            }

            if (check == true)
            {
                BindingContext[ds.Tables[0]].EndCurrentEdit();
                ada.Update(ds);
                BindingContext[ds.Tables[0]].AddNew();
                check = false;
                errorProvider1.Clear();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            BindingContext[ds.Tables[0]].AddNew();
        }

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

        private void txtTell_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }

        private void txtFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }

        private void txtMobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }
    }
}
