import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/app.dart';
import 'package:worker_client/models/entities/person.dart';
import 'package:worker_client/services/user.dart';
import 'package:worker_client/widgets/MyButton/index.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

class UserInfo extends BasicPage {
  UserInfo({Key? key}) : super(key: key);

  AppController store = AppController.t;
  late String name;
  late String phone;
  late TextEditingController _cName;
  late TextEditingController _cPhone;

  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    _cName = TextEditingController(text: store.user().nickName.toString());
    _cPhone = TextEditingController(text: store.user().phone.toString());
    name = store.user().nickName.toString();
    phone = store.user().phone.toString();
  }
  RegExp exp = RegExp(r'^1[3456789]\d{9}$');

  void handleOk() {
    if (name == '' && phone == '') {
      Fluttertoast.showToast(msg: '请输入您需要修改的信息');
      return;
    } else if (phone != '' && !exp.hasMatch(phone)) {
      Fluttertoast.showToast(msg: '请输入正确的手机号');
      return;
    } else {

      updateInfo(store.user().personId.toString(), {
        ...store.user().toJson(),
        'nickName': name,
        'phone': phone
      }).then((value) {
        Fluttertoast.showToast(msg: '修改成功');
        store.user(Person.fromJson(value));
        Get.offAllNamed('/main');
      });
    }
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '个人信息';
    return Container(
      padding: EdgeInsets.all(15.w),
      decoration: const BoxDecoration(color: Colors.white),
      child: Column(
        children: [
          Container(
            width: 345.w,
            height: 92.h,
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(color: Colors.white, boxShadow: [
              BoxShadow(color: Color(0x1f000000), offset: Offset(0, 1.w))
            ]),
            child: store.user().avatar != null
                ? Container(
                    clipBehavior: Clip.hardEdge,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(31.w))),
                    child: Image.network(
                      store.user().avatar.toString(),
                      width: 62.w,
                      height: 62.w,
                    ),
                  )
                : Image.asset(
                    'images/main/defaultAvatar.png',
                    width: 62.w,
                    height: 62.w,
                  ),
          ),
          Container(
              width: 345.w,
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.only(top: 40.h),
              decoration: BoxDecoration(color: Colors.white, boxShadow: [
                BoxShadow(color: Color(0x1f000000), offset: Offset(0, 1.w))
              ]),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '用户名：',
                    style: TextStyle(
                        color: const Color(0xFF333333),
                        letterSpacing: 2,
                        fontSize: 17.sp,
                        fontWeight: FontWeight.bold),
                  ),
                  Container(
                    height: 54.h,
                    alignment: Alignment.centerLeft,
                    child: store.user().userName != null
                        ? Text(
                            store.user().userName.toString(),
                            style: TextStyle(
                                color: const Color(0xFF333333),
                                fontSize: 17.sp,
                                fontWeight: FontWeight.bold),
                          )
                        : const Text(''),
                  ),
                ],
              )),
          Container(
            width: 345.w,
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(top: 40.h),
            decoration: BoxDecoration(color: Colors.white, boxShadow: [
              BoxShadow(color: const Color(0x1f000000), offset: Offset(0, 1.w))
            ]),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '姓名：',
                  style: TextStyle(
                      color: const Color(0xFF333333),
                      fontSize: 17.sp,
                      fontWeight: FontWeight.bold,
                      letterSpacing: 2),
                ),
                Container(
                  height: 54.h,
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: TextField(
                          controller: _cName,
                          style: TextStyle(
                              fontSize: 17.sp,
                              fontWeight: FontWeight.bold,
                              color: const Color(0xff333333)),
                          decoration: const InputDecoration(
                            isCollapsed: true,
                            hintText: '请输入您的姓名',
                            counterText: '', //去掉右下角的东西
                            border: InputBorder.none,
                            floatingLabelBehavior: FloatingLabelBehavior.never,
                          ),
                          onChanged: (val) {
                            name = val;
                          },
                        ),
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          name = '';
                          _cName.clear();
                        },
                        child: Container(
                          width: 50.w,
                          alignment: Alignment.center,
                          child: Image.asset(
                            'images/main/cancel.png',
                            width: 18.w,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
          Container(
            width: 345.w,
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(top: 40.h),
            decoration: BoxDecoration(color: Colors.white, boxShadow: [
              BoxShadow(color: Color(0x1f000000), offset: Offset(0, 1.w))
            ]),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '手机号：',
                  style: TextStyle(
                      color: const Color(0xFF333333),
                      letterSpacing: 2,
                      fontSize: 17.sp,
                      fontWeight: FontWeight.bold),
                ),
                Container(
                  height: 54.h,
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: TextField(
                          //赋初值
                          controller: _cPhone,
                          maxLength: 11,
                          keyboardType: TextInputType.number,
                          style: TextStyle(
                              fontSize: 17.sp,
                              fontWeight: FontWeight.bold,
                              color: const Color(0xff333333)),
                          decoration: const InputDecoration(
                            isCollapsed: true,
                            hintText: '请输入您的手机号',
                            counterText: '', //去掉右下角的东西
                            border: InputBorder.none,
                            floatingLabelBehavior: FloatingLabelBehavior.never,
                          ),
                          onChanged: (val) {
                            phone = val;
                          },
                        ),
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          phone = '';
                          _cPhone.clear();
                        },
                        child: Container(
                          width: 50.w,
                          alignment: Alignment.center,
                          child: Image.asset(
                            'images/main/cancel.png',
                            width: 18.w,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),

          const Spacer(),
          MyButton(
              text: '保存',
              type: 0,
              pwith: 30.w,
              disable: false,
              onClick: () {
                handleOk();
              })
        ],
      ),
    );
  }
}
