using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kırtasiye_Otomasyonu_v1._0
{
    public partial class StokIslemleri : DevExpress.XtraEditors.XtraForm
    {
        public StokIslemleri()
        {
            InitializeComponent();
            // This line of code is generated by Data Source Configuration Wizard
            // Fill the SqlDataSource asynchronously
            sqlDataSource1.FillAsync();
        }
        public static SqlConnection connectionGeneral;

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            SqlConnection connection1 = DBConnection.GetConnection();
            SqlConnection connection2 = DBConnection.GetConnection();



            string urunAdi = textEdit1.Text;
            int miktar;
            decimal birimFiyat;

            if (!string.IsNullOrEmpty(textEdit2.Text) && int.TryParse(textEdit2.Text, out miktar) &&
                !string.IsNullOrEmpty(textEdit3.Text) && decimal.TryParse(textEdit3.Text, out birimFiyat))
            {
                try
                {
                    using (connection1)
                    {
                        connection1.Open();

                        
                        string checkSql = "SELECT COUNT(*) FROM [dbo].[Stok] WHERE [UrunAdi] = @UrunAdi";
                        using (SqlCommand checkCmd = new SqlCommand(checkSql, connection1))
                        {
                            checkCmd.Parameters.AddWithValue("@UrunAdi", urunAdi);
                            int count = (int)checkCmd.ExecuteScalar();
                            if (count > 0)
                            {
                                MessageBox.Show("Bu ürün daha önce zaten eklenmiş.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }

                        
                        string sql = "INSERT INTO [dbo].[Stok] ([UrunAdi], [Miktar], [BirimFiyat]) VALUES (@UrunAdi, @Miktar, @BirimFiyat)";
                        using (SqlCommand cmd = new SqlCommand(sql, connection1))
                        {
                            cmd.Parameters.AddWithValue("@UrunAdi", urunAdi);
                            cmd.Parameters.AddWithValue("@Miktar", miktar);
                            cmd.Parameters.AddWithValue("@BirimFiyat", birimFiyat);
                            cmd.ExecuteNonQuery();
                            connection1.Close();
                        }

                        MessageBox.Show("Stok kaydı başarıyla eklendi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //LoadGridData();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Stok kaydı eklenirken bir hata oluştu: " + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Lütfen tüm alanları doldurunuz ve geçerli değerler giriniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        private void LoadGridData()
        {
            connectionGeneral = DBConnection.GetConnection();

            if (connectionGeneral.State != ConnectionState.Open)
                {
                 connectionGeneral.Open();
                }
            string sql = "SELECT [ID], [UrunAdi], [Miktar], [BirimFiyat], ([Miktar]*[BirimFiyat]) AS [ToplamFiyat] FROM [dbo].[Stok]";
            using (connectionGeneral)
            {
                
                using (SqlDataAdapter da = new SqlDataAdapter(sql, connectionGeneral))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    gridControl1.DataSource = dt;
                    connectionGeneral.Close();
                }
                
            }
        }


        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textEdit3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit1.Checked == true)
            {
                groupControl3.Enabled = true;
            }
            else if (checkEdit1.Checked == false)
            {
                groupControl3.Enabled = false;
            }
        }

        private void checkEdit2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit2.Checked == true)
            {
                groupControl4.Enabled = true;
            }
            else if (checkEdit2.Checked == false)
            {
                groupControl4.Enabled = false;
            }
        }

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

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            
            if (!int.TryParse(textEdit6.Text, out int stokID) || string.IsNullOrEmpty(textEdit7.Text) || !int.TryParse(textEdit5.Text, out int miktar))
            {
                MessageBox.Show("Lütfen geçerli bir stok ID, ürün adı ve miktar girin.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            connectionGeneral = DBConnection.GetConnection();
            using (connectionGeneral)
             {
                connectionGeneral.Open();
                string query = "UPDATE [dbo].[Stok] SET [UrunAdi] = @urunAdi, [Miktar] = @miktar, [BirimFiyat] = @birimFiyat WHERE [ID] = @stokID";
                using (SqlCommand cmd = new SqlCommand(query, connectionGeneral))
                {
                    cmd.Parameters.AddWithValue("@urunAdi", textEdit7.Text);
                    cmd.Parameters.AddWithValue("@miktar", int.Parse(textEdit5.Text));
                    cmd.Parameters.AddWithValue("@birimFiyat", decimal.Parse(textEdit4.Text));
                    cmd.Parameters.AddWithValue("@stokID", int.Parse(textEdit6.Text));
                    cmd.ExecuteNonQuery();
                 }
                connectionGeneral.Close();
             }

            LoadGridData();
            MessageBox.Show("Stok güncellendi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
           
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            
            if (!int.TryParse(textEdit8.Text, out int stokID))
            {
                MessageBox.Show("Lütfen geçerli bir stok ID girin.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            
            DialogResult result = MessageBox.Show("Stok ID: " + stokID.ToString() + " olan ürünü silmek istiyor musunuz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                connectionGeneral = DBConnection.GetConnection();
                using (connectionGeneral)
                 {
                    connectionGeneral.Open();
                     string query = "DELETE FROM [dbo].[Stok] WHERE [ID] = @stokID";
                     using (SqlCommand cmd = new SqlCommand(query, connectionGeneral))
                     {
                        cmd.Parameters.AddWithValue("@stokID", stokID);
                      cmd.ExecuteNonQuery();

                    }
                }
                LoadGridData();
                MessageBox.Show("Stok silindi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
        }

        private void StokIslemleri_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}