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;

namespace Project.Dialog
{
	public partial class fImportSIDConv : Form
	{
		public fImportSIDConv()
		{
			InitializeComponent();
		}

		private void component_Reel_SIDConvBindingNavigatorSaveItem_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.bs.EndEdit();
			this.tam.UpdateAll(this.dataSet1);

		}

		private void fImportSIDConv_Load(object sender, EventArgs e)
		{
			// TODO: 이 코드는 데이터를 'dataSet1.Component_Reel_SIDConv' 테이블에 로드합니다. 필요 시 이 코드를 이동하거나 제거할 수 있습니다.
			this.ta.Fill(this.dataSet1.Component_Reel_SIDConv);
		}

		private void btImpo_Click(object sender, EventArgs e)
		{
			//엑셀을 읽어와서 처리한다.
			var od = new OpenFileDialog();
			od.RestoreDirectory = true;
			od.Filter = "모든파일(*.*)|*.*";
			if (od.ShowDialog() != DialogResult.OK) return;
			libxl.Book book;
			var fi = new System.IO.FileInfo(od.FileName);
			var ext = fi.Extension.ToLower();
			if (ext == ".xlsx") book = new libxl.XmlBook();
			else if (ext == ".xls") book = new libxl.BinBook();
			else
			{
				Util.MsgE("지원되지 않는 파일 입니다");
				return;
			}

			var keyinfo = Properties.Settings.Default.libxl.Split('/');
			book.setKey(keyinfo[0], keyinfo[1]);
			book.load(fi.FullName);
			var sheet = book.getSheet(0);


			var cs = sheet.firstCol();
			var ce = sheet.lastCol();
			var rs = sheet.firstRow();
			var re = sheet.lastRow();

			this.progressBar1.Minimum = 0;
			this.progressBar1.Maximum = re;
			this.progressBar1.Value = 0;

			int cntA = 0;
			int cntU = 0;
			for (int row = rs; row <= re; row++)
			{
				if (this.progressBar1.Value < this.progressBar1.Maximum)
					this.progressBar1.Value += 1;
				var c1 = sheet.readStr(row, 0);
				var c2 = sheet.readStr(row, 1);
				if (c1.StartsWith("10") || c2.StartsWith("10"))
				{
					var M101 = string.Empty;
					var M103 = string.Empty;
					var M106 = string.Empty;

					if (c1.StartsWith("101")) M101 = c1.Trim();
					if (c1.StartsWith("103")) M103 = c1.Trim();
					if (c1.StartsWith("106")) M106 = c1.Trim();

					if (c2.StartsWith("101")) M101 = c2.Trim();
					if (c2.StartsWith("103")) M103 = c2.Trim();
					if (c2.StartsWith("106")) M106 = c2.Trim();

					if (M101.isEmpty() == false)
					{
						var plist = this.dataSet1.Component_Reel_SIDConv.Where(t => t.M101 == M101);
						if (plist.Any() == false)
						{
							var newdr = this.dataSet1.Component_Reel_SIDConv.NewComponent_Reel_SIDConvRow();
							newdr.M101 = M101;
							newdr.M103 = M103;
							newdr.M106 = M106;
							this.dataSet1.Component_Reel_SIDConv.AddComponent_Reel_SIDConvRow(newdr);
							cntA += 1;
						}
						else
						{
							foreach (var item in plist)
							{
								if (M103.isEmpty() == false) item.M103 = M103;
								if (M106.isEmpty() == false) item.M106 = M106;
								item.EndEdit();
								cntU += 1;
							}
						}
					}
				}
				else if (c1.isEmpty() && c2.isEmpty()) break; //
			}
			Util.MsgI(string.Format("추가:{0},변경:{1}", cntA, cntU));

		}

		private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				var search = this.tbFind.Text.Trim();
				if (search.isEmpty())
				{
					this.bs.Filter = "";
					this.tbFind.BackColor = SystemColors.Window;
				}
				else
				{
					var list = new string[] { "M101", "M103", "M106" };
					var filter = string.Join(" like '%{0}%' or ", list);
					filter += " like '%{0}%'";
					filter = string.Format(filter, search.Replace("'", "''"));
					try
					{
						this.bs.Filter = filter;
						this.tbFind.BackColor = Color.Lime;
						this.tbFind.SelectAll();
						this.tbFind.Focus();
					}
					catch (Exception ex)
					{
						this.tbFind.BackColor = Color.Tomato;
						Pub.log.AddE("sid 변환 테이블 필터 오류 : " + ex.Message);
					}
				}
			}
		}

	}
}
