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

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

  final storeC = Get.put(StoreController());

  // gaya input border + label
  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(
          'Toko',
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
      ),
      body: Obx(() {
        if (storeC.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        final isNew = storeC.storeData.value == null;

        return SingleChildScrollView(
          padding: const EdgeInsets.all(20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Nama Toko
              TextFormField(
                decoration: _inputDeco('Nama Toko'),
                controller: TextEditingController(
                    text: storeC.namaTokoC.value)
                  ..selection = TextSelection.fromPosition(
                      TextPosition(offset: storeC.namaTokoC.value.length)),
                onChanged: (v) => storeC.namaTokoC.value = v,
              ),
              const SizedBox(height: 16),

              // No HP
              TextFormField(
                decoration: _inputDeco('No HP Toko'),
                keyboardType: TextInputType.phone,
                controller: TextEditingController(
                    text: storeC.noHpC.value)
                  ..selection = TextSelection.fromPosition(
                      TextPosition(offset: storeC.noHpC.value.length)),
                onChanged: (v) => storeC.noHpC.value = v,
              ),
              const SizedBox(height: 16),

              // Alamat
              TextFormField(
                decoration: _inputDeco('Alamat Toko'),
                maxLines: 2,
                controller: TextEditingController(
                    text: storeC.alamatC.value)
                  ..selection = TextSelection.fromPosition(
                      TextPosition(offset: storeC.alamatC.value.length)),
                onChanged: (v) => storeC.alamatC.value = v,
              ),
              const SizedBox(height: 16),

              // No Registrasi
              TextFormField(
                decoration: _inputDeco('No Registrasi'),
                controller: TextEditingController(
                    text: storeC.noRegistrasiC.value)
                  ..selection = TextSelection.fromPosition(
                      TextPosition(offset: storeC.noRegistrasiC.value.length)),
                onChanged: (v) => storeC.noRegistrasiC.value = v,
              ),
              const SizedBox(height: 16),

              // Keterangan
              TextFormField(
                decoration: _inputDeco('Keterangan'),
                maxLines: 3,
                controller: TextEditingController(
                    text: storeC.keteranganC.value)
                  ..selection = TextSelection.fromPosition(
                      TextPosition(offset: storeC.keteranganC.value.length)),
                onChanged: (v) => storeC.keteranganC.value = v,
              ),
              const SizedBox(height: 24),

              // Tombol simpan
              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: () {
  if (storeC.namaTokoC.value.trim().isEmpty ||
      storeC.noHpC.value.trim().isEmpty ||
      storeC.alamatC.value.trim().isEmpty) {
    Get.snackbar(
      "Peringatan",
      "Nama toko, No HP, dan Alamat wajib diisi",
      snackPosition: SnackPosition.TOP,
      backgroundColor: Colors.white,
      colorText: Colors.black,
      margin: const EdgeInsets.all(12),
      borderRadius: 12,
      
    );
    return;
  }

  storeC.saveStore();
},

                  child: Text(
                    isNew ? 'Simpan Toko' : 'Update Toko',
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      }),
    );
  }
}