<?php
ob_start();

// --- Database Connection Details ---
$host_sqlsrv_1 = '192.168.10.61';
$port_sqlsrv_1 = '1433';
$user_sqlsrv_1 = 'zack';
$pass_sqlsrv_1 = 'zack123456';

$db_sqlsrv_1 = 'EHONGDB';
$dsn_sqlsrv_1 = "sqlsrv:Server=$host_sqlsrv_1,$port_sqlsrv_1;Database=$db_sqlsrv_1;TrustServerCertificate=true";

// (ลบ $dsn_sqlsrv_config ออก)

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    
    // === START: ลบส่วนที่เกี่ยวกับ Activity Code ทั้งหมด ===
    // (ลบ $activity_code_select, $activity_code_input)
    // (ลบ if (empty($activity_code)))
    // === END: ลบส่วนที่เกี่ยวกับ Activity Code ทั้งหมด ===

    try {
        // 1. ตรวจสอบ User/Password (จาก EHONGDB)
        $conn = new PDO($dsn_sqlsrv_1, $user_sqlsrv_1, $pass_sqlsrv_1);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT 
                    e.US_CARDID, e.FNAME, e.LNAME, e.NICK, e.BR_ID, e.BARCODE, 
                    b.BR_NAME, b.SZONE_CODE, sz.staff_nick, sz.zstaff_name 
                FROM EMPLOYEE e
                LEFT JOIN BRANCH b ON e.BR_ID = b.BR_ID
                LEFT JOIN branch_salezone sz ON b.SZONE_CODE = sz.szone_code
                WHERE (e.US_NAME = ? OR e.BARCODE = ?) 
                AND e.US_PASSWD = ? 
                AND e.EM_STATUS = 'NM'";
        
        $stmt = $conn->prepare($sql);
        $stmt->execute([$username, $username, $password]);
        $employee = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($employee) {
            
            // === START: ลบส่วนที่ 2 (ตรวจสอบ Activity Code) ออกทั้งหมด ===
            // (ลบ try...catch ที่เชื่อมต่อ CONFIG DB)
            // === END: ลบส่วนที่ 2 (ตรวจสอบ Activity Code) ออกทั้งหมด ===


            // --- 3. Login Successful: Set Cookies ---
            $ten_years_expiry = time() + (10 * 365 * 24 * 60 * 60);
            $current_login_date = date('Y-m-d');
            
            $cookie_options = [
                'expires' => $ten_years_expiry,
                'path' => '/',
                'domain' => '', 
                'secure' => false, 
                'httponly' => true,
                'samesite' => 'Lax'
            ];
            
            setcookie('kRlfYPpU_user_card_id', $employee['US_CARDID'], $cookie_options);
            setcookie('kRlfYPpU_user_fname', $employee['FNAME'], $cookie_options);
            setcookie('kRlfYPpU_user_lname', $employee['LNAME'], $cookie_options);
            setcookie('kRlfYPpU_login_date', $current_login_date, $cookie_options);
            setcookie('kRlfYPpU_user_barcode', $employee['BARCODE'], $cookie_options);
            setcookie('kRlfYPpU_user_br_id', $employee['BR_ID'], $cookie_options);
            setcookie('kRlfYPpU_user_br_name', $employee['BR_NAME'], $cookie_options);
            setcookie('kRlfYPpU_user_szone_code', $employee['SZONE_CODE'], $cookie_options);
            setcookie('kRlfYPpU_user_nick', $employee['NICK'], $cookie_options);
            setcookie('kRlfYPpU_user_staff_nick', $employee['staff_nick'], $cookie_options);
            setcookie('kRlfYPpU_user_zstaff_name', $employee['zstaff_name'], $cookie_options);
            
            // (ลบ setcookie('kRlfYPpU_activity_code', ...))

            // === แก้ไข: เปลี่ยนปลายทางไปที่ select_activity.php ===
            header('Location: select_activity.php');
            exit();
            
        } else {
            // --- Login Failed (User/Pass ผิด) ---
            header('Location: index.php?error=1');
            exit();
        }

    } catch (PDOException $e) {
        error_log("Login Error: " . $e->getMessage());
        header('Location: index.php?error=dberror');
        exit();
    }
} else {
    header('Location: index.php');
    exit();
}

ob_end_flush();
?>