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

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/views/auth/captin/login_captin.dart';

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

class RegisterCaptinController 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 verifyCode = TextEditingController();

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

  getBirthDate() {
    Get.defaultDialog(
      title: 'Select Date'.tr,
      content: SizedBox(
        width: 300,
        child: CalendarDatePicker(
          initialDate: DateTime.now().subtract(const Duration(days: 18 * 365)),
          firstDate: DateTime.parse('1940-06-01'),
          lastDate: DateTime.now().subtract(const Duration(days: 18 * 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();
            Get.back();
          },

          // onDateChanged: (DateTime value) {},
        ),
      ),
    );
  }

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

  sendverfications() async {
    var res = await CRUD().post(link: AppLink.verifyEmail, payload: {
      'email': emailController.text,
      'token': verifyCode.text,
    });

    if (res != 'failure') {
      Get.offAll(() => LoginCaptin());
    }
  }

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