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


class ChangePasswordView extends StatelessWidget {
  ChangePasswordView({super.key});

  final _currentPasswordC = TextEditingController();
  final _newPasswordC = TextEditingController();
  final _confirmPasswordC = TextEditingController();

  final changePasswordC = Get.put(ChangePasswordController());

  InputDecoration _inputDeco(String label, {String? hint}) {
    return InputDecoration(
      labelText: label,
      hintText: hint,
      floatingLabelBehavior: FloatingLabelBehavior.always,
      contentPadding:
          const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      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(
      backgroundColor: Colors.grey[50],
      appBar: AppBar(
        backgroundColor: Colors.blue,
        elevation: 0,
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text(
          'Ubah Kata Sandi',
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
      ),
      body: Obx(() {
        return SingleChildScrollView(
          padding: const EdgeInsets.all(20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                controller: _currentPasswordC,
                obscureText: true,
                decoration: _inputDeco(
                  'Kata sandi saat ini',
                  hint: 'Masukkan kata sandi lama',
                ),
              ),
              const SizedBox(height: 16),
              TextField(
                controller: _newPasswordC,
                obscureText: true,
                decoration: _inputDeco(
                  'Kata sandi baru',
                  hint: 'Masukkan kata sandi baru',
                ),
              ),
              const SizedBox(height: 16),
              TextField(
                controller: _confirmPasswordC,
                obscureText: true,
                decoration: _inputDeco(
                  'Konfirmasi kata sandi baru',
                  hint: 'Ulangi kata sandi baru',
                ),
              ),
              const SizedBox(height: 24),
              SizedBox(
  width: double.infinity,
  child: ElevatedButton(
    style: ElevatedButton.styleFrom(
      backgroundColor: const Color(0xFF1976D2),
      minimumSize: const Size.fromHeight(50),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      elevation: 0,
    ),
    onPressed: changePasswordC.isLoading.value
        ? null
        : () {
            // ✅ VALIDASI FIELD KOSONG
            if (_currentPasswordC.text.trim().isEmpty ||
                _newPasswordC.text.trim().isEmpty ||
                _confirmPasswordC.text.trim().isEmpty) {
              Get.snackbar(
                "Peringatan",
                "Semua field harus diisi",
                snackPosition: SnackPosition.TOP,
                backgroundColor: Colors.white,
                colorText: Colors.black,
                margin: const EdgeInsets.all(12),
                borderRadius: 12,
                
              );
              return;
            }

            // ✅ VALIDASI PASSWORD SAMA
            if (_newPasswordC.text != _confirmPasswordC.text) {
              Get.snackbar(
                "Error",
                "Konfirmasi kata sandi tidak sama",
                snackPosition: SnackPosition.TOP,
                backgroundColor: Colors.white,
                colorText: Colors.black,
                margin: const EdgeInsets.all(12),
                borderRadius: 12,
                icon: const Icon(Icons.error, color: Colors.red),
              );
              return;
            }

            // ✅ PROSES CHANGE PASSWORD
            changePasswordC.changePassword(
              currentPassword: _currentPasswordC.text,
              newPassword: _newPasswordC.text,
            );
          },
    child: changePasswordC.isLoading.value
        ? const SizedBox(
            height: 22,
            width: 22,
            child: CircularProgressIndicator(
              color: Colors.white,
              strokeWidth: 2,
            ),
          )
        : const Text(
            'Simpan Perubahan',
            style: TextStyle(
              color: Colors.white,
              fontSize: 16,
              fontWeight: FontWeight.w600,
            ),
          ),
  ),
),

            ],
          ),
        );
      }),
    );
  }
}