import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/constant/links.dart';
import 'package:sefer_driver/constant/style.dart';
import 'package:sefer_driver/controller/functions/crud.dart';
import 'package:sefer_driver/views/widgets/elevated_btn.dart';

import '../../views/auth/captin/login_captin.dart';
import '../../views/auth/verify_email_page.dart';

class RegisterController extends GetxController {
  final formKey = GlobalKey<FormState>();

  TextEditingController firstNameController = TextEditingController();
  TextEditingController lastNameController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  TextEditingController siteController = TextEditingController();
  TextEditingController verfyCode = TextEditingController();

  String birthDate = 'Birth Date'.tr;
  String gender = 'Male'.tr;
  @override
  void onInit() {
    super.onInit();
  }

  getBirthDate() {
    Get.defaultDialog(
        title: 'Select Date'.tr,
        titleStyle: AppStyle.title,
        content: SizedBox(
          width: 300,
          child: CalendarDatePicker(
            initialDate:
                DateTime.now().subtract(const Duration(days: 14 * 365)),
            firstDate: DateTime.parse('1940-06-01'),
            lastDate: DateTime.now().subtract(const Duration(days: 14 * 365)),
            onDateChanged: (date) {
              // Get the selected date and convert it to a DateTime object
              DateTime dateTime = date;
              // Call the getOrders() function from the controller
              birthDate = dateTime.toString().split(' ')[0];
              update();
            },

            // onDateChanged: (DateTime value) {},
          ),
        ),
        confirm: MyElevatedButton(title: 'Ok'.tr, onPressed: () => Get.back()));
  }

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

  sendVerifications() async {
    var res = await CRUD().post(link: AppLink.verifyEmail, payload: {
      'email': emailController.text,
      'token': verfyCode.text,
    });
    var dec = jsonDecode(res);
    if (dec['status'] == 'success') {
      Get.offAll(() => LoginCaptin());
    }
  }

  void register() async {
    if (formKey.currentState!.validate()) {
      var res = await CRUD().post(link: AppLink.signUp, payload: {
        'first_name': firstNameController.text.toString(),
        'last_name': lastNameController.text.toString(),
        'email': emailController.text.toString(),
        'phone': phoneController.text.toString(),
        'password': passwordController.text.toString(),
        'gender': 'yet',
        'site': siteController.text,
        'birthdate': birthDate,
      });
      if (jsonDecode(res)['status'] == 'success') {
        int randomNumber = Random().nextInt(100000) + 1;
        await CRUD().post(link: AppLink.sendVerifyEmail, payload: {
          'email': emailController.text,
          'token': randomNumber.toString(),
        });
        Get.to(() => const VerifyEmailPage());
      }
    }
  }
}
