169 lines
5.6 KiB
C#
169 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace C968Project.Views
|
|
{
|
|
public partial class AddModifyProductScreen : Form
|
|
{
|
|
private ScreenOption _screenOption;
|
|
private Product? _currentProduct;
|
|
|
|
|
|
public AddModifyProductScreen(ScreenOption screenOption, Product? product = null)
|
|
{
|
|
InitializeComponent();
|
|
this._screenOption = screenOption;
|
|
_currentProduct = product;
|
|
|
|
if (_screenOption is ScreenOption.ADD)
|
|
{
|
|
screenLabel.Text = "Add Product";
|
|
_currentProduct = new();
|
|
associatedPartsDataGridView.DataSource = _currentProduct.AssociatedParts;
|
|
}
|
|
if (_screenOption is ScreenOption.MODIFY)
|
|
{
|
|
screenLabel.Text = "Modify Product";
|
|
PopulateProductData(_currentProduct);
|
|
}
|
|
|
|
allPartsDataGridView.DataSource = Program.Inventory.Parts;
|
|
}
|
|
|
|
private void PopulateProductData(Product product)
|
|
{
|
|
idTextBox.Text = product.ProductId.ToString();
|
|
nameTextBox.Text = product.Name;
|
|
inventoryTextBox.Text = product.InStock.ToString();
|
|
priceTextBox.Text = product.Price.ToString();
|
|
minTextBox.Text = product.Min.ToString();
|
|
maxTextBox.Text = product.Max.ToString();
|
|
associatedPartsDataGridView.DataSource = product.AssociatedParts;
|
|
}
|
|
|
|
private Product CreateProduct(int productId)
|
|
{
|
|
Product product = new();
|
|
|
|
product.ProductId = productId;
|
|
product.Name = nameTextBox.Text;
|
|
product.InStock = int.Parse(inventoryTextBox.Text);
|
|
product.Price = float.Parse(priceTextBox.Text);
|
|
product.Min = int.Parse(minTextBox.Text);
|
|
product.Max = int.Parse(maxTextBox.Text);
|
|
|
|
var dataGridViewData = associatedPartsDataGridView.DataSource;
|
|
if (dataGridViewData != null)
|
|
{
|
|
if (dataGridViewData is BindingList<Part> partsList)
|
|
{
|
|
product.AssociatedParts = partsList;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Error generating Associated Parts: DataGridView DataSource is not of type BindingList<Part> somehow. Defaulting to an empty list.", "Data Type Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
product.AssociatedParts = new();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
product.AssociatedParts = new();
|
|
}
|
|
|
|
return product;
|
|
}
|
|
|
|
private void AddProduct()
|
|
{
|
|
int newId = Program.CreateNewProductId();
|
|
Program.Inventory.AddProduct(CreateProduct(newId));
|
|
}
|
|
|
|
private void ModifyProduct()
|
|
{
|
|
int productIndex = 0;
|
|
if (_currentProduct != null && Program.Inventory.Products.Contains(_currentProduct))
|
|
{
|
|
productIndex = Program.Inventory.Products.IndexOf(_currentProduct);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Error modifying product: Could not find product index", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
var oldProduct = Program.Inventory.Products[productIndex];
|
|
var oldProductId = oldProduct.ProductId;
|
|
var modifiedProduct = CreateProduct(oldProductId);
|
|
Program.Inventory.Products[productIndex] = modifiedProduct;
|
|
}
|
|
|
|
private void saveButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (_screenOption is ScreenOption.ADD)
|
|
{
|
|
AddProduct();
|
|
Close();
|
|
}
|
|
|
|
if (_screenOption is ScreenOption.MODIFY)
|
|
{
|
|
ModifyProduct();
|
|
Close();
|
|
}
|
|
}
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
private void addButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (allPartsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Part part = Program.Inventory.LookupPart(allPartsDataGridView.CurrentCell.RowIndex);
|
|
if (part is null) return;
|
|
|
|
|
|
|
|
if (_currentProduct.AssociatedParts.Contains(part))
|
|
{
|
|
MessageBox.Show("Part already added", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
_currentProduct.AddAssociatedPart(part);
|
|
}
|
|
|
|
private void deleteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (associatedPartsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Part part = _currentProduct.LookupAssociatedPart(associatedPartsDataGridView.CurrentCell.RowIndex);
|
|
|
|
if (part is null || !_currentProduct.AssociatedParts.Contains(part))
|
|
{
|
|
MessageBox.Show("Error finding associated part from selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
_currentProduct.RemoveAssociatedPart(_currentProduct.AssociatedParts.IndexOf(part));
|
|
}
|
|
}
|
|
}
|