import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/er/rrt-view-model.dart';
import 'package:diplomaticquarterapp/pages/ErService/rapid-response-team/rrt-main-screen.dart';
import 'package:diplomaticquarterapp/pages/ErService/rapid-response-team/rrt-order-list-item.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/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({this.orders});

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

class RRTLogPageState extends State<RRTLogPage> {
  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()));
    }
  }
}