/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bp2_lab9;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author samet
 */
public class Soru3 extends javax.swing.JFrame {

    /**
     * Creates new form Soru3
     */
    DefaultTableModel dtm;
    ArrayList<Integer> malzemeid = new ArrayList<>();
    ArrayList<Integer> musteriid = new ArrayList<>();
    ArrayList<Object[]> kayitbekleyen = new ArrayList<>();

    public Soru3() {
        try {
            initComponents();

            dtm = new DefaultTableModel();
            dtm.setColumnIdentifiers(new Object[]{"ID", "MUSTERİ ID", "MUSTERİ ADI", "MALZEME ID", "MALZEME ADI", "SATIS", "ADET", "TUTAR"});
            tbl_liste.setModel(dtm);
// bağlantı için
            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/DEPO", "sa", "as");
            // iletişimi acıyor
            Statement stmt = con.createStatement();
            // veritabanında sorgu çalıştırıp bize geri tablo döndürüyor.
            ResultSet rs = stmt.executeQuery("SELECT * FROM MALZEME");
            while (rs.next()) {

                String malzemeadi = rs.getString("ADI");
                malzemeid.add(rs.getInt("ID"));
                cmb_malzeme.addItem(malzemeadi);
            }

            rs = stmt.executeQuery("SELECT * FROM MUSTERI");
            while (rs.next()) {

                String adi = rs.getString("ADI");
                musteriid.add(rs.getInt("ID"));
                cmb_musteri.addItem(adi);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Soru3.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmb_malzeme = new javax.swing.JComboBox<>();
        cmb_musteri = new javax.swing.JComboBox<>();
        btn_ekle = new javax.swing.JButton();
        btn_eklenentoplam = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        btn_sorgula = new javax.swing.JButton();
        btn_satissil = new javax.swing.JButton();
        btn_satilmayanmalzeme = new javax.swing.JButton();
        btn_musterisatisoku = new javax.swing.JButton();
        btn_satisekle = new javax.swing.JButton();
        btn_toplamtutaryaz = new javax.swing.JButton();
        txt_tutar = new javax.swing.JTextField();
        txt_adet = new javax.swing.JTextField();
        txt_toplameklenen = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_liste = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        getContentPane().add(cmb_malzeme, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 60, 140, -1));

        getContentPane().add(cmb_musteri, new org.netbeans.lib.awtextra.AbsoluteConstraints(292, 61, 140, -1));

        btn_ekle.setText("EKLE");
        btn_ekle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ekleActionPerformed(evt);
            }
        });
        getContentPane().add(btn_ekle, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 60, -1, -1));

        btn_eklenentoplam.setText("EKLENEN TOPLAMI BUL");
        btn_eklenentoplam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_eklenentoplamActionPerformed(evt);
            }
        });
        getContentPane().add(btn_eklenentoplam, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 110, -1, -1));

        jButton3.setText("VERİ TABANINA EKLE");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 60, -1, -1));

        btn_sorgula.setText("SORGULA");
        getContentPane().add(btn_sorgula, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 390, 110, -1));

        btn_satissil.setText("SATIŞ YAZ");
        getContentPane().add(btn_satissil, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 450, 110, -1));

        btn_satilmayanmalzeme.setText("SATILMAYAN MALZEME");
        getContentPane().add(btn_satilmayanmalzeme, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 390, 180, -1));

        btn_musterisatisoku.setText("MUSTERİ SATIŞ OKU");
        getContentPane().add(btn_musterisatisoku, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 450, 170, -1));

        btn_satisekle.setText("SATIŞ EKLE");
        getContentPane().add(btn_satisekle, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 390, 110, -1));

        btn_toplamtutaryaz.setText("TOPLAM TUTAR YAZ");
        getContentPane().add(btn_toplamtutaryaz, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 450, 110, -1));
        getContentPane().add(txt_tutar, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 110, 140, -1));
        getContentPane().add(txt_adet, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 110, 140, -1));
        getContentPane().add(txt_toplameklenen, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 110, 80, -1));

        tbl_liste.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbl_liste);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 880, 170));

        jLabel1.setText("MÜŞTERİ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 70, -1, -1));

        jLabel2.setText("MALZEME");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 66, -1, 10));

        jLabel3.setText("TUTAR");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        jLabel4.setText("ADET");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 120, -1, -1));

        jLabel5.setText("TOPLAM EKLENEN");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 120, -1, -1));

        jLabel6.setText("4. SORU");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 460, -1, 20));

        jLabel7.setText("3. SORU ");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 400, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents
   int enbuyuk = 0;
    private void btn_ekleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ekleActionPerformed
        try {
            // TODO add your handling code here:
            String mazemeadi = cmb_malzeme.getSelectedItem().toString();
            String musteriadi = cmb_musteri.getSelectedItem().toString();
            int tutar = Integer.parseInt(txt_tutar.getText());
            int adet = Integer.parseInt(txt_adet.getText());
            int mzid = malzemeid.get(cmb_malzeme.getSelectedIndex());
            int msid = musteriid.get(cmb_musteri.getSelectedIndex());

            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/DEPO", "sa", "as");
            // iletişimi acıyor
            Statement stmt = con.createStatement();
            // veritabanında sorgu çalıştırıp bize geri tablo döndürüyor.
            ResultSet rs = stmt.executeQuery("SELECT * FROM SATIS");

            while (rs.next()) {
                if (rs.getInt("ID") > enbuyuk) {
                    enbuyuk = rs.getInt("ID");
                }
            }
            enbuyuk++;
            kayitbekleyen.add(new Object[]{enbuyuk, musteriadi, msid, mazemeadi, mzid, 0, adet, tutar});
            dtm.addRow(kayitbekleyen.get(kayitbekleyen.size() - 1));
//"ID", "MUSTERİ ID", "MUSTERİ ADI", "MALZEME ID", "MALZEME ADI", "SATIS", "ADET", "TUTAR"
        } catch (SQLException ex) {
            Logger.getLogger(Soru3.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_ekleActionPerformed

    private void btn_eklenentoplamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_eklenentoplamActionPerformed
        // TODO add your handling code here:
        int toplam = 0;
        for (Object[] row : kayitbekleyen) {
            toplam += (int) row[7];
        }

        txt_toplameklenen.setText(toplam + "");
    }//GEN-LAST:event_btn_eklenentoplamActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:

        try {
            // TODO add your handling code here:

            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/DEPO", "sa", "as");
            String query = "INSERT INTO SATIS (ID,ID_MUSTERI,ID_MALZEME,TUTAR,ADET) VALUES (?,?,?,?,?)";
            PreparedStatement stm = con.prepareStatement(query);
            for (Object[] mrow : kayitbekleyen) {

                stm.setInt(1, (int) mrow[0]);
                stm.setInt(2, (int) mrow[2]);
                stm.setInt(3, (int) mrow[4]);
                stm.setInt(4, (int) mrow[6]);
                stm.setInt(5, (int) mrow[7]);
                stm.executeUpdate();
            }

            kayitbekleyen.clear();
        } catch (SQLException ex) {
            Logger.getLogger(Soru2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Soru3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Soru3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Soru3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Soru3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Soru3().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_ekle;
    private javax.swing.JButton btn_eklenentoplam;
    private javax.swing.JButton btn_musterisatisoku;
    private javax.swing.JButton btn_satilmayanmalzeme;
    private javax.swing.JButton btn_satisekle;
    private javax.swing.JButton btn_satissil;
    private javax.swing.JButton btn_sorgula;
    private javax.swing.JButton btn_toplamtutaryaz;
    private javax.swing.JComboBox<String> cmb_malzeme;
    private javax.swing.JComboBox<String> cmb_musteri;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbl_liste;
    private javax.swing.JTextField txt_adet;
    private javax.swing.JTextField txt_toplameklenen;
    private javax.swing.JTextField txt_tutar;
    // End of variables declaration//GEN-END:variables
}