import 'package:flutter/material.dart';

import 'package:get/get.dart';

import '../../../component/sys_app_bar.dart';
import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  const LoginView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/images/login/login.png"),
          fit: BoxFit.fill,
        ),
      ),
      child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: const SysAppBar(title: Text("登录"),),

          body: Obx(() {
            if(controller.errorMsg.isNotEmpty){
              WidgetsBinding.instance.addPostFrameCallback((_) {
                ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text(controller.errorMsg.value))
                );
              });
            }
            return LoginScreen(isLoading: controller.isLoading.value,
                username: controller.username(),
                password: controller.password(),
                onLogin: (username, password) {
                  // 在这里处理登录逻辑，例如调用API
                  controller.fetchLogin(username,password);
                });
          })
      ),
    );
  }
}

class LoginScreen extends StatefulWidget {
  final Function(String username, String password) onLogin;
  final bool isLoading;
  final String username;
  final String password;
  LoginScreen({required this.isLoading, required this.onLogin, required this.username, required this.password});

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 30),
      child: Center(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(55, 0, 55, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextField(
                controller: _usernameController,
                decoration: const InputDecoration(
                  labelText: '用户名',
                  hintText: '请输入用户名',
                  border: InputBorder.none,
                ),
              ),
              const SizedBox(height: 16),
              TextField(
                controller: _passwordController,
                decoration: const InputDecoration(
                  labelText: '密码',
                  hintText: '输入密码',
                  border: InputBorder.none,
                ),
                obscureText: true,

              ),
              const SizedBox(height: 20),
              SizedBox(
                width: 200,
                height: 40,
                child: ElevatedButton(
                  onPressed: () {
                    if (!widget.isLoading) {
                      final username = _usernameController.text;
                      final password = _passwordController.text;
                      widget.onLogin(username, password);
                    }
                  },
                  child: widget.isLoading ? const CircularProgressIndicator(
                    color: Colors.white,
                  ) : const Text('登录'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    super.dispose();
  }
}