import 'dart:async';
import 'package:get/get.dart';

class CountdownController extends GetxController {
  var countdown = 0.obs;
  Timer? timer;

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

  void startCountdown(int seconds) {
    timer?.cancel(); // 取消之前的定时器
    countdown.value = seconds;
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      int elapsed = timer.tick;
      int remaining = seconds - elapsed;
      if (remaining > 0) {
        countdown.value = remaining;
      } else {
        countdown.value = 0;
        timer.cancel();
      }
    });
  }

  @override
  void onClose() {
    timer?.cancel();
    super.onClose();
  }
}
