135 lines
4.0 KiB
C#
135 lines
4.0 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;
|
||
using Diplom_B.DB;
|
||
|
||
namespace Diplom_B
|
||
{
|
||
public partial class StatForm : Form
|
||
{
|
||
public StatForm()
|
||
{
|
||
InitializeComponent();
|
||
Init();
|
||
UpdateTable(WorkDB.ListStatus());
|
||
}
|
||
|
||
private void Init()
|
||
{
|
||
if (Program.user == null) this.Close();
|
||
{
|
||
deleteButton.Enabled = Program.user.Usr.Set > 2;
|
||
createButton.Enabled = Program.user.Usr.Set > 2;
|
||
changeButton.Enabled = Program.user.Usr.Set > 1;
|
||
}
|
||
}
|
||
|
||
private void UpdateTable(Status[] arr, bool reset_cursor = false)
|
||
{
|
||
var selected = (!reset_cursor && statGridView.SelectedRows.Count > 0) ? statGridView.SelectedRows[0].Index : -1;
|
||
{
|
||
var r = statGridView.Rows;
|
||
while (r.Count > 0)
|
||
r.Remove(r[0]);
|
||
var c = statGridView.Columns;
|
||
while (c.Count > 0)
|
||
c.Remove(c[0]);
|
||
}
|
||
{
|
||
var c = statGridView.Columns;
|
||
c.Add("Id", "№");
|
||
c["Id"].Width = 40;
|
||
c.Add("Name", "Наим.");
|
||
c["Name"].Width = 100;
|
||
}
|
||
{
|
||
var r = statGridView.Rows;
|
||
foreach (var stat in arr)
|
||
r.Add(new object[] {
|
||
stat.Id,
|
||
stat.Stat,
|
||
});
|
||
}
|
||
if (statGridView.Rows.Count > 0)
|
||
statGridView.Rows[0].Selected = true;
|
||
if (selected != -1 && selected < statGridView.Rows.Count)
|
||
for (var i = 0; i < statGridView.Rows.Count; i++)
|
||
statGridView.Rows[i].Selected = (i == selected);
|
||
statGridView_CurrentCellChanged(this, new EventArgs());
|
||
}
|
||
|
||
private Task errDrop;
|
||
private void ShowError(string msg = null)
|
||
{
|
||
errorLable.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
|
||
errorLable.Visible = true;
|
||
errDrop = new Task(() =>
|
||
{
|
||
var fd = errDrop.Id;
|
||
Task.Delay(5000).Wait();
|
||
if (errDrop.Id == fd)
|
||
if (InvokeRequired) Invoke((Action)(() => { errorLable.Visible = false; }));
|
||
else errorLable.Visible = false;
|
||
});
|
||
errDrop.Start();
|
||
}
|
||
|
||
private void statGridView_CurrentCellChanged(object sender, EventArgs e)
|
||
{
|
||
if (statGridView.SelectedRows.Count != 1)
|
||
return;
|
||
{
|
||
var stat = WorkDB.GetStatus((int)statGridView.SelectedRows[0].Cells[0].Value);
|
||
if (stat == null)
|
||
return;
|
||
nameBox.Text = stat.Stat;
|
||
}
|
||
}
|
||
|
||
private void createButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Статус пустой."); return; }
|
||
if (WorkDB.GetStatusList().Contains(nameBox.Text)) { ShowError("Статус существует."); return; }
|
||
|
||
try
|
||
{
|
||
var r = new Status() { Stat = nameBox.Text };
|
||
WorkDB.AddStatus(r);
|
||
UpdateTable(WorkDB.ListStatus());
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void changeButton_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (statGridView.SelectedRows.Count != 1) { ShowError("Статус не выбран."); return; }
|
||
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Название пустое."); return; }
|
||
var stat = WorkDB.GetStatus((int)statGridView.SelectedRows[0].Cells[0].Value);
|
||
if (stat.Stat != nameBox.Text && WorkDB.GetStatusList().Contains(nameBox.Text)) { ShowError("Статус существует."); return; }
|
||
stat.Stat = nameBox.Text;
|
||
WorkDB.ChangeStatus(stat);
|
||
UpdateTable(WorkDB.ListStatus());
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void deleteButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (statGridView.SelectedRows.Count != 1) { ShowError("Статус не выбран."); return; }
|
||
if (WorkDB.ListStatus().Length <= 1) { ShowError("Нельзя удалить."); return; }
|
||
if (WorkDB.GetPostavkyFromStatus((int)statGridView.SelectedRows[0].Cells[0].Value).Length > 0) { ShowError("Есть связи с поставками."); return; }
|
||
var stat = WorkDB.GetStatus((int)statGridView.SelectedRows[0].Cells[0].Value);
|
||
if (stat == null) { ShowError("Статуса нет."); return; }
|
||
try { WorkDB.DeleteStatus(stat); }
|
||
catch { ShowError(); }
|
||
UpdateTable(WorkDB.ListStatus());
|
||
}
|
||
}
|
||
}
|