Diplom_O/MainForms/ShtatForm.cs
2024-09-20 06:53:52 +03:00

155 lines
3.5 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_O.DataBase;
namespace Diplom_O
{
public partial class ShtatForm : Form
{
private readonly ColumnConf[] tableColumns =
{
new ColumnConf("№"),
new ColumnConf("Должность", 140),
new ColumnConf(name: "Кол-во"),
new ColumnConf(name: "Своб.")
};
private void findBox_TextChanged(object sender, EventArgs e)
{
this.FindOnChanged(resetShatTable);
}
private void dropFindButton_Click(object sender, EventArgs e)
{
findBox.Text = "";
this.FindOnChanged(null);
resetShatTable();
}
private void resetShatTable()
{
try
{
var tmp = FuncDB.ShtatListTableMain(findBox.Text, freeShtatCheckBox.Checked);
var res = new List<string[]>();
foreach (var i in tmp)
if (int.TryParse(i[0], out int t) && t != 0)
res.Add(i);
this.ResetTable(
shtatGridView,
tableColumns,
freeShtatCheckBox.Checked ?
tmp :
res.ToArray()
);
}
catch ( Exception e )
{
this.ShowError( errorLabel, e.Message );
}
}
private Shtat selectedShtat()
{
try
{
if ( shtatGridView.SelectedRows.Count != 1 )
throw new Exception("Должность не выбрана.");
if (int.TryParse((string)shtatGridView.SelectedRows[0].Cells[0].Value, out int value))
return FuncDB.ShtatGetById(value);
else
throw new Exception("Ошибка в таблице.");
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
return null;
}
}
public bool isCanceled = true;
public Shtat formResult = null;
public ShtatForm(bool select = false)
{
try
{
InitializeComponent();
selectButton.Visible = select;
shtatMI.Enabled = !select;
this.correctUpperMenu(menuStrip, 0);
resetShatTable();
addButton.Enabled =
changeButton.Enabled =
delButton.Enabled = false;
var user = this.GetUser();
if (user == null)
return;
delButton.Enabled = FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 0, 1) != null;
changeButton.Enabled = FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 0, 2) != null;
addButton.Enabled = FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 0, 3) != null;
}
catch { this.Close(); }
}
private void addButton_Click(object sender, EventArgs e)
{
var res = new SupportForms.WorkShtatForm();
res.ShowDialog();
if (!res.isCanceled)
resetShatTable();
}
private void changeButton_Click(object sender, EventArgs e)
{
var shtat = selectedShtat();
if (shtat == null)
return;
var res = new SupportForms.WorkShtatForm(shtat);
res.ShowDialog();
if (!res.isCanceled)
resetShatTable();
}
private void delButton_Click(object sender, EventArgs e)
{
var shtat = selectedShtat();
if (shtat == null)
return;
try
{
FuncDB.ShtatDelete(shtat);
resetShatTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void selectButton_Click(object sender, EventArgs e)
{
formResult = selectedShtat();
isCanceled = false;
this.Close();
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
resetShatTable();
}
private void upperMenuClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.upperMenuClicked((MenuStrip)sender);
if (this.GetNextForm() != -1)
Close();
}
}
}