using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XHandler.Class.DataEx;
using XImagingXhandler.XDAL;
using XCore;
using XCommon.Log;
namespace XHandler.View.BacteriaProperty
{
///
/// CoatingFileExportProperty.xaml 的交互逻辑
///
public partial class CoatingFileExportProperty : System.Windows.Controls.UserControl
{
public MethodCoatingReport methodCoatingReport { get; set; }
public MethodCoatingReport currmethodCoatingReport = null;
List dropdownNames = new List();
#region 变量
string isrun = "";
string status = "";
string name = "";
string label = "";
string filePath = "";
List reportColumns = new List();
string filePostfix = "";
string filename = "";
static int mark = 0;
#endregion
public CoatingFileExportProperty()
{
InitializeComponent();
}
public CoatingFileExportProperty(MethodEx method)
{
InitializeComponent();
mark = 0;
CoatingReportBll coatingReportBll = new CoatingReportBll();
dropdownNames = coatingReportBll.GenerateReportColumn();
BindReportColumn();
methodCoatingReport = new MethodCoatingReport();
methodCoatingReport.name = method.method_name;
methodCoatingReport.label = method.method_name;
methodCoatingReport.status = (method.isEnabled == true ? "enable" : "disable");
methodCoatingReport.strIndex = method.strIndex;
methodCoatingReport.reportColumns = new List();
foreach (DropdownName d in dropdownNames)
{
ReportColumn r = new ReportColumn();
r.reportcolumn_id = d.dropdown_id;
r.reportcolumn_name = d.dropdown_name;
r.reportcolumn_sname = d.dropdown_name;
methodCoatingReport.reportColumns.Add(r);
}
DataContext = methodCoatingReport;
if (method.tag != null)
{
methodCoatingReport = (MethodCoatingReport)method.tag;
currmethodCoatingReport = methodCoatingReport;
isrun = methodCoatingReport.isrun;
status = methodCoatingReport.status;
name = methodCoatingReport.name;
label = methodCoatingReport.label;
filePath = methodCoatingReport.filePath;
reportColumns = methodCoatingReport.reportColumns;
filePostfix = methodCoatingReport.filePostfix;
filename = methodCoatingReport.fileName;
}
}
System.Windows.Controls.CheckBox[] checkBoxes;
System.Windows.Controls.TextBox[] tboxs;
private void BindReportColumn()
{
//dropdownNames = DataExportBll.GenerateReportColumn();
gdReportColList.Children.Clear();
gdReportColList.RowDefinitions.Clear();
gdReportColList.ColumnDefinitions.Clear();
ColumnDefinition cd = new ColumnDefinition();
cd.Width = new GridLength(50);
gdReportColList.ColumnDefinitions.Add(cd);
cd = new ColumnDefinition();
cd.Width = new GridLength(50);
gdReportColList.ColumnDefinitions.Add(cd);
cd = new ColumnDefinition();
cd.Width = new GridLength(120);
gdReportColList.ColumnDefinitions.Add(cd);
cd = new ColumnDefinition();
cd.Width = new GridLength(120);
gdReportColList.ColumnDefinitions.Add(cd);
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(30);
gdReportColList.RowDefinitions.Add(rd);
TextBlock tb = new TextBlock();
tb.Text = "";
gdReportColList.Children.Add(tb);
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 0);
tb = new TextBlock();
tb.Text = "序号";
gdReportColList.Children.Add(tb);
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 1);
tb = new TextBlock();
tb.Text = "字段名";
gdReportColList.Children.Add(tb);
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 2);
tb = new TextBlock();
tb.Text = "列名";
gdReportColList.Children.Add(tb);
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 3);
checkBoxes = new System.Windows.Controls.CheckBox[dropdownNames.Count];
tboxs = new System.Windows.Controls.TextBox[dropdownNames.Count];
for (int i = 0; i < dropdownNames.Count; i++)
{
rd = new RowDefinition();
rd.Height = new GridLength(30);
gdReportColList.RowDefinitions.Add(rd);
checkBoxes[i] = new System.Windows.Controls.CheckBox();
checkBoxes[i].IsChecked = true;
checkBoxes[i].Tag = i;
gdReportColList.Children.Add(checkBoxes[i]);
checkBoxes[i].SetValue(Grid.RowProperty, i + 1);
checkBoxes[i].SetValue(Grid.ColumnProperty, 0);
checkBoxes[i].AddHandler(System.Windows.Controls.CheckBox.ClickEvent, new RoutedEventHandler(check_Click));
TextBlock tbk = new TextBlock();
tbk.Text = dropdownNames[i].dropdown_id;
tbk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
gdReportColList.Children.Add(tbk);
tbk.SetValue(Grid.RowProperty, i + 1);
tbk.SetValue(Grid.ColumnProperty, 1);
tbk = new TextBlock();
tbk.Text = dropdownNames[i].dropdown_name;
tbk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
gdReportColList.Children.Add(tbk);
tbk.SetValue(Grid.RowProperty, i + 1);
tbk.SetValue(Grid.ColumnProperty, 2);
tboxs[i] = new System.Windows.Controls.TextBox();
tboxs[i].Text = dropdownNames[i].dropdown_name;
tboxs[i].AddHandler(System.Windows.Controls.TextBox.TextChangedEvent, new TextChangedEventHandler(tbx_TextBoxChanged));
tboxs[i].Tag = i;
tboxs[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
tboxs[i].VerticalAlignment = VerticalAlignment.Top;
tboxs[i].Width = 120;
tboxs[i].Height = 30;
tboxs[i].Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF2F3F5"));
gdReportColList.Children.Add(tboxs[i]);
tboxs[i].SetValue(Grid.RowProperty, i + 1);
tboxs[i].SetValue(Grid.ColumnProperty, 3);
}
}
private void tbx_TextBoxChanged(object sender, TextChangedEventArgs e)
{
System.Windows.Controls.TextBox tb = (System.Windows.Controls.TextBox)sender;
if (checkBoxes[(int)tb.Tag].IsChecked.Equals(true))
{
int a = methodCoatingReport.reportColumns.FindLastIndex(x => x.reportcolumn_id == ((int)tb.Tag).ToString());
methodCoatingReport.reportColumns[a].reportcolumn_sname = tb.Text;
}
}
private void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
methodCoatingReport.filePath = dialog.SelectedPath;
textboxDestPath.Text = dialog.SelectedPath;
}
}
private void radioBtnXls_Checked(object sender, RoutedEventArgs e)
{
if (methodCoatingReport == null)
return;
methodCoatingReport.filePostfix = ".xls";
}
private void radioBtnCsv_Checked(object sender, RoutedEventArgs e)
{
if (methodCoatingReport == null)
return;
methodCoatingReport.filePostfix = ".csv";
}
private void textboxDestPath_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (methodCoatingReport != null)
{
methodCoatingReport.filePath = textboxDestPath.Text;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void tbxFileName_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (methodCoatingReport != null)
{
methodCoatingReport.fileName = tbxFileName.Text;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void tbxCommandName_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (methodCoatingReport != null)
{
methodCoatingReport.name = tbxCommandName.Text;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
mark += 1;
try
{
if (mark > 1)
{
return;
}
if (!string.IsNullOrEmpty(filePostfix))
{
tbxCommandName.Text = name;
tbxFileName.Text = filename;
textboxDestPath.Text = filePath;
if (filePostfix == ".xls")
{
radioBtnXls.IsChecked = true;
}
else if (filePostfix == ".csv")
{
radioBtnCsv.IsChecked = true;
}
List reportColumns = methodCoatingReport.reportColumns;
if (reportColumns != null)
{
for (int j = 0; j < checkBoxes.Length; j++)
{
var a = reportColumns.SingleOrDefault(x => x.reportcolumn_id.Equals((j).ToString()));
if (a != null)
{
checkBoxes[j].IsChecked = true;
tboxs[j].Text = a.reportcolumn_sname;
}
else
{
checkBoxes[j].IsChecked = false;
}
}
}
checkedHeaderByClickResult();
if (currmethodCoatingReport != null)
{
methodCoatingReport = currmethodCoatingReport;
}
}
else
{
if (methodCoatingReport == null)
return;
if (string.Compare(methodCoatingReport.filePostfix, ".csv", true) == 0)
radioBtnCsv.IsChecked = true;
else
radioBtnXls.IsChecked = true;
if (currmethodCoatingReport != null)
{
methodCoatingReport = currmethodCoatingReport;
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
#region 单个选中
private void check_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.CheckBox cbx = (System.Windows.Controls.CheckBox)sender;
if (cbx.IsChecked.Equals(true))
{
if (methodCoatingReport.reportColumns != null)
{
var a = methodCoatingReport.reportColumns.SingleOrDefault(x => x.reportcolumn_id == ((int)cbx.Tag).ToString());
if (a == null)
{
ReportColumn reportColumn = new ReportColumn();
reportColumn.reportcolumn_id = ((int)cbx.Tag).ToString();
reportColumn.reportcolumn_name = dropdownNames[(int)cbx.Tag].dropdown_name;
reportColumn.reportcolumn_sname = tboxs[(int)cbx.Tag].Text;
methodCoatingReport.reportColumns.Add(reportColumn);
}
}
}
else
{
if (methodCoatingReport.reportColumns != null)
{
var a = methodCoatingReport.reportColumns.SingleOrDefault(x => x.reportcolumn_id == ((int)cbx.Tag).ToString());
if (a != null)
{
methodCoatingReport.reportColumns.Remove(a);
}
}
}
checkedHeaderByClickResult();
}
#endregion
#region 检查当前是否已经全部勾选,如果是则把表头的也勾选;否则不勾选
private void checkedHeaderByClickResult()
{
int total = 0;
for (int i = 0; i < checkBoxes.Count(); i++)
{
if (checkBoxes[i].IsChecked == true)
{
total++;
}
else
{
}
}
if (total == 11)
{
ckbSelectedAll.IsChecked = true;
}
else
{
ckbSelectedAll.IsChecked = false;
}
}
#endregion
private void ckbSelectedAll_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < checkBoxes.Count(); i++)
{
if (ckbSelectedAll.IsChecked == true)
{
checkBoxes[i].IsChecked = true;
}
else
{
checkBoxes[i].IsChecked = false;
}
}
}
}
}