import 'dart:convert';

import 'package:siro_rider/constant/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_rider/constant/box_name.dart';
import 'package:siro_rider/constant/links.dart';
import 'package:siro_rider/controller/functions/crud.dart';
import 'package:siro_rider/main.dart';

class ProfileController extends GetxController {
  bool isloading = false;
  Map prfoileData = {};
  TextEditingController txtController = TextEditingController();
  List genders = ['Male', 'Female', 'Other'];

  String gender = 'Male';

  void setGender(String value) {
    gender = value;
    update();
  }

  String? selectedDegree;

  void setDegree(String? degree) {
    selectedDegree = degree;
    update();
  }

  String? selectedCountry;

  void setCountry(String? country) {
    selectedCountry = country;
    // box.write(BoxName.countryCode, country);
    update();
  }

  updateColumn(Map<String, dynamic> payload) async {
    isloading = true;
    update();
    await CRUD().post(link: AppLink.updateprofile, payload: payload);
    await getProfile();
    isloading = false;
    update();
  }

  updatField(String columnName, TextInputType type) async {
    Get.dialog(
      CupertinoAlertDialog(
        title: Text('${'Update'.tr} $columnName'),
        content: Column(
          children: [
            const SizedBox(height: 16), // Add spacing between title and input
            CupertinoTextField(
              controller: txtController,
              placeholder: 'type here'.tr,
              keyboardType: type,
              padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
              decoration: BoxDecoration(
                border: Border.all(color: CupertinoColors.lightBackgroundGray),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            const SizedBox(height: 20),
            CupertinoButton(
              color: AppColor.blueColor,
              onPressed: () async {
                Get.back();
                await updateColumn({
                  'id': box.read(BoxName.passengerID),
                  columnName: (txtController.text),
                });
                if (columnName == 'first_name') {
                  box.write(BoxName.name, (txtController.text));
                }

                txtController.clear();
              },
              child: Text('Update'.tr),
            ),
          ],
        ),
      ),
    );
  }

  getProfile() async {
    isloading = true;
    update();
    var res = await CRUD().get(link: AppLink.getprofile, payload: {
      'id': box.read(BoxName.passengerID).toString(),
    });

    if (res.toString() == 'failure') {
      // Get.snackbar('failure', 'message');
      isloading = false;
      update();
    } else {
      var jsonDecoded = jsonDecode(res);
      prfoileData = jsonDecoded['data'];
      box.write(BoxName.sosPhonePassenger, prfoileData['sosPhone'].toString());
      box.write(BoxName.gender, prfoileData['gender'].toString());
      box.write(BoxName.name,
          '${prfoileData['first_name']} ${prfoileData['last_name']}');
      isloading = false;
      update();
    }
  }

  @override
  void onInit() {
    getProfile();
    super.onInit();
  }
}
