import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:hmg_patient_app/core/viewModels/er/rrt-view-model.dart';
import 'package:hmg_patient_app/pages/ErService/rapid-response-team/rrt-main-screen.dart';
import 'package:hmg_patient_app/pages/ErService/rapid-response-team/rrt-order-list-item.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RRTLogPage extends StatefulWidget {
  final List<GetCMCAllOrdersResponseModel> orders;

  const RRTLogPage({required this.orders});

  @override
  State<StatefulWidget> createState() => RRTLogPageState();
}

class RRTLogPageState extends State<RRTLogPage> {
  late RRTViewModel viewModel;

  @override
  void initState() {
    setState(() {});
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<RRTViewModel>(
        onModelReady: (vm) => {
              viewModel = vm,
            },
        builder: (ctx, vm, widgetState) {
          return widget.orders.length > 0
              ? ListView.builder(
                  itemCount: widget.orders.length,
                  padding: EdgeInsets.all(21),
                  itemBuilder: (ctx, idx) {
                    var order = widget.orders.reversed.toList()[idx];
                    return RRTLogListItem(order, onCancel: deleteOrder);
                  })
              : getNoDataWidget(context);
        });
  }

  deleteOrder(GetCMCAllOrdersResponseModel order) async {
    GifLoaderDialogUtils.showMyDialog(context);
    var success = await viewModel.cancelOrderRC(order);

    if (success) {
      viewModel.setState(ViewState.Busy);
      await viewModel.getAllOrdersRC();
      viewModel.setState(ViewState.Idle);
      Navigator.popUntil(context, (route) => Utils.route(route, equalsTo: RRTMainScreen));
      Navigator.push(context, FadePage(page: RRTMainScreen()));
    }
  }
}