#include "StdAfx.h"
#include "Global.h"
#include "BatchUpdateForm.h"
#include <stdio.h>
#include "DotNetString.h"
//Environment::MachineName

using namespace ObjDataEdit;

void BatchUpdateForm::Init( )
{
	g_BatchUpdateForm = new BatchUpdateForm;	
}

void	BatchUpdateForm::ShowForm( )
{
	g_BatchUpdateForm->LV_PreView->Columns->Clear();
	g_BatchUpdateForm->LV_PreView->Items->Clear();
	g_BatchUpdateForm->InsertStr->Text = "";

	g_BatchUpdateForm->_IsAccept = false;
	g_BatchUpdateForm->ShowDialog();	
}

System::Void BatchUpdateForm::PreView_Click(System::Object*  sender, System::EventArgs*  e)
{
	try
	{
		int		ColCount = 0;
		String* delimStr = ",\t";

		String* Str = InsertStr->Lines[ 0 ];
		String* split[] = 0;

		//////////////////////////////////////////////////////////////////////////
		// 欄位名稱
		split = Str->Split( delimStr->ToCharArray() , 500 );

		if( split->Length <= 1 )
			return;
		if( String::Compare( split[0] , "GUID" ) != 0 )
		{
			MessageBox::Show( "第一個欄位一定要是 GUID" ,"錯誤" );
			return;
		}
		ColCount = split->Length;

		LV_PreView->Columns->Clear();
		LV_PreView->Items->Clear();

		System::Windows::Forms::ColumnHeader* ColTemp[] = new System::Windows::Forms::ColumnHeader*[ split->Length ];
		for( int i = 0 ; i < split->Length ; i++ )
		{
			ColTemp[i] = (new System::Windows::Forms::ColumnHeader());
			ColTemp[i]->Text = split[i];
		}
		LV_PreView->Columns->AddRange( ColTemp );
		//////////////////////////////////////////////////////////////////////////
		//內容
		for( int i = 1 ; i < InsertStr->Lines->Length ; i++ )
		{
			Str = InsertStr->Lines[ i ];
	

			split = Str->Split( delimStr->ToCharArray() , ColCount );
			for (int j = 0 ; j < split->Length ; j ++)
			{
				split[j] = split[j]->Replace("<n>","\n");
			}
			if( split->Length != ColCount )
				continue;

			ListViewItem*  NewListViewItem = new ListViewItem( split );
			LV_PreView->Items->Add( NewListViewItem );

		}
	}
	catch(...)
	{

	}
}