import 'package:flutter/material.dart';
import '../../utils/showToast.dart';
import '../../utils/dioHttp.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  final _userName = TextEditingController();
  final _password = TextEditingController();
  bool checked = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        elevation: 0, // z轴阴影
        titleSpacing: 0, // 标题与其他控件的间隔
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: Text(
          '登录',
          style: TextStyle(
              color: Color.fromRGBO(51, 51, 51, 1),
              fontSize: 16,
              fontWeight: FontWeight.bold),
        ),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 30),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 80,
              ),
              Text(
                '您好，欢迎登录',
                style: TextStyle(fontSize: 28, fontWeight: FontWeight.w800),
              ),
              SizedBox(
                height: 20,
              ),
              TextFormField(
                controller: _userName,
                validator: (value) {
                  return value!.trim().isEmpty ? '请输入账号' : null;
                },
                decoration: InputDecoration(
                  labelText: "账号",
                  labelStyle: TextStyle(color: Color.fromRGBO(21, 185, 208, 1)),
                  enabledBorder: UnderlineInputBorder(
                    borderSide:
                        BorderSide(color: Color.fromRGBO(187, 187, 187, 1)),
                  ),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: Color.fromRGBO(21, 185, 208, 1),
                    ),
                  ),
                ),
                style: TextStyle(fontSize: 16),
                keyboardType: TextInputType.phone,
              ),
              SizedBox(
                height: 20,
              ),
              TextFormField(
                controller: _password,
                validator: (value) {
                  return value!.trim().isEmpty ? '请输入密码' : null;
                },
                decoration: InputDecoration(
                  labelText: "密码",
                  labelStyle: TextStyle(color: Color.fromRGBO(21, 185, 208, 1)),
                  enabledBorder: UnderlineInputBorder(
                    borderSide:
                        BorderSide(color: Color.fromRGBO(187, 187, 187, 1)),
                  ),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: Color.fromRGBO(21, 185, 208, 1),
                    ),
                  ),
                ),
                style: TextStyle(fontSize: 16),
                obscureText: true,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Checkbox(
                      value: checked,
                      activeColor: Color.fromRGBO(21, 185, 208, 1),
                      onChanged: (value) {
                        setState(() {
                          checked = !checked;
                        });
                      }),
                  Text(
                    '同意',
                    style: TextStyle(fontSize: 14),
                  ),
                  Text(
                    '《服务协议》',
                    style: TextStyle(fontSize: 14, color: Colors.blue),
                  ),
                  Text(
                    '和',
                    style: TextStyle(fontSize: 14),
                  ),
                  Text('《隐私政策》',
                      style: TextStyle(fontSize: 14, color: Colors.blue)),
                ],
              ),
              SizedBox(
                height: 50,
              ),
              ElevatedButton(
                child: Text(
                  '登录',
                  style: TextStyle(fontSize: 16.0, color: Colors.white),
                ),
                style: ElevatedButton.styleFrom(
                  primary: Color.fromRGBO(21, 185, 208, 1),
                  minimumSize: Size(MediaQuery.of(context).size.width, 50),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(30)),
                ),
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    print('表单验证通过！');
                    if (!checked) {
                      print('请勾选服务协议和隐私政策');
                    } else {
                      dioHttp().dio.post('chat/api/auth/login', data: {
                        "username": _userName.text,
                        "password": _password.text,
                      }).then((res) {
                        print(res);
                      });

                      // print('请求登录接口业务逻辑');

                      // showToast("请求登录接口业务逻辑");

                      // showLoading(context, '加载中...');
                      // Future.delayed(Duration(seconds: 3), () {
                      //   Navigator.of(context).pop();
                      // });

                      // showConfirmDialog(context, '确认xxxxxxx吗？', () {
                      //   print('xxxxxxx');
                      // });

                    }
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
