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 The_Contacts
{
    public partial class Edit : Form
    {
        public Edit(string No)
        {
            InitializeComponent();

            ctnNo = No;
        }
        SqlConnection objSqlConnection;
        SqlCommand objSQLcommand;

        string ctnNo;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("Invalid name.", "Editing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return;
            }
            try
            {
                objSQLcommand.CommandText = "update Contact set name=@1,lastname=@2,tel=@3,mobile=@4,web=@5,email=@6,address1=@7,address2=@8,job=@9,note=@10,image=@11,isfamily=@12,BirthDay=@13 where No=" + ctnNo;
                objSQLcommand.Parameters.AddWithValue("@1", textBox1.Text);
                objSQLcommand.Parameters.AddWithValue("@2", textBox9.Text);
                objSQLcommand.Parameters.AddWithValue("@3", textBox2.Text);
                objSQLcommand.Parameters.AddWithValue("@4", textBox3.Text);
                objSQLcommand.Parameters.AddWithValue("@5", textBox5.Text);
                objSQLcommand.Parameters.AddWithValue("@6", textBox4.Text);
                objSQLcommand.Parameters.AddWithValue("@7", textBox6.Text);
                objSQLcommand.Parameters.AddWithValue("@8", textBox10.Text);
                objSQLcommand.Parameters.AddWithValue("@9", textBox8.Text);
                objSQLcommand.Parameters.AddWithValue("@10", textBox7.Text);
                objSQLcommand.Parameters.AddWithValue("@11", IMGTObyte(vistaPictureBox1.Image));
                objSQLcommand.Parameters.AddWithValue("@12", checkBox1.Checked.ToString());
                if (chkDontRemeberBrithDay.Checked)
                {
                    objSQLcommand.Parameters.AddWithValue("@13", "False");
                }
                else
                {
                    objSQLcommand.Parameters.AddWithValue("@13", dateTimePicker1.Value.ToShortDateString());
                }
               
                objSQLcommand.ExecuteNonQuery();
                FarsiDate.FarsiDateTime F = new FarsiDate.FarsiDateTime();
                MessageBox.Show( "اطلاعات " +textBox1.Text+" در تاریخ "+ F.Today +" به روز شد" , "Contacts", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Apllyed = true;
                this.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\nPlease try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); }
        }
        Byte[] IMGTObyte(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            System.IO.MemoryStream M = new System.IO.MemoryStream();
            bmp.Save(M, System.Drawing.Imaging.ImageFormat.Png);
            return M.ToArray();
            
        }
        Image IMGFROMBYTE(byte[] bytes)
        {
            System.IO.MemoryStream M = new System.IO.MemoryStream(bytes);
            return Image.FromStream(M);
        }
        String DataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Pars Contacts\DataBase.mdf";
        private void Edit_Load(object sender, EventArgs e)
        {
            objSqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + DataDirectory + ";Integrated Security=True;User Instance=True");
            try
            {
                objSqlConnection.Open();
                objSQLcommand = new SqlCommand();
                objSQLcommand.Connection = objSqlConnection;

            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "Error");
                this.Close();
            }
            try
            {
                objSQLcommand.CommandText = "select * from contact where No=" + ctnNo;
                SqlDataReader Dr = objSQLcommand.ExecuteReader();
                Dr.Read();
                textBox1.Text = Dr[1].ToString();
                textBox9.Text = Dr[2].ToString();
                textBox2.Text = Dr[3].ToString();
                textBox3.Text = Dr[4].ToString();
                textBox5.Text = Dr[5].ToString();
                textBox4.Text = Dr[6].ToString();
                textBox8.Text = Dr[7].ToString();
                textBox6.Text = Dr[8].ToString();
                textBox7.Text = Dr[10].ToString();
                textBox10.Text = Dr[9].ToString();
                if (Dr[12].ToString() == "True") checkBox1.Checked = true;
                else if (Dr[12].ToString() == "False") checkBox1.Checked = false;
                vistaPictureBox1.Image = IMGFROMBYTE((byte[])Dr[11]);
                if (Dr[14].ToString() == "False")
                {
                    dateTimePicker1.Enabled = label8.Enabled = false;
                    chkDontRemeberBrithDay.Checked = true;
                }
                else
                {
                    dateTimePicker1.Value = DateTime.Parse(Dr[14].ToString());
                    chkDontRemeberBrithDay.Checked = false;
                }
                if (Dr.IsClosed == false) Dr.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\nPlease try later", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); }
        }

    

        private void vistaApiPicture1_Click(object sender, EventArgs e)
        {
            contextMenu1.Show(vistaPictureBox1, pntBBT);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog O = new OpenFileDialog();
            O.Title = "Select an Image";
            O.Filter = "Images File|*.png;*.jpg;*.bmp;*.gif";
            O.Multiselect = false;
            if (O.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    vistaPictureBox1.Image = Image.FromFile(O.FileName);
 


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            vistaPictureBox1.Image = Properties.Resources._1324041534_User_Black;
        }

  

        private void vistaPictureBox1_Click(object sender, EventArgs e)
        {
            vistaApiPicture1_Click(null, null);
        }

        private void vistaPictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pntBBT = new Point(e.X, e.Y);
        }
        Point pntBBT;
        bool Apllyed = false;
        private void Edit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Apllyed)
            {
                if (MessageBox.Show("ایا می خواهید تغییرات را ذخیره کنید؟", textBox1.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    button1_Click(null, null);
                }
            }
        }

        private void chkDontRemeberBrithDay_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDontRemeberBrithDay.Checked) dateTimePicker1.Enabled = label8.Enabled = false;
            else

                dateTimePicker1.Enabled = label8.Enabled = true;
        }
    }
}
