import 'dart:convert';

import 'package:get/get.dart';
import 'package:ride/constant/style.dart';
import 'package:ride/constant/table_names.dart';
import 'package:ride/views/widgets/elevated_btn.dart';

import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../main.dart';
import '../functions/crud.dart';

class NotificationCaptainController extends GetxController {
  bool isloading = false;
  Map notificationData = {};

  getNotifications() async {
    isloading = true;
    update();
    var res = await CRUD().get(
        link: AppLink.getNotificationCaptain,
        payload: {'driverID': box.read(BoxName.driverID)});
    if (res == "failure") {
      // print(res);
      Get.defaultDialog(
          title: 'There is no notification yet'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(
              title: 'Back',
              onPressed: () {
                Get.back();
                Get.back();
              }));
    }
    notificationData = jsonDecode(res);
    // sql.insertData(notificationData['message'], TableName.captainNotification);

    // print(notificationData);
    isloading = false;
    update();
  }

  updateNotification(String id) async {
    await CRUD().post(
      link: AppLink.updateNotificationCaptain,
      payload: {'isShown': true, 'id': id},
    );
  }

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