import 'dart:convert';

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

import '../../views/widgets/elevated_btn.dart';

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

  String gender = 'Male';

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

  String? selectedDegree;

  void setDegree(String? degree) {
    selectedDegree = degree;
    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) async {
    Get.defaultDialog(
      title: '${'Update'.tr} $columnName',
      content: Column(
        children: [
          SizedBox(
            width: Get.width * .7,
            child: TextField(
              controller: txtController,
              decoration: const InputDecoration(
                  border: OutlineInputBorder(), hintText: 'type here'),
            ),
          ),
          MyElevatedButton(
            title: 'Update'.tr,
            onPressed: () {
              updateColumn({
                'id': prfoileData['id'].toString(),
                columnName: txtController.text,
              });
              Get.back();
              txtController.clear();
            },
          )
        ],
      ),
    );
  }

  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'];
      isloading = false;
      update();
    }
  }

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