import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/common/base_structure_result.dart';
import 'package:marking_app/provider/user_provider.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/const_text.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/my_text.dart';
import 'package:marking_app/utils/request/rest_client.dart';

class LogOff extends StatefulHookConsumerWidget {
  const LogOff({Key? key}) : super(key: key);

  @override
  ConsumerState<LogOff> createState() => _LogOffState();
}

class _LogOffState extends ConsumerState<LogOff> with CommonMixin{
  bool canClick = true;
  bool readAgreement = false; // 阅读协议

  logOff() async{

    if (!canClick) return;
    setState(() => canClick = false);
    void toMsg(msg) {
      ToastUtils.showError(msg);
      setState(() => canClick = true);
    }
    if (!readAgreement) return toMsg('请勾选我已阅读用户协议和隐私协议');

    final userState = ref.watch(userProvider);
    EasyLoading.show(status: 'loading...');
    RestClient client = await getClientLogin();
    BaseStructureResult<dynamic> resultData = await client.getLogOff(userState.loginName);
    if (resultData.code != 200) {
      return ToastUtils.showError(resultData.message ?? '注销失败，请重试');
    }
    EasyLoading.dismiss();
    ToastUtils.showSuccess('注销成功');
    // 跳转登录页
    RouterManager.router.navigateTo(context, RouterManager.loginPath);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(248, 248, 248, 1),
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          '注销',
          style: TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
        ),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 30.r,
          ),
          Center(
            child: Text(
              '温馨提示，请仔细阅读',
              style: TextStyle(
                  fontSize: 16.sp,
                  color: Color(0xFF464646),
                  fontWeight: FontWeight.w500),
            ),
          ),
          SizedBox(
            height: 20.r,
          ),
          cont('您仅可注销您本人申请的账号'),
          cont('注销后，账号的全部权益将被清除'),
          cont('注销后，账号下的所有数据、记录等将无法访问或找回'),
          cont('注销后，将清除该账号在网站保留的个人信息'),
          cont('注销账号(账号删除)属于用户自主行为'),
       /*   Padding(
            padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 14.r),
            child: Row(
              children: [
                Text(
                  '注销账号即表示您已确认并同意',
                  style: TextStyle(fontSize: 14.sp, color: Color(0xFF616161)),
                ),
                InkWell(
                  onTap: (){
                    RouterManager.router.navigateTo(
                      context,
                      '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.USER_LOGOUT.name}',
                      transition: getTransition(),
                    );
                  },
                  child: Text(
                    '注销协议',
                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF6888FD)),
                  ),
                )
              ],
            ),
          ),*/
          SizedBox(
            height: 50.r,
          ),
          InkWell(
            onTap:logOff,
            child: Center(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 10.h),
                alignment: Alignment.center,
                width: 200.r,
                height: 50.r,
                decoration: BoxDecoration(
                  color: canClick
                      ? const Color.fromRGBO(9, 105, 246, 1)
                      : Colors.grey,
                  /*boxShadow: [
                    BoxShadow(
                      color: const Color.fromRGBO(46, 91, 255, 0.5),
                      offset: Offset(6.w, 10.h), //阴影y轴偏移量
                      blurRadius: 14, //阴影模糊程度
                      spreadRadius: 0.5, //阴影扩散程度
                    )
                  ],*/
                  borderRadius: BorderRadius.all(
                    Radius.circular(20.w),
                  ),
                ),
                child: Text(
                  '确认注销',
                  style: TextStyle(fontSize: 16.sp, color: Colors.white),
                ),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 14.r),
            child: Row(
              children: [
                Container(
                  width: 30.w,
                  padding: EdgeInsets.only(right: 10.w),
                  child: Checkbox(
                    activeColor: Colors.deepOrangeAccent,
                    checkColor: Colors.white,
                    value: readAgreement,
                    onChanged: (value) {
                      setState(() {
                        readAgreement = value ?? false;
                      });
                    },
                  ),
                ),
                Text(
                  '已确认并同意',
                  style: TextStyle(fontSize: 14.sp, color: Color(0xFF616161)),
                ),
                InkWell(
                  onTap: () {
                    RouterManager.router.navigateTo(
                      context,
                      '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.USER_LOGOUT.name}',
                      transition: getTransition(),
                    );
                  },
                  child: quickText(
                    '《注销协议》',
                    size: 12.sp,
                    color: Colors.deepOrangeAccent,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Widget cont(String name) {
  return Padding(
    padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 14.r),
    child: Text(
      name,
      style: TextStyle(fontSize: 14.sp, color: Color(0xFF616161)),
    ),
  );
}
