import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '/app/route.dart';

import '../../app/constant.dart';
import '../../provider/current_user_provider.dart';
import '../../widget/snackbar_widget.dart';
import 'personal_information_provider.dart';

class PersonalInformationScreen extends HookConsumerWidget {
  const PersonalInformationScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final name = ref.watch(currentUserProvider)?.model.user?.mStaffName ?? "0";
    final namaController = useTextEditingController(text: name);
    final emailController = useTextEditingController(text: "-");
    final nikController = useTextEditingController(text: "-");
    final isLoading = useState<bool>(false);

    final mCourirID =
        ref.watch(currentUserProvider)?.model.user?.mCourierID ?? "0";

    fetchData() {
      ref
          .read(PersonalInformationProvider.notifier)
          .getData(mCourierID: mCourirID);
    }

    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        fetchData();
      });
      return () {};
    }, []);

    ref.listen(PersonalInformationProvider, (previous, next) {
      if (next is PersonalInformationStateLoading) {
        isLoading.value = true;
      } else if (next is PersonalInformationStateError) {
        isLoading.value = false;
        // print(next.message);
        SanckbarWidget(
            context, next.message.substring(0, 30), snackbarType.error);
      } else if (next is PersonalInformationStateDone) {
        isLoading.value = false;
        if (next.model.isNotEmpty) {
          nikController.text = next.model[0].natStaffNIK.toString();
        } else {
          nikController.text = "-";
        }
      }
    });

    return Scaffold(
        body: ListView(
      children: [
        Container(
          // color: Colors.white,
          height: Constant.getActualY(context: context, y: 56),
        ),
        Container(
          padding: EdgeInsets.symmetric(
              horizontal: Constant.getActualX(context: context, x: 10)),
          // color: Colors.white,
          height: Constant.getActualY(context: context, y: 48),
          child: Row(
            children: [
              IconButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                icon: Icon(Icons.arrow_back_ios_rounded),
              ),
              Text(
                "Informasi Pribadi",
                style: Constant.body1(context: context).copyWith(
                    fontWeight: FontWeight.w600, color: Constant.textPrimary),
              ),
            ],
          ),
        ),
        Container(
          // color: Colors.white,
          height: Constant.getActualY(context: context, y: 31),
        ),
        Container(
          margin: EdgeInsets.symmetric(
              horizontal: Constant.getActualX(context: context, x: 34)),
          child: TextFormField(
            style: TextStyle(color: Colors.grey),
            enabled: false,
            controller: namaController,
            decoration: InputDecoration(
              labelText: "Nama",
              hintText: "Nama",
              border:
                  OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
            ),
          ),
        ),
        Container(
          height: Constant.getActualY(context: context, y: 24),
        ),
        Container(
          margin: EdgeInsets.symmetric(
              horizontal: Constant.getActualX(context: context, x: 34)),
          // child: TextFormField(
          //   style: TextStyle(color: Colors.grey),
          //   enabled: false,
          //   controller: emailController,
          //   decoration: InputDecoration(
          //     labelText: "Email",
          //     hintText: "Email",
          //     border:
          //         OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
          //   ),
          // ),
          child: TextFormField(
            style: TextStyle(color: Colors.grey),
            enabled: false,
            controller: nikController,
            decoration: InputDecoration(
              labelText: "NIK",
              hintText: "NIK",
              border:
                  OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
            ),
          ),
        ),

        SizedBox(
          height: Constant.getActualY(context: context, y: 20),
        ),
        // loading
        if (isLoading.value)
          const Center(
            child: CircularProgressIndicator(),
          ),
        Container(
          height: Constant.getActualY(context: context, y: 32),
        ),
        Container(
          margin: EdgeInsets.symmetric(
              horizontal: Constant.getActualX(context: context, x: 34)),
          child: OutlinedButton(
            onPressed: () {
              Navigator.pushNamed(context, changePasswordRoute);
            },
            style: OutlinedButton.styleFrom(
              shape: RoundedRectangleBorder(
                side: BorderSide(
                    style: BorderStyle.solid, color: Constant.primaryBlue),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            child: SizedBox(
              height: Constant.getActualY(context: context, y: 46),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    "Ubah Password",
                    style: Constant.body3(context: context)
                        .copyWith(color: Constant.textPrimary),
                  ),
                  SizedBox(
                    width: Constant.getActualX(context: context, x: 8),
                  ),
                  Icon(
                    Icons.arrow_forward_ios_rounded,
                    color: Constant.textPrimary,
                  )
                ],
              ),
            ),
          ),
        ),
      ],
    ));
  }
}
