import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:praresi/presentation/controllers/profile_controller.dart';

class ProfileView extends StatelessWidget {
  ProfileView({Key? key}) : super(key: key);

  final controller = Get.put(ProfileController());

  InputDecoration _inputDeco(String label, {bool readOnly = false}) {
    return InputDecoration(
      labelText: label,
      filled: readOnly,
      fillColor: readOnly ? Colors.grey[200] : null,
      floatingLabelBehavior: FloatingLabelBehavior.always,
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: const BorderSide(color: Color(0xFF1976D2), width: 2),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Profil Pengguna", style: TextStyle(color: Colors.white)),
        
        backgroundColor: const Color(0xFF1976D2),
        centerTitle: true,
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        return Padding(
          padding: const EdgeInsets.all(20),
          child: Column(
            children: [
              TextFormField(
                controller: TextEditingController(text: controller.name.value)
                  ..selection = TextSelection.fromPosition(
                    TextPosition(offset: controller.name.value.length),
                  ),
                onChanged: (v) => controller.name.value = v,
                decoration: _inputDeco("Nama Pengguna"),
              ),
              const SizedBox(height: 16),
              TextFormField(
                readOnly: true,
                controller: TextEditingController(text: controller.email.value),
                decoration: _inputDeco("Email", readOnly: true),
              ),
              const SizedBox(height: 32),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF1976D2),
                    minimumSize: const Size.fromHeight(50),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  onPressed: controller.updateName,
                  child: const Text(
                    "Simpan Perubahan",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      }),
    );
  }
}
