﻿Imports System.Windows.Forms

Public Class ValueUI

	' Form Load
	Private Sub ChangeProfitMarginUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		TextBox1.Text = CDbl(getValue("ProfitMargin"))
		TextBoxCC.Text = toPercent(getCreditCardDiscount())
		TextBoxNormal.Text = toPercent(CDbl(getValue("NormalMember")))
		TextBoxGold.Text = toPercent(CDbl(getValue("GoldMember")))
		TextBoxTwo.Text = toPercent(CDbl(getValue("TwoMonth")))
		TextBoxThree.Text = toPercent(CDbl(getValue("ThreeMonth")))
		TextBoxFrd.Text = toPercent(CDbl(getValue("Friend")))
	End Sub

	Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

		If TextBox1.Text = "" Or TextBoxCC.Text = "" Or TextBoxNormal.Text = "" Or TextBoxGold.Text = "" Or TextBoxTwo.Text = "" Or TextBoxThree.Text = "" Or TextBoxFrd.Text = "" Then
			MsgBox("Please fill in all fields!")
			Return
		End If

		setValue("ProfitMargin", TextBox1.Text)
		setValue("CreditCardPayment", toDouble(TextBoxCC.Text))
		setValue("NormalMember", toDouble(TextBoxNormal.Text))
		setValue("GoldMember", toDouble(TextBoxGold.Text))
		setValue("TwoMonth", toDouble(TextBoxTwo.Text))
		setValue("ThreeMonth", toDouble(TextBoxThree.Text))
		setValue("Friend", toDouble(TextBoxFrd.Text))

		Me.DialogResult = System.Windows.Forms.DialogResult.OK
		Me.Close()
	End Sub

	Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
		Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
		Me.Close()
	End Sub

	' Update
	Private Sub setValue(ByVal attr As String, ByVal value As String)
		db.query("UPDATE Options SET OValue = '" & value & "' WHERE OName = '" & attr & "'")
	End Sub

	' Get Value
	Shared Function getValue(ByVal attr As String)
		Dim result = db.query("SELECT * FROM Options WHERE OName = '" & esc(attr) & "'")
		result.nextRow()
		Return result.readAttr("OValue")
	End Function
	Shared Function getCreditCardDiscount() As Double
		Return CDbl(getValue("CreditCardPayment"))
	End Function
	Shared Function getNormalMemberDiscount() As Double
		Return CDbl(getValue("NormalMember"))
	End Function
	Shared Function getGoldMemberDiscount() As Double
		Return CDbl(getValue("GoldMember"))
	End Function

	' To Discount Precentage
	Shared Function toPercent(ByVal value As Double) As Double
		Return (1 - value) * 100
	End Function
	Shared Function toDouble(ByVal value As Double) As Double
		Return (100 - value) / 100
	End Function

	' Member toString
	Shared Function memberToString(ByVal i) As String
		Select Case i
			Case 0
				Return "Non-member"
			Case 1
				Return "Normal Member"
			Case 2
				Return "Gold member"
			Case Else
				Return "<Unkown Membership>"
		End Select
	End Function


End Class
