#pragma once
#include <Windows.h>
#include "Global.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Data::SqlClient;

namespace ObjDataEdit
{
	/// <summary> 
	/// Summary for SetAccount
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public __gc class SetAccount : public System::Windows::Forms::Form
	{
	public: 
		SetAccount(void)
		{
			InitializeComponent();

			sqlConnection->ConnectionString = Global::GetDBSource();
            sqlConnection->Open();            
			
		}
        
	protected: 
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
    private: System::Windows::Forms::Button *  button2;
    private: System::Windows::Forms::Button *  button1;


    private: System::Windows::Forms::Label *  label2;
    private: System::Windows::Forms::Label *  label1;

    private: System::Windows::Forms::Label *  label3;
    private: System::Windows::Forms::GroupBox *  groupBox1;
    private: System::Windows::Forms::TextBox *  TB_Password1;
    private: System::Windows::Forms::TextBox *  TB_Account;
    private: System::Windows::Forms::TextBox *  TB_Password2;
    protected: System::Data::SqlClient::SqlCommand *  sqlCommand;
    protected: System::Data::SqlClient::SqlConnection *  sqlConnection;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container* components;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
            this->button2 = new System::Windows::Forms::Button();
            this->button1 = new System::Windows::Forms::Button();
            this->TB_Password1 = new System::Windows::Forms::TextBox();
            this->TB_Account = new System::Windows::Forms::TextBox();
            this->label2 = new System::Windows::Forms::Label();
            this->label1 = new System::Windows::Forms::Label();
            this->TB_Password2 = new System::Windows::Forms::TextBox();
            this->label3 = new System::Windows::Forms::Label();
            this->groupBox1 = new System::Windows::Forms::GroupBox();
            this->sqlCommand = new System::Data::SqlClient::SqlCommand();
            this->sqlConnection = new System::Data::SqlClient::SqlConnection();
            this->groupBox1->SuspendLayout();
            this->SuspendLayout();
            // 
            // button2
            // 
            this->button2->DialogResult = System::Windows::Forms::DialogResult::Cancel;
            this->button2->Location = System::Drawing::Point(128, 120);
            this->button2->Name = S"button2";
            this->button2->Size = System::Drawing::Size(75, 24);
            this->button2->TabIndex = 11;
            this->button2->Text = S"取消";
            this->button2->Click += new System::EventHandler(this, &ObjDataEdit::SetAccount::Cancel_Click);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(40, 120);
            this->button1->Name = S"button1";
            this->button1->Size = System::Drawing::Size(75, 24);
            this->button1->TabIndex = 10;
            this->button1->Text = S"確定";
            this->button1->Click += new System::EventHandler(this, &ObjDataEdit::SetAccount::OK_Click);
            // 
            // TB_Password1
            // 
            this->TB_Password1->Location = System::Drawing::Point(88, 48);
            this->TB_Password1->MaxLength = 31;
            this->TB_Password1->Name = S"TB_Password1";
            this->TB_Password1->PasswordChar = '*';
            this->TB_Password1->Size = System::Drawing::Size(120, 22);
            this->TB_Password1->TabIndex = 9;
            this->TB_Password1->Text = S"";
            // 
            // TB_Account
            // 
            this->TB_Account->Location = System::Drawing::Point(88, 16);
            this->TB_Account->MaxLength = 31;
            this->TB_Account->Name = S"TB_Account";
            this->TB_Account->Size = System::Drawing::Size(120, 22);
            this->TB_Account->TabIndex = 8;
            this->TB_Account->Text = S"";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(40, 56);
            this->label2->Name = S"label2";
            this->label2->Size = System::Drawing::Size(42, 18);
            this->label2->TabIndex = 7;
            this->label2->Text = S"密碼：";
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(40, 24);
            this->label1->Name = S"label1";
            this->label1->Size = System::Drawing::Size(42, 18);
            this->label1->TabIndex = 6;
            this->label1->Text = S"帳號：";
            // 
            // TB_Password2
            // 
            this->TB_Password2->Location = System::Drawing::Point(88, 80);
            this->TB_Password2->MaxLength = 31;
            this->TB_Password2->Name = S"TB_Password2";
            this->TB_Password2->PasswordChar = '*';
            this->TB_Password2->Size = System::Drawing::Size(120, 22);
            this->TB_Password2->TabIndex = 13;
            this->TB_Password2->Text = S"";
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(16, 88);
            this->label3->Name = S"label3";
            this->label3->Size = System::Drawing::Size(66, 18);
            this->label3->TabIndex = 12;
            this->label3->Text = S"確認密碼：";
            // 
            // groupBox1
            // 
            this->groupBox1->Controls->Add(this->TB_Password1);
            this->groupBox1->Controls->Add(this->TB_Account);
            this->groupBox1->Controls->Add(this->label2);
            this->groupBox1->Controls->Add(this->label1);
            this->groupBox1->Controls->Add(this->TB_Password2);
            this->groupBox1->Controls->Add(this->label3);
            this->groupBox1->Location = System::Drawing::Point(8, 0);
            this->groupBox1->Name = S"groupBox1";
            this->groupBox1->Size = System::Drawing::Size(216, 112);
            this->groupBox1->TabIndex = 14;
            this->groupBox1->TabStop = false;
            // 
            // sqlCommand
            // 
            this->sqlCommand->Connection = this->sqlConnection;
            // 
            // sqlConnection
            // 
			this->sqlConnection->ConnectionString = S"workstation id=HSIANG;packet size=4096;user id=hsiangren;persist security info=True;initial catalog=ObjectData;password=2ddilxxl;data source=GS1";
            // 
            // SetAccount
            // 
            this->AutoScaleBaseSize = System::Drawing::Size(5, 15);
            this->ClientSize = System::Drawing::Size(232, 150);
            this->Controls->Add(this->groupBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = S"SetAccount";
            this->Text = S"新增帳號";
            this->Load += new System::EventHandler(this, &ObjDataEdit::SetAccount::SetAccount_Load);
            this->groupBox1->ResumeLayout(false);
            this->ResumeLayout(false);

        }		
    private: System::Void SetAccount_Load(System::Object *  sender, System::EventArgs *  e)
             {
             }



private: System::Void Cancel_Click(System::Object *  sender, System::EventArgs *  e)
         {
             Close();
         }

private: System::Void OK_Click(System::Object *  sender, System::EventArgs *  e)
         {
             if( TB_Account->Text->Length == 0 )
             {
                 MessageBox::Show( "帳號沒輸入" ,"錯誤" );
                 return;
             }
             else
             {
                 int Count = 0;                 
                 sqlCommand->CommandText = String::Format( "Select Account_ID from AccountDB where Account_ID = '{0}'" , TB_Account->Text );
                 SqlDataReader* myReader = sqlCommand->ExecuteReader();

                 while( myReader->Read())
                 {
                     Count++;
                 }
                 myReader->Close();
                 if( Count != 0 )
                 {
                     MessageBox::Show( "帳號重覆" ,"錯誤" );
                     return;
                 }
             }

             if( String::Compare( TB_Password1->Text , TB_Password2->Text ) != 0 )
             {
                 MessageBox::Show( "密碼輸入不相同 " ,"錯誤" );
                 return;
             }

             if( TB_Password1->Text->Length < 4 )
             {
                 MessageBox::Show( "密碼輸入至少要四個字" ,"錯誤" );
                 return;
             }

             sqlCommand->CommandText = String::Format( "INSERT INTO AccountDB (Account_id, Password) VALUES ('{0}', '{1}')" 
                 , TB_Account->Text , TB_Password1->Text ) ;

             sqlCommand->ExecuteReader()->Close();
             Close();
         }

};
}