import 'package:hmg_patient_app/core/model/packages_offers/responses/PackagesCartItemsResponseModel.dart';
import 'package:hmg_patient_app/core/model/packages_offers/responses/PackagesResponseModel.dart';
import 'package:hmg_patient_app/core/viewModels/packages_offers/PackagesOffersViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/Loader/gif_loader_container.dart';
import 'package:hmg_patient_app/widgets/dialogs/ConfirmWithMessageDialog.dart';
import 'package:hmg_patient_app/widgets/offers_packages/PackagesOrderHistoryItemCard.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'OfferAndPackageDetailPage.dart';
class PackagesOrdersHistory extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => PackagesOrdersHistorySatate();
}

class PackagesOrdersHistorySatate extends State<PackagesOrdersHistory>{
  ProjectViewModel? projectViewModel;
  PackagesViewModel? packagesViewModel;

  List<PackagesResponseModel>? orders;

  @override
  void initState() {
    Future.delayed(Duration(milliseconds: 200)).then((value) async{
      final orders_ = await packagesViewModel!.service.orderHistory(context: context);
      setState(() => orders = orders_ ?? []);
    });
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<PackagesViewModel>(
        allowAny: true,
        onModelReady: (model) => packagesViewModel = model,
        builder: (_, model, wi) {
          return content(context);
        }
    );
  }

  Widget content(BuildContext context){
    if(orders == null){
      return SizedBox();
    }else if(orders!.isEmpty){
      return getNoDataWidget(context);
    }else {
      return ListView.separated(
        padding: EdgeInsets.all(20),
        itemCount: orders!.length,
        itemBuilder: (ctx, idx) => item(ctx, order: orders![idx]),
        separatorBuilder: (ctx, idx) => SizedBox(height: 10),
      );
    }
  }

  Widget item(BuildContext context, {required PackagesResponseModel order}){
    return InkWell(
      child: PackagesOrderHistoryItemCard(itemModel: order, viewModel: packagesViewModel!),
      onTap: (){
        final detailPage = OfferAndPackagesDetail(itemModel: order, onCartClick: (PackagesResponseModel){})..showAddToCartFooter = false;
        Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) => detailPage));
      },
    );
  }

  void showConfirmMessage(BuildContext context, {required PackagesResponseModel order}){
    showDialog(
      context: context,
      builder: (cxt) => ConfirmWithMessageDialog(
        message: TranslationBase.of(context).cancelOrderMsg,
        onTap: () {
        },
      ),
    );
    return;
  }

  Color color(int status){
    if (status == 1) { //pending
      return  Color(0xffCC9B14);
    } else if (status == 2) { //processing
      return Color(0xff2E303A);
    } else if (status == 3) { //completed
      return Color(0xff359846);
    } else if (status == 4 || status == 6  || status == 7) { //cancel // Rejected
      return Color(0xffD02127);
    }
    return Colors.transparent;
  }
}