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

namespace WindowsApplication2
{
    public partial class Sale : Form
    {
        public Sale()
        {
            InitializeComponent();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
            pictureBox1.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form19_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet21.cd2' table. You can move, or remove it, as needed.
            this.cd2TableAdapter.Fill(this.dataSet21.cd2);
            // TODO: This line of code loads data into the 'dataSet21.cd' table. You can move, or remove it, as needed.
            this.cdTableAdapter.Fill(this.dataSet21.cd);

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = comboBox2.Text;
            SqlDataReader myRead;
            sqlDataAdapter1.SelectCommand.Parameters.Clear();
            sqlDataAdapter1.SelectCommand.CommandText = "SELECT cd.* FROM cd WHERE nameCd=@name ";
            sqlDataAdapter1.SelectCommand.Parameters.AddWithValue("@name", str);
            sqlConnection1.Open();
            myRead = sqlDataAdapter1.SelectCommand.ExecuteReader();
            if (myRead.Read())
            {
                groupBox5.Visible = true;
                textBox2.Text = Convert.ToString(myRead["idCd"]);
                label10.Text = Convert.ToString(myRead["typeCd"]);
                label11.Text = Convert.ToString(myRead["rateCd"]);
                label16.Text = label11.Text;
                textBox4.Text = label11.Text;
                label12.Text = Convert.ToString(myRead["numberCd"]);
                label13.Text = Convert.ToString(myRead["discription"]);
            }
            sqlConnection1.Close();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            long r;
            try
            {
                if (Int64.TryParse(textBox5.Text, out r))
                {
                    label16.Text = Convert.ToString(Convert.ToInt64(textBox5.Text) * Convert.ToInt64(label11.Text));
                    textBox4.Text = label16.Text;
                }
            }
            catch (System.Exception)
            {
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox4.Visible = true;
            label4.Visible = true;
            textBox4.Visible = true;
            label17.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (radioButton6.Checked == true)
            {
                long x = Convert.ToInt64(label12.Text) - Convert.ToInt64(textBox5.Text);
                if (x < 0)
                {
                    MMDClassLibrary.MMessageBox mb1 = new MMDClassLibrary.MMessageBox();
                    mb1.ShowAlertMessage("درخواست شما از موجودی بانک بیشتر هست . لطفا به موجودی بانک نیز توجه فرمایید ");
                    return;
                }

                sqlDataAdapter3.UpdateCommand.Parameters.Clear();
                sqlDataAdapter3.UpdateCommand.CommandText = " UPDATE [cd] SET [numberCd] = @numberCd WHERE ([idCd] = @idCd)";
                sqlDataAdapter3.UpdateCommand.Parameters.AddWithValue("@numberCd", x);
                sqlDataAdapter3.UpdateCommand.Parameters.AddWithValue("@idCd", Convert.ToInt32(textBox2.Text));

                sqlConnection1.Open();
                sqlDataAdapter3.UpdateCommand.ExecuteNonQuery();
                sqlConnection1.Close();
            }
            ////////////////////////////////////////////////////////
            long maxID;
            sqlDataAdapter1.SelectCommand.CommandText = "SELECT MAX(rank) AS rank FROM salecd";
            try
            {
                sqlConnection1.Open();
                maxID = Convert.ToInt64(sqlDataAdapter1.SelectCommand.ExecuteScalar()) + 1;
            }
            catch
            {
                maxID = 1;
            }
            sqlConnection1.Close();
            MMDClassLibrary.MDate MyDate = new MMDClassLibrary.MDate();
            sqlDataAdapter2.InsertCommand.Parameters.Clear();
            sqlDataAdapter2.InsertCommand.CommandText="INSERT INTO [salecd] ([rank], [idCd], [number], [idRememmber], [dateSale], [idSeller], [rateSale]) VALUES (@rank, @idCd, @number, @idRememmber, @dateSale, @idSeller, @rateSale)";
            
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@rank", maxID);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@idRememmber", textBox1.Text);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@idCd", textBox2.Text);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@idSeller", textBox3.Text);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@rateSale", textBox4.Text);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@number", textBox5.Text);
            sqlDataAdapter2.InsertCommand.Parameters.AddWithValue("@dateSale", MyDate.Today());
            sqlConnection1.Open();

            try
            {
                sqlDataAdapter2.InsertCommand.ExecuteNonQuery();
                MMDClassLibrary.MMessageBox p = new MMDClassLibrary.MMessageBox();
                p.ShowSaveDialog1();
            }
            catch
            {
                MMDClassLibrary.MMessageBox m = new MMDClassLibrary.MMessageBox();
                m.ShowAlertMessage("اطلاعات داده شده همخوانی ندارد ");
            }
            sqlConnection1.Close();
            sqlDataAdapter1.SelectCommand.CommandText = "SELECT saled.* FROM saled";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox3.Visible = false;
 
        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox1.Visible = true;
            textBox1.ReadOnly = false;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox4.Visible = false;
            label4.Visible = false;
            textBox4.Visible = false;
            label17.Visible = false;
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {

        }


    }
}