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/login_page.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',
      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();
            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': verfyCode.text,
    });

    if (jsonDecode(res)['status'] == 'success') {
      Get.offAll(() => LoginPage());
    }
  }

  void register() async {
    if (formKey.currentState!.validate()) {
      var res = await CRUD().post(link: AppLink.signUp, 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));
      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());
      }
    }
  }
}
