import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/const/colors.dart';
import '../bloc/login_bloc.dart';
import '../model/login_model.dart';


final TextEditingController _userController = TextEditingController();
final TextEditingController _passController = TextEditingController();
LoginModel? loginModels;
class LoginPage extends StatelessWidget {
  const LoginPage({super.key});
  @override
  Widget build(BuildContext context) {
    return
      BlocConsumer<LoginBloc, LoginState>(
      listener: (context, state) async {
        // hàm sẽ đc gọi mỗi khi có sự thay đổi state của Bloc.ví dụ:
        // if(state is MyStateSuccess){
        //   // Thực hiện hành động
        // }
      },
      builder: (context, state) {
        return
          Container(
            color: AppColors.background,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text('Dang Ky & Tu Van',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 60, right: 60),
                  child:
                  TextFormField(
                    decoration: const InputDecoration(
                      labelText: 'Product Name',
                    ),
                    controller: _userController,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 60, right: 60),
                  child:
                  TextFormField(
                    decoration: const InputDecoration(
                      labelText: 'PassWord',
                    ),
                    controller: _passController,
                  ),
                ),

                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: OutlinedButton(
                      style: OutlinedButton.styleFrom(
                        minimumSize: const Size.fromHeight(50),
                        backgroundColor: AppColors.primary,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20.0),
                        ),
                      ),
                      onPressed: () async {
                        BlocProvider.of<LoginBloc>(context).add(
                            RequestLogin(
                                context: context,
                                username: _userController.text,
                                password: _passController.text));
                      },
                      child: Text("Dang Nhap".toUpperCase(),
                        style: const TextStyle(
                          color: AppColors.white,
                            fontWeight: FontWeight.bold),)
                  ),
                ),
                BlocBuilder<LoginBloc, LoginState>(
                    builder: (context, state) {
                      if (state is LoginLoading) {
                        return const CircularProgressIndicator();
                      }
                      if (state is LoginFailed) {
                        return const Text('login false!');
                      }
                      if (state is LoginInitial) {
                        return
                          Text('LoginInitial');
                      }
                      if (state is LoginSuccess) {
                        return
                          Text('Dang Nhap Thanh Cong');
                      }
                      return const CircularProgressIndicator();
                    }),
              ],
            ),
          );
      },
    );
  }
}


