133 lines
4.1 KiB
C#
133 lines
4.1 KiB
C#
namespace C968Project.Views;
|
|
|
|
public partial class MainScreen : Form
|
|
{
|
|
public MainScreen()
|
|
{
|
|
InitializeComponent();
|
|
|
|
productsDataGridView.DataSource = Program.Inventory.Products;
|
|
partsDataGridView.DataSource = Program.Inventory.Parts;
|
|
}
|
|
|
|
private void exitButton_Click(object sender, EventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
|
|
// --- Parts
|
|
private void partsAddButton_Click(object sender, EventArgs e)
|
|
{
|
|
AddModifyPartScreen partScreen = new AddModifyPartScreen(ScreenOption.ADD);
|
|
partScreen.Show();
|
|
}
|
|
|
|
private void partsModifyButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (partsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex);
|
|
if (part is null) return;
|
|
|
|
AddModifyPartScreen partScreen = new AddModifyPartScreen(ScreenOption.MODIFY, part);
|
|
partScreen.Show();
|
|
}
|
|
|
|
private void partsDeleteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (partsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex);
|
|
if (part is null) return;
|
|
|
|
if (CheckPartForProductAssociation(part)) return;
|
|
if (Helpers.ConfirmDelete()) Program.Inventory.DeletePart(part);
|
|
}
|
|
|
|
private bool CheckPartForProductAssociation(Part part)
|
|
{
|
|
foreach (var product in Program.Inventory.Products)
|
|
{
|
|
if (product.AssociatedParts.Contains(part))
|
|
{
|
|
MessageBox.Show($"Cannot delete part as it is associated with product: {product.Name}", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void partsSearchButton_Click(object sender, EventArgs e)
|
|
{
|
|
// TODO: Search functionality
|
|
}
|
|
|
|
// --- Products
|
|
private void productsAddButton_Click(object sender, EventArgs e)
|
|
{
|
|
AddModifyProductScreen productScreen = new AddModifyProductScreen(ScreenOption.ADD);
|
|
productScreen.Show();
|
|
}
|
|
|
|
private void productsModifyButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (productsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Product product = Program.Inventory.LookupProduct(productsDataGridView.CurrentCell.RowIndex);
|
|
if (product is null) return;
|
|
|
|
AddModifyProductScreen productScreen = new AddModifyProductScreen(ScreenOption.MODIFY, product);
|
|
productScreen.Show();
|
|
}
|
|
|
|
private void productsDeleteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (productsDataGridView.CurrentCell is null)
|
|
{
|
|
MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
Product product = Program.Inventory.LookupProduct(productsDataGridView.CurrentCell.RowIndex);
|
|
if (product is null) return;
|
|
|
|
if (CheckProductForPartAssociations(product)) return;
|
|
if (Helpers.ConfirmDelete()) Program.Inventory.RemoveProduct(Program.Inventory.Products.IndexOf(product));
|
|
}
|
|
|
|
private bool CheckProductForPartAssociations(Product product)
|
|
{
|
|
if (product.AssociatedParts.Count == 0) return false;
|
|
|
|
string associatedParts = "";
|
|
|
|
foreach (var part in product.AssociatedParts)
|
|
{
|
|
associatedParts += part.Name + "\n";
|
|
}
|
|
|
|
MessageBox.Show($"Cannot delete product as it has association with parts:\n{associatedParts}", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
|
|
return true;
|
|
}
|
|
|
|
private void productsSearchButton_Click(object sender, EventArgs e)
|
|
{
|
|
// TODO: Search functionality
|
|
}
|
|
|
|
|
|
} |