narsil/Tanshu.Accounts.PointOfSale/Sales/ReorderTableForm.cs

127 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Helpers;
using System.Diagnostics;
using Tanshu.Accounts.Repository;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ReorderTableForm : Form
{
private readonly IList<FoodTable> _tableList;
public ReorderTableForm()
{
InitializeComponent();
using (var bi = new FoodTableBI())
{
_tableList = bi.List();
}
Selection = null;
}
private void button_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
var button = (Button)sender;
button.DoDragDrop(button, DragDropEffects.Move);
}
}
private void flpTables_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void flpTables_DragDrop(object sender, DragEventArgs e)
{
Button button = (Button)e.Data.GetData(typeof(Button));
Point p = flpTables.PointToClient(new Point(e.X, e.Y));
var item = flpTables.GetChildAtPoint(p);
int index = flpTables.Controls.GetChildIndex(item, false);
flpTables.Controls.SetChildIndex(button, index);
flpTables.Invalidate();
var foodTable = (FoodTable)button.Tag;
var listIndex = _tableList.IndexOf(foodTable);
var newIndex = _tableList.IndexOf((FoodTable)((Button)item).Tag);
_tableList.Remove(foodTable);
_tableList.Insert(index - 1, foodTable);
}
public FoodTable Selection { get; private set; }
private void MoveTableForm_Load(object sender, EventArgs e)
{
GenerateTables(new Point(75, 75));
}
private void tableButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var item = button.Tag as FoodTable;
Selection = item;
using (var frm = new TableForm(item.FoodTableID))
{
frm.ShowDialog();
}
}
private void GenerateTables(Point size)
{
var saveButton = new Button()
{
Name = "saveButton",
Text = "Save",
Width = size.X,
Height = size.Y,
};
saveButton.Click += saveButton_Click;
flpTables.Controls.Add(saveButton);
for (int i = 0; i < _tableList.Count; i++)
{
var item = _tableList[i];
var control = new Button()
{
Name = string.Format("g{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = item,
BackColor = item.IsActive ? Color.Green : Color.Red
};
control.MouseDown += button_MouseDown;
control.Click += tableButton_Click;
flpTables.Controls.Add(control);
}
var addButton = new Button()
{
Name = "addButton",
Text = "Add Table",
Width = size.X,
Height = size.Y,
};
addButton.Click += addButton_Click;
flpTables.Controls.Add(addButton);
}
private void addButton_Click(object sender, EventArgs e)
{
using (var frm = new TableForm())
{
frm.ShowDialog();
}
}
private void saveButton_Click(object sender, EventArgs e)
{
using (var bi = new FoodTableBI())
{
bi.UpdateSortOrder(_tableList);
bi.SaveChanges();
}
MessageBox.Show("Order Updated");
}
}
}