package com.example.kasirapp

import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class LoginActivity : AppCompatActivity() {

    private val adminPin = "1234"
    private val kasirPin = "0000"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val layout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(64, 128, 64, 64)
        }

        val title = TextView(this).apply {
            text = "Login Kasir/Admin"
            textSize = 24f
        }

        val pinInput = EditText(this).apply {
            hint = "Masukkan PIN"
            inputType = android.text.InputType.TYPE_CLASS_NUMBER or android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD
        }

        val loginButton = Button(this).apply {
            text = "Login"
            setOnClickListener {
                val pin = pinInput.text.toString()
                val role = when (pin) {
                    adminPin -> "admin"
                    kasirPin -> "kasir"
                    else -> null
                }

                if (role != null) {
                    val intent = Intent(this@LoginActivity, MainActivity::class.java)
                    intent.putExtra("role", role)
                    startActivity(intent)
                    finish()
                } else {
                    Toast.makeText(this@LoginActivity, "PIN salah", Toast.LENGTH_SHORT).show()
                }
            }
        }

        layout.addView(title)
        layout.addView(pinInput)
        layout.addView(loginButton)

        setContentView(layout)
    }
}
