import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:odf/tools/set/set_model.dart';

class ChangePasswordPage extends StatefulWidget {
  const ChangePasswordPage({super.key});

  @override
  State<ChangePasswordPage> createState() => _ChangePasswordPageState();
}

class _ChangePasswordPageState extends State<ChangePasswordPage> {
  final TextEditingController _oldController = TextEditingController();
  final TextEditingController _newController = TextEditingController();

  late StreamSubscription subscription;
  final SetModel _viewmodel = SetModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    subscription = _viewmodel.streamController.stream.listen((event) {
      String code = event['code'];
      if (code.isNotEmpty) {
        switch (code) {
          case "updateUserPwd":
            Navigator.pop(context);
            EasyLoading.showToast("修改成功");
            break;
          case "updateUserPwdError":
            EasyLoading.showToast(event['data']);
            break;
        }
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    subscription.cancel();
    _oldController.dispose();
    _newController.dispose();
    super.dispose();
  }

  Future<void> changePwd() async {
    if (_oldController.text == "") {
      EasyLoading.showToast("请输入旧密码!");
      return;
    }
    if (_newController.text == "") {
      EasyLoading.showToast("请输入新密码!");
      return;
    }

    _viewmodel.updateUserPwd(_oldController.text, _newController.text);
  }

  @override
  Widget build(BuildContext context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    final size = MediaQuery.of(context).size;
    final t10 = size.width / 36;
    final w25 = size.width / 14.4;
    final p5 = size.width / 72;
    final w9 = size.width / 40;
    final s21 = size.width / 17.142857142857;
    final t20 = size.width / 18;

    return Scaffold(
      backgroundColor: const Color(0xFFD8D8D8),
      body: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
          image: AssetImage('assets/images/home_bg.png'),
          fit: BoxFit.cover,
        )),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: statusBarHeight + t10, left: t10, right: t10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Container(
                      width: w25,
                      height: w25,
                      padding: EdgeInsets.all(p5),
                      child: Image(
                        width: w9,
                        image: const AssetImage('assets/images/ic_back.png'),
                      ),
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    child: Text(
                      '修改密码',
                      style: TextStyle(fontSize: s21, fontWeight: FontWeight.w600),
                    ),
                  ),
                  Container(
                    width: w25,
                    height: w25,
                    padding: EdgeInsets.all(p5),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15, right: 15, top: 25),
              padding: EdgeInsets.only(left: 20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
              child: TextField(
                cursorColor: Color(0xFF1A73EC),
                controller: _oldController,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.zero,
                  hintText: '请输入旧密码',
                  border: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  hintStyle: TextStyle(color: Color(0xFF999999), fontSize: 12),
                ),
                style: TextStyle(fontSize: 12),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15, right: 15, top: 25),
              padding: EdgeInsets.only(left: 20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
              child: TextField(
                cursorColor: Color(0xFF1A73EC),
                controller: _newController,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.zero,
                  hintText: '请输入新密码',
                  border: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  hintStyle: TextStyle(color: Color(0xFF999999), fontSize: 12),
                ),
                style: TextStyle(fontSize: 12),
              ),
            ),
            GestureDetector(
              onTap: () {
                changePwd();
              },
              child: Container(
                height: 50,
                margin: EdgeInsets.symmetric(horizontal: 15, vertical: 50),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Color(0xFF1A73EC),
                  borderRadius: BorderRadius.all(Radius.circular(8)),
                ),
                child: Text(
                  "确认修改",
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
