import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/screens/login/login.controller.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';

class LoginScreen extends StatelessWidget {
  LoginScreen({super.key});
  final _controller = Get.put<LoginController>(LoginController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/bg.jpg'),
                fit: BoxFit.cover,
              ),
            ),
            child: buildBody()));

    // return Scaffold(

    //   body: SafeArea(child: buildBody()));
  }

  buildBody() {
    return Center(
        child: SingleChildScrollView(
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 点击空白处时收起键盘
          FocusScope.of(Get.context!).unfocus();
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image(
                    height: ScreenAdaper.width(150),
                    image: const AssetImage(
                      'assets/images/company_logo.png',
                    )),
                SizedBox(
                  width: ScreenAdaper.width(10),
                ),
                Text(
                  '山矿通',
                  style: TextStyle(
                      fontWeight: FontWeight.w700,
                      color: AppTheme.nearlyWhite,
                      letterSpacing: ScreenAdaper.sp(5),
                      fontSize: ScreenAdaper.sp(70)),
                )
              ],
            ),
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.center,
            //   children: [
            //     Text(
            //       '山矿通',
            //       style: TextStyle(
            //           letterSpacing: ScreenAdaper.sp(10),
            //           fontSize: ScreenAdaper.sp(70)),
            //     )
            //   ],
            // ),
            SizedBox(height: ScreenAdaper.height(50)),
            buildForm(),
            SizedBox(height: ScreenAdaper.height(50)),
          ],
        ),
      ),
    ));
  }

  Widget buildForm() {
    final children = [
      buildUserNameInput(),
      SizedBox(
        height: ScreenAdaper.height(20),
      ),
      buildPasswordInput(),
      SizedBox(
        height: ScreenAdaper.height(20),
      ),
      buildForgotPassword(),
      SizedBox(
        height: ScreenAdaper.height(20),
      ),
      buildSubmitButton(),
    ];

    final child = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: children,
    );

    return Form(
        key: _controller.formKey,
        child: Container(
          alignment: Alignment.center,
          width: ScreenAdaper.width(800),
          padding: EdgeInsets.symmetric(
              vertical: 0, horizontal: ScreenAdaper.width(50)),
          child: child,
        ));
  }

  Widget buildSubmitButton() {
    final button = ElevatedButton(
      style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(ScreenAdaper.sp(30))),
          padding: EdgeInsets.symmetric(
              horizontal: ScreenAdaper.height(25),
              vertical: ScreenAdaper.height(20)),
          foregroundColor: AppTheme.nearlyWhite,
          textStyle: TextStyle(
              letterSpacing: 5,
              fontWeight: FontWeight.bold,
              fontSize: ScreenAdaper.sp(25)),
          backgroundColor: AppTheme.primaryColor),
      onPressed: _controller.doLogin,
      child: const Text('登录'),
    );
    final child = Expanded(
      child: button,
    );
    return Row(
      children: [child],
    );
  }

  Widget buildUserNameInput() {
    return TextFormField(
      decoration: const InputDecoration(
        labelText: '用户名',
      ),
      onFieldSubmitted: (value) {
        _controller.passwordFocusNode.requestFocus();
      },
      style: TextStyle(fontSize: ScreenAdaper.sp(25)),
      onChanged: (value) {},
      validator: (String? value) {
        if (value == null || value == '') {
          return '用户名必填';
        } else {
          return null;
        }
      },
    );
  }

  Widget buildPasswordInput() {
    return TextFormField(
      decoration: const InputDecoration(
        labelText: '密码',
      ),
      obscureText: true,
      focusNode: _controller.passwordFocusNode,
      onFieldSubmitted: (value) {
        _controller.doLogin();
      },
      style: TextStyle(fontSize: ScreenAdaper.sp(25)),
      onChanged: (value) {},
      validator: (String? value) {
        return (value ?? '').length >= 6 ? null : '密码长度至少6位';
      },
    );
  }

  Widget buildForgotPassword() {
    return GestureDetector(
      onTap: () {},
      child: Container(
          alignment: Alignment.centerRight,
          child: Text(
            '忘记密码?',
            style: TextStyle(
              color: Colors.grey,
              fontSize: ScreenAdaper.sp(20),
              decoration: TextDecoration.underline,
            ),
          )),
    );
  }
}
