import 'package:chat/configs/app_colors.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/utils/ui_tools.dart';
import 'package:chat/widgets/custom_primary_button.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

  @override
  State<ContactFriendRequestApplyPage> createState() =>
      _ContactFriendRequestApplyPageState();
}

class _ContactFriendRequestApplyPageState
    extends State<ContactFriendRequestApplyPage> {
  late final String userID;
  String _remark = '';
  String _wording = '';
  final TextEditingController _wordingController = TextEditingController();
  final TextEditingController _remarkController = TextEditingController();

  @override
  void initState() {
    super.initState();
    userID = Get.arguments['userID'];

    _wordingController.text = '我是 ${AuthService.to.userInfo.value.nickname}';
  }

  @override
  void dispose() {
    _wordingController.dispose();
    _remarkController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('好友申请'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '发送添加朋友申请',
              style: TextStyle(
                color: AppColors.unactive,
              ),
            ),
            const SizedBox(height: 4),
            TextField(
              controller: _wordingController,
              style: const TextStyle(
                fontSize: 14,
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                fillColor: AppColors.unactive.withOpacity(0.1),
                filled: true,
              ),
              onChanged: (e) {
                setState(() {
                  _wording = e;
                });
              },
            ),
            const SizedBox(height: 32),
            const Text(
              '设置备注',
              style: TextStyle(
                color: AppColors.unactive,
              ),
            ),
            const SizedBox(height: 4),
            TextField(
              controller: _remarkController,
              style: const TextStyle(
                fontSize: 14,
              ),
              decoration: InputDecoration(
                hintText: '',
                border: InputBorder.none,
                fillColor: AppColors.unactive.withOpacity(0.1),
                filled: true,
              ),
              onChanged: (e) {
                setState(() {
                  _remark = e;
                });
              },
            ),
            const SizedBox(height: 32),
            CustomPrimaryButton(
              text: '发送',
              onPressed: () async {
                var res = await TimFriendService.to.add(
                  userID,
                  remark: _remark,
                  addWording: _wording,
                );

                if (res) {
                  UiTools.toast('申请成功');
                  Get.back();
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}
