@echo off
title MariaDB Lab Access Manager ( Provided by HK Groups )
color 0b

:: --- NEW: LOAD SAVED IP FROM FILE ---
set "SAVE_FILE=server_config.dat"
set "MY_DEFAULT_IP=192.168.1.10"

if exist "%SAVE_FILE%" (
    set /p MY_DEFAULT_IP=<"%SAVE_FILE%"
)

:COLOR_MENU
cls
echo ===========================================
echo       SELECT YOUR THEME COLOR
echo ===========================================
echo [1] Cyan (Default)
echo [2] Light Green (Matrix Style)
echo [3] Yellow 
echo [4] Light Red
echo [5] Bright White
echo [6] Purple
echo [Q] Skip to Login
echo ===========================================
choice /C 123456Q /M "Select a theme:"

if errorlevel 7 goto :RETRY_LOOP
if errorlevel 6 color 05
if errorlevel 5 color 0F
if errorlevel 4 color 0C
if errorlevel 3 color 0E
if errorlevel 2 color 0A
if errorlevel 1 color 0B

:RETRY_LOOP
cls
echo ===========================================
echo       MARIADB LAB SYSTEM CONNECTOR
echo ===========================================
echo.

set "INPUT_IP="
echo Current Default IP: %MY_DEFAULT_IP%
set /p INPUT_IP="Enter Server IP [Press Enter for Default]: "

:: If user types a new IP, save it. If they press Enter, use the default.
if "%INPUT_IP%"=="" (
    set "SERVER_IP=%MY_DEFAULT_IP%"
) else (
    set "SERVER_IP=%INPUT_IP%"
    echo %INPUT_IP% > "%SAVE_FILE%"
)

echo.
set /p DB_USER="Enter your USN (e.g., 1by24ai001): "

echo.
echo Connecting to %SERVER_IP% as %DB_USER%...
echo ===========================================

:: Checks for mysql.exe in the same extracted folder
if exist "mysql.exe" (
    mysql.exe -h %SERVER_IP% -u %DB_USER% -p --skip-ssl
) else (
    echo [ERROR] mysql.exe not found!
    echo Please ensure you extracted the ZIP before running.
    pause
)

echo.
echo ===========================================
echo Connection Closed.
echo ===========================================
echo.

echo [R] Retry / Change User
echo [C] Change Color Theme
echo [Q] Quit
echo.

choice /C RCQ /M "Select an option:"

if errorlevel 3 goto :QUIT
if errorlevel 2 goto :COLOR_MENU
if errorlevel 1 goto :RETRY_LOOP

:QUIT
echo.
echo Closing Lab Manager (HK Groups)...
timeout /t 2 >nul
exit
