using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace WpfApplication19
{
	/// <summary>
	/// Interaction logic for ChooseRecord2.xaml
	/// </summary>
	public partial class ChooseRecord2 : Window
	{
		private string _lotNo;
		private SqlConnection _proConn;
		private ObservableCollection<RecordSelection> _recordList = new ObservableCollection<RecordSelection>();
		
		private const int GWL_STYLE = -16;
		private const int WS_SYSMENU = 0x80000;
		[DllImport("user32.dll", SetLastError = true)]
		private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
		[DllImport("user32.dll")]
		private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);


		public ChooseRecord2(string lotNo)
		{
			
			InitializeComponent();
			
			_lotNo = lotNo;
			try
			{
				_proConn = Utils.createSqlConnection("productionDbCS");
			}
			catch (SqlException se)
			{
				MessageBox.Show("productionDbCS : ", se.Message);
			}
			setUpGrid();
		}
		private void setUpGrid(){
			/*
			SqlDataReader reader = Utils.Query(_proConn, "select * from data where lot_no = '" + this.LotNo + "' group by updated_at");
			while (reader.Read())
			{

				DateTimeCB.Items.Add(new ComboBoxItem() { Tag = "a", Content = reader["updated_at"].ToString() });
			}
			reader.Close();*/
			SqlDataReader reader = Utils.Query(_proConn, "select * from lot_summary where lot_no = '"+_lotNo+"' order by created_at desc");
			while(reader.Read()){
					
					_recordList.Add(new RecordSelection(){ lotNo = reader["lot_no"].ToString(), 
					code = reader["code"].ToString(),
					date = Convert.ToDateTime(reader["created_at"]),
					avg = Convert.ToDouble(reader["avg"])});
			}
			chooseGrid.ItemsSource = _recordList;
			reader.Close();
		}

		private void selectBtn_Click(object sender, RoutedEventArgs e)
		{
			RecordSelection r =  chooseGrid.SelectedItem as RecordSelection;
			var w = (MainWindow)Application.Current.MainWindow;
			try{
			w.setFilterLogData(r.date.ToString());
			Close();
			}catch{
				MessageBox.Show("Please Select A Record");
			}
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			var hwnd = new WindowInteropHelper(this).Handle;
			SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

		}

		private void cancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
		
	}
	class RecordSelection{
		public string lotNo { get; set; }
		public string code { get; set; }
		public DateTime date { get; set; }
		public double avg { get; set; }
	}
}
