import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/controller/functions/crud.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/widgets/my_textField.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();
  }

  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.defaultDialog(
      title: '${'Update'.tr} $columnName',
      content: Column(
        children: [
          SizedBox(
              width: Get.width * .7,
              child: MyTextForm(
                  controller: txtController,
                  label: 'type here'.tr,
                  hint: 'type here',
                  type: type)
              //  TextField(
              //   controller: txtController,
              //   decoration: const InputDecoration(
              //       border: OutlineInputBorder(), hintText: 'type here'),
              // ),

              ),
          MyElevatedButton(
            title: 'Update'.tr,
            onPressed: () {
              updateColumn({
                'id': box.read(BoxName.passengerID),
                columnName: txtController.text,
              });
              if (columnName == 'first_name') {
                box.write(BoxName.name, 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();
  }
}
