import 'package:flutter/material.dart';
import '../../plugins/api.dart';
import '../../dialogs/response_dialog.dart';
import '../room/main_page.dart';

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

  @override
  _InquiryToManagerPageState createState() => _InquiryToManagerPageState();
}

class _InquiryToManagerPageState extends State<InquiryToManagerPage> {
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _contentsController = TextEditingController();

  @override
  void dispose() {
    _titleController.dispose();
    _contentsController.dispose();
    super.dispose();
  }

  void _handleSubmit() async {
    final title = _titleController.text.trim();
    final contents = _contentsController.text.trim();

    // If title or contents are empty, show message
    // /* 예시: 제목이나 내용이 비어있으면 막기 */
    if (title.isEmpty || contents.isEmpty) {
      showResponseDialog(
        context, 
        'Notice' /* 안내 */, 
        'Please enter both title and content.' 
        /* "제목과 내용을 입력해주세요." */
      );
      return;
    }

    // Submit to server (서버에 제출)
    try {
      final requestBody = {
        'title': title,
        'contents': contents,
      };
      final serverResponse = await Api.serverRequest(uri: '/inquiry/request', body: requestBody);

      if (serverResponse == null) {
        showResponseDialog(
          context, 
          'Inquiry Failed' /* 문의 전송 실패 */, 
          'No response from server.' /* 서버 응답이 없습니다. */
        );
      }

      if (serverResponse['result'] == 'OK') {
        final serverResponse1 = serverResponse['response'];
        if (serverResponse1['result'] == 'OK') {
          await showResponseDialog(
            context, 
            'Inquiry Sent' /* 문의 전송 완료 */, 
            'Your inquiry has been successfully sent.' 
            /* 문의가 전송되었습니다. */
          );
          Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (context) => const MainPage()),
            (route) => false,
          );
        } else {
          showResponseDialog(
            context, 
            'Inquiry Failed' /* 문의 전송 실패 */, 
            'Failed to send inquiry.' /* 문의 전송에 실패했습니다. */
          );
        }
      } else {
        showResponseDialog(
          context, 
          'Inquiry Failed' /* 문의 전송 실패 */, 
          'Failed to send inquiry.' /* 문의 전송에 실패했습니다. */
        );
      }
    } catch (e) {
      showResponseDialog(
        context, 
        'Inquiry Failed' /* 문의 전송 실패 */, 
        'Failed to send inquiry.' /* 문의 전송에 실패했습니다. */
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: const Text(
          'Inquiry' 
          /* 문의하기 */, 
          style: TextStyle(color: Colors.white),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // Inquiry Title input (문의 제목)
            TextField(
              controller: _titleController,
              maxLength: 100,
              decoration: const InputDecoration(
                labelText: 'Inquiry Title' 
                /* 문의 제목 */,
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            // Inquiry Content input (문의 내용)
            TextField(
              controller: _contentsController,
              maxLength: 1000,
              maxLines: 10,
              decoration: const InputDecoration(
                labelText: 'Inquiry Content' 
                /* 문의 내용 */,
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            // Submit button (제출하기)
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(backgroundColor: Colors.black),
                onPressed: _handleSubmit,
                child: const Text(
                  'Submit' 
                  /* 제출하기 */,
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
