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 AmanatForm : Form
    {
        public AmanatForm()
        {
            InitializeComponent();
        }

        private void Form10_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {

  
        }

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

        private void button4_Click(object sender, EventArgs e)
        {
            int maxID;
            sqlDataAdapter1.SelectCommand.CommandText = "SELECT     MAX(rank) AS rank FROM integrity";
            sqlConnection1.Open();
            try
            {
                maxID = Convert.ToInt16(sqlDataAdapter1.SelectCommand.ExecuteScalar()) + 1;
            }
            catch
            {
                maxID = 1;
            }
            sqlConnection1.Close();
            
            MMDClassLibrary.MDate MyDate = new MMDClassLibrary.MDate();
            sqlDataAdapter1.InsertCommand.Parameters.Clear();
            sqlDataAdapter1.InsertCommand.CommandText = "INSERT INTO [integrity] ([rank], [idCd], [idRemember], [dateIntegrity], [rateIntergrity], [idSeller]) VALUES (@rank, @idCd, @idRemember, @dateIntegrity, @rateIntergrity, @idSeller)";
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@rank", maxID);
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@idCd", textBox1.Text);
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@idRemember", textBox2.Text);
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@rateIntergrity", textBox3.Text);
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@idSeller", textBox4.Text);
            sqlDataAdapter1.InsertCommand.Parameters.AddWithValue("@dateIntegrity", MyDate.Today());
            sqlConnection1.Open();

            try
            {
                sqlDataAdapter1.InsertCommand.ExecuteNonQuery();
                sqlConnection1.Close();

            SqlDataReader myRead;
            sqlDataAdapter2.SelectCommand.Parameters.Clear();
            sqlDataAdapter2.SelectCommand.CommandText = "SELECT numberCd FROM cd WHERE idCd=@id ";
            sqlDataAdapter2.SelectCommand.Parameters.AddWithValue("@id",Convert.ToInt32(textBox1.Text));
            sqlConnection1.Open();
            myRead = sqlDataAdapter2.SelectCommand.ExecuteReader();
            if (myRead.Read())
            {
                label5.Text = Convert.ToString(myRead["numberCd"]);
                label5.Visible = false;
            }
            sqlConnection1.Close();
            int x = Convert.ToInt32(label5.Text);
                if(x==0)
                {
                    MMDClassLibrary.MMessageBox bb = new MMDClassLibrary.MMessageBox();
                    bb.ShowMessage("سی دی مورد نظر در حال حاضر در بانک موجود نمی باشد ");
                    return;
                }
            x--;
        

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

                sqlConnection1.Open();
                sqlDataAdapter2.UpdateCommand.ExecuteNonQuery();
                sqlConnection1.Close();

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

            sqlConnection1.Close();
        }

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

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