content: package Interfaces;
  
  //Imports
  import javax.swing.BorderFactory;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.border.Border;
  import javax.swing.border.EmptyBorder;
  import javax.swing.table.DefaultTableModel;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  //Java Class Imports
  import Interfaces.StockPurchases;
  import controller.salesController;
  import db.DBConnection;
  import dto.salesdto;
  
  
  public class StockPurchases extends JFrame{
  
      private JTable viewTable;
      private DefaultTableModel dtm;
  
      private salesController salesController;
  
      public StockPurchases(){
  
          salesController = new salesController();
  
          //JFrame Definitions
          setTitle("Grain Store Managment System"); //Title Changed
          setSize(1000, 700);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLayout(null);
          getContentPane().setBackground(Color.white);
  
          //JFrame Logo
          ImageIcon iconImage = new ImageIcon(getClass().getResource("assets/icon.png"));
          Image image = iconImage.getImage();
          setIconImage(image);
  
          //JFrame Background Image
          ImageIcon backgroundImageSet = new ImageIcon(getClass().getResource("Assets/sideView.png"));
          Image imageSet = backgroundImageSet.getImage();
          Image resizedImage = imageSet.getScaledInstance(1000, 700, Image.SCALE_SMOOTH);
          ImageIcon backgroundImage = new ImageIcon(resizedImage);
          JLabel backgroundImageSetter = new JLabel(backgroundImage);
          backgroundImageSetter.setBounds(100, 0, 1000, 700);
  
          //JFrame Logo Image
          ImageIcon logoImageSet = new ImageIcon(getClass().getResource("Assets/Logo.png"));
          Image logoSet = logoImageSet.getImage();
          Image resizedLogoImage = logoSet.getScaledInstance(200, 100, Image.SCALE_SMOOTH);
          ImageIcon logoImage = new ImageIcon(resizedLogoImage);
          JLabel logoImageSetter = new JLabel(logoImage);
          logoImageSetter.setBounds(40, 0, 200, 100);
  
          //JPanel for menu Box
          JPanel menuBox = new JPanel();
          menuBox.setBounds(10, 10, 250, 640);
          menuBox.setBackground(Color.WHITE);
  
          //JPanel for Body Box
          JPanel bodyBox = new JPanel();
          bodyBox.setBounds(260, 10, 710, 640);
          //bodyBox.setBackground(new Color(237, 235, 235));
          bodyBox.setBackground(Color.WHITE);
  
          //JPanel for Title Box
          JPanel titleBox = new JPanel();
          titleBox.setBounds(260, 0, 740, 75);
          titleBox.setBackground(new Color(172, 145, 127));
  
          Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
  
          //JLabel For Interface Title
          JLabel titleLabel = new JLabel("Stock Purchases");
          titleLabel.setBounds(310, 15, 400, 50);
          titleLabel.setBackground(new Color(237, 235, 235));
          titleLabel.setForeground(Color.WHITE);
          titleLabel.setFont(new Font("Arial", Font.BOLD, 40));
  
          //Buttons defined for DashBoard
          JButton dashBoardButton = new JButton("Dashboard");
          dashBoardButton.setBounds(10, 140, 250, 50);
          dashBoardButton.setBackground(Color.WHITE);
          dashBoardButton.setForeground(new Color(27, 17, 14));
          dashBoardButton.setFont(new Font("Arial", Font.BOLD, 20));
          dashBoardButton.setBorder(border);
  
          //Buttons defined for Stock Availability
          JButton availabilityButton = new JButton("Stock Availability");
          availabilityButton.setBounds(10, 220, 200, 50);
          availabilityButton.setBackground(new Color(237, 235, 235));
          availabilityButton.setForeground(Color.BLACK);
          availabilityButton.setFont(new Font("Arial", Font.BOLD, 20));
          availabilityButton.setBorder(border);
  
          //Buttons defined for Report Generation
          JButton reportButton = new JButton("Report");
          reportButton.setBounds(10, 300, 200, 50);
          reportButton.setBackground(new Color(237, 235, 235));
          reportButton.setForeground(Color.BLACK);
          reportButton.setFont(new Font("Arial", Font.BOLD, 20));
          reportButton.setBorder(border);
  
          //Buttons defined for manage Stocks
          JButton manageStocksButton = new JButton("Manage Stocks");
          manageStocksButton.setBounds(10, 380, 200, 50);
          manageStocksButton.setBackground(new Color(237, 235, 235));
          manageStocksButton.setForeground(Color.BLACK);
          manageStocksButton.setFont(new Font("Arial", Font.BOLD, 20));
          manageStocksButton.setBorder(border);
  
          //Buttons defined for manage Warehouses
          JButton manageWarehouseButton = new JButton("Manage Warehouses");
          manageWarehouseButton.setBounds(10, 460, 200, 50);
          manageWarehouseButton.setBackground(new Color(237, 235, 235));
          manageWarehouseButton.setForeground(Color.BLACK);
          manageWarehouseButton.setFont(new Font("Arial", Font.BOLD, 20));
          manageWarehouseButton.setBorder(border);
  
           //Buttons defined for Logout
          JButton logoutButton = new JButton("Logout");
          logoutButton.setBounds(10, 570, 200, 50);
          logoutButton.setBackground(new Color(237, 235, 235));
          logoutButton.setForeground(Color.BLACK);
          logoutButton.setFont(new Font("Arial", Font.BOLD, 20));
          logoutButton.setBorder(border);
  
          
  
          // Create a table model
          dtm = new DefaultTableModel();
  
          // Create a JTable using the model
          viewTable = new JTable(dtm);
  
          //Table Appearance Customizations
          viewTable.setFont(new Font("Arial",Font.PLAIN, 14));
          viewTable.setRowHeight(30);
          viewTable.setBackground(new Color(237, 235, 235));
          viewTable.setForeground(Color.BLACK);
          viewTable.setGridColor(Color.DARK_GRAY);
          viewTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
          viewTable.getTableHeader().setBackground(new Color(172, 145, 127));
          viewTable.getTableHeader().setForeground(Color.WHITE);
  
          
          
          // Add the table to a JScrollPane for scroll functionality
          JScrollPane scrollPane = new JScrollPane(viewTable);
          scrollPane.setBounds(300, 140, 600, 450);
          scrollPane.setBackground(new Color(237, 235, 235));
          scrollPane.getViewport().setBackground(new Color(237, 235, 235));
          scrollPane.setBorder(new EmptyBorder(0,0,0,0));
          
  
          //Event actions defined for Dashboard Button
          dashBoardButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new DashboardView().setVisible(true);
              }
          });
  
          //Event actions defined for StockAvailabitity Button
          availabilityButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new StockAvailability().setVisible(true);
              }
          });
  
          //Event actions defined for Reports Button
          reportButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new ReportGenerate().setVisible(true);
              }
          });
  
          //Event actions defined for manage Stocks Button
          manageStocksButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new ManageStocks().setVisible(true);
              }
          });
  
          //Event actions defined for manage Warehouse Button
          manageWarehouseButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new ManageWarehouse().setVisible(true);
              }
          });
  
          //Event actions defined for Logout Button
          logoutButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  dispose();
                  new HomeScreen().setVisible(true);
              }
          });
  
          
          //Add Elements to the Frame
          add(logoImageSetter);
          add(titleLabel);
          add(dashBoardButton);
          //add(availabilityButton);
          //add(reportButton);
          //add(manageStocksButton);
          //add(manageWarehouseButton);
          add(logoutButton);
          add(scrollPane);
          add(titleBox);
          add(menuBox);
          //add(bodyBox);
          add(backgroundImageSetter);
  
          loadOrders();
      } 
  
      private void loadOrders() {
                  try {
                      String[] columns = {"Stock Name", "Quantity(Kg)"};
                      DefaultTableModel dtm = new DefaultTableModel(columns, 0) {
                          @Override
                          public boolean isCellEditable(int row, int column) {
                              return false;
                          }
                      };
                      viewTable.setModel(dtm);
              
                      ArrayList<salesdto> sales = salesController.getAllsales();
                      for (salesdto sale : sales) {
                          String stockName = getNameById(sale.getStock_ID());
                          Object[] rowData = {
                              stockName != null ? stockName : "Unknown", // Fallback to "Unknown" if name is null
                              sale.getQuantity_obtained()
                          };
                          dtm.addRow(rowData);
                          dtm.addRow(rowData);
                      }
                  } catch (Exception ex) {
                      Logger.getLogger(AddNewStocks.class.getName()).log(Level.SEVERE, null, ex);
                      JOptionPane.showMessageDialog(this, ex.getMessage());
                  }
              }
  
              public String getNameById(String id) {
                  String name = null;
                  try {
                      // Establish database connection
                      Connection connection = DBConnection.getInstance().getConnection();
              
                      // SQL query to get the ID based on the name
                      String query = "SELECT Stock_name FROM stock WHERE Stock_ID = ?";
                      PreparedStatement preparedStatement = connection.prepareStatement(query);
              
                      // Set the name parameter
                      preparedStatement.setString(1, id.trim());
              
                      // Execute the query
                      ResultSet resultSet = preparedStatement.executeQuery();
              
                      // Retrieve the ID if a record is found
                      if (resultSet.next()) {
                          name = resultSet.getString("Stock_name");
                      }
                  } catch (SQLException e) {
                      e.printStackTrace();
                      JOptionPane.showMessageDialog(null, "Error retrieving name by id: " + e.getMessage());
                  }
                  return name; // Returns null if no record is found
              }
  
  }
  