import 'package:flutter/material.dart';
import 'package:ont_app2/ui/widgets/my_button.dart';
import 'package:stacked/stacked.dart';
import 'package:validatorless/validatorless.dart';

import '../../widgets/my_textformfield.dart';
import './login_view_model.dart';

class LoginView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder<LoginViewModel>.reactive(
      viewModelBuilder: () => LoginViewModel(),
      onViewModelReady: (LoginViewModel model) async {
        await model.init();
      },
      builder: (
        BuildContext context,
        LoginViewModel model,
        Widget? child,
      ) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.all(20),
            child: Center(
              child: SingleChildScrollView(
                child: Form(
                  key: model.formKey,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Image(
                        image: AssetImage("assets/logo1.png"),
                        width: 200,
                        height: 200,
                      ),
                      const Text(
                        "Silahkan Login",
                      ),
                      const SizedBox(height: 20),
                      MyTextFormField(
                        controller: model.nomorInternetController,
                        labelText: "Nomor Internet",
                        hintText: "Masukkan Nomor Internet",
                        prefixIcon: const Icon(
                          Icons.phone,
                        ),
                        validator: Validatorless.multiple(
                          [
                            Validatorless.required("Masukkan Nomor Internet"),
                            Validatorless.min(
                                12, "Nomor Internet harus 12 angka"),
                            Validatorless.number(
                                "Nomor Internet harus berupa angka"),
                          ],
                        ),
                        keyboardType: TextInputType.number,
                        maxLength: 12,
                      ),
                      const SizedBox(height: 20),
                      MyTextFormField(
                        controller: model.passwordController,
                        labelText: "Password",
                        hintText: "Masukkan Password",
                        obscureText: true,
                        prefixIcon: const Icon(
                          Icons.lock,
                        ),
                        validator: Validatorless.required("Masukkan Password"),
                      ),
                      const SizedBox(height: 15),
                      MyButton(
                        text: "Login",
                        onPressed: () {
                          // if (!model.formKey.currentState!.validate()) {
                          //   return;
                          // }
                          model.login();
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
