import 'package:after_layout/after_layout.dart';
import 'package:hmg_patient_app/core/model/packages_offers/requests/AddProductToCartRequestModel.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/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/packages_offers/packages_offers_tab_pager.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart' as utils;
import 'package:hmg_patient_app/widgets/offers_packages/PackagesOfferCard.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
// import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart' as grid_view;

import 'CreateCustomerDailogPage.dart';

dynamic languageID;

class ClinicPackagesPage extends StatefulWidget {
  List<PackagesResponseModel> products;

  ClinicPackagesPage({required this.products});

  @override
  _ClinicPackagesPageState createState() => _ClinicPackagesPageState();
}

class _ClinicPackagesPageState extends State<ClinicPackagesPage> with AfterLayoutMixin<ClinicPackagesPage> {
  AppScaffold? appScaffold;

  List<PackagesResponseModel> get _products => widget.products;
  PackagesViewModel? viewModel;

  onProductCartClick(PackagesResponseModel product) async {
    if (viewModel!.service.customer == null) viewModel!.service.customer = await CreateCustomerDialogPage(context: context).show();

    if (viewModel!.service.customer != null) {
      var request = AddProductToCartRequestModel(product_id: product.id, customer_id: viewModel!.service!.customer!.id);
      await viewModel!.service.addProductToCart(request, context: context).then((response) {}).catchError((error) {
        utils.Utils.showErrorToast(error);
      });
    }
  }

  @override
  void afterFirstLayout(BuildContext context) async {
    // appScaffold.appBar.badgeUpdater(viewModel.service.cartItemCount);
  }

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

  @override
  Widget build(BuildContext context) {
    return BaseView<PackagesViewModel>(
      allowAny: true,
      onModelReady: (model) {
        viewModel = model;
      },
      builder: (_, model, wi) => appScaffold = AppScaffold(
        appBarTitle: TranslationBase.of(context).offerAndPackages,
        isShowAppBar: true,
        isPharmacy: false,
        showPharmacyCart: false,
        showHomeAppBarIcon: false,
        isOfferPackages: true,
        showOfferPackagesCart: true,
        isShowDecPage: false,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        body: Padding(
          padding: const EdgeInsets.all(12),
          // child: grid_view.AlignedGridView.count(
          //   crossAxisCount: 4,
          //   itemCount: _products.length,
          //   itemBuilder: (BuildContext context, int index) => new Container(
          //       color: Colors.transparent,
          //       child: PackagesItemCard(
          //         itemContentPadding: 10,
          //         itemModel: _products[index],
          //         onCartClick: OFFERS_VIEW_ONLY ? onProductCartClick : (PackagesResponseModel) {},
          //       )),
          //   // staggeredTileBuilder: (int index) => StaggeredTile.fit(2),
          //   mainAxisSpacing: 20,
          //   crossAxisSpacing: 10,
          // ),
        ),
      ),
    );
  }
}
