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 ReturnCD : Form
    {
        public ReturnCD()
        {
            InitializeComponent();
        }
        public static string dateII;
        public static string IDR;
        private void button2_Click(object sender, EventArgs e)
        {
        }

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

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void textbp_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form11_Load(object sender, EventArgs e)
        {
            ((MainForm)(this.MdiParent)).notifyIcon1.ShowBalloonTip(5000, "عودت", "....", ToolTipIcon.None);
        }

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

        }

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

        private void button5_Click(object sender, EventArgs e)
        {
                if (textBox1.Text == "" || textBox2.Text == "")
                {
                    emptyErorre emp = new emptyErorre();
                    emp.ShowDialog();
                    return;
                }
            
            IDR = textBox2.Text;
            SqlDataReader my;
            sqlDataAdapter1.SelectCommand.Parameters.Clear();
            sqlDataAdapter1.SelectCommand.CommandText = "SELECT integrity.* FROM integrity where ((idCd = @idCd ) and (idRemember = idRemember))";
            sqlDataAdapter1.SelectCommand.Parameters.AddWithValue("@idCd", Convert.ToInt64(textBox1.Text));
            sqlDataAdapter1.SelectCommand.Parameters.AddWithValue("@idRemember", Convert.ToInt64(textBox2.Text));
            sqlConnection1.Open();
            my = sqlDataAdapter1.SelectCommand.ExecuteReader();
            if (my.Read())
            {
                dateII = Convert.ToString(my["dateIntegrity"]);
                MMDClassLibrary.MDate d = new MMDClassLibrary.MDate();
                string mydate = d.Today();
                int d1 = d.StrDateToInt(mydate);
                int d2 = d.StrDateToInt(dateII);
                int verify = d1 - d2;
                if (verify > 2)
                {
                    notifyIcon1.ShowBalloonTip(1000, "شما دیرکرد دارید ", "این مشتری" + (verify - 2).ToString() + " روز دیرکرد داشته است", ToolTipIcon.Info);
                    MMDClassLibrary.MMessageBox al = new MMDClassLibrary.MMessageBox();
                    al.ShowMessage("شما دیرکرد داشتید . پرداخت جریمه");
                    if (al.ShowYesNo())
                    {
                        Jarime f18 = new Jarime();
                        f18.ShowDialog();
                    }
                    else
                    {
                        MMDClassLibrary.MMessageBox mx = new MMDClassLibrary.MMessageBox();
                        mx.ShowAlertMessage("این دفعه شما مورد بخشش قرار گرفتید ");
                    }
                }
                sqlConnection1.Close();

                sqlDataAdapter1.SelectCommand.Parameters.Clear();
                sqlDataAdapter1.DeleteCommand.Parameters.Clear();
                sqlDataAdapter1.DeleteCommand.CommandText = "DELETE FROM [integrity] WHERE (([idCd] = @idCd) AND ([idRemember] = @idR))";
                sqlDataAdapter1.DeleteCommand.Parameters.AddWithValue("@idCd", Convert.ToInt64(textBox1.Text));
                sqlDataAdapter1.DeleteCommand.Parameters.AddWithValue("@idR", Convert.ToInt64(textBox2.Text));
                sqlConnection1.Open();
                try
                {
                    sqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
                }
                catch
                {
                    MMDClassLibrary.MMessageBox n1 = new MMDClassLibrary.MMessageBox();
                    n1.ShowAlertMessage("اطلاعات داده شده با اطلاعات بانک داده همخوانی ندارد لطفا دوباره سعی کنید ");
                }
                sqlConnection1.Close();
            }
            else
            {
                MMDClassLibrary.MMessageBox nnn = new MMDClassLibrary.MMessageBox();
                nnn.ShowAlertMessage("unable to read date bese");
                sqlConnection1.Close();
                return;
            }
            sqlConnection1.Close();

            /*********************************************************************************/

            SqlDataReader myRead;
            sqlDataAdapter2.SelectCommand.Parameters.Clear();
            sqlDataAdapter2.SelectCommand.CommandText = "SELECT cd.numberCd FROM cd WHERE idCd=@idCD1 ";
            sqlDataAdapter2.SelectCommand.Parameters.AddWithValue("@idCD1",Convert.ToInt64(textBox1.Text));
            sqlConnection1.Open();
            myRead = sqlDataAdapter2.SelectCommand.ExecuteReader();
            if (myRead.Read())
            {
                label3.Text = Convert.ToString(myRead["numberCd"]);
               // label5.Visible = false;
            }
            sqlConnection1.Close();
            int x = Convert.ToInt32(label3.Text);
            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.ToInt64(textBox1.Text));

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

                MMDClassLibrary.MMessageBox p = new MMDClassLibrary.MMessageBox();
                p.ShowSaveDialog1();
 
            sqlDataAdapter2.SelectCommand.CommandText = "SELECT integrity.* FROM integrity";


            textBox1.Text = "";
            textBox2.Text = "";

        }
    }
}