import 'package:farm_tpf/custom_model/CropPlot.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
import 'package:farm_tpf/presentation/screens/plot_detail/bloc/plot_detail_bloc.dart';
import 'package:farm_tpf/presentation/screens/plot_detail/widget_tab.dart';
import 'package:farm_tpf/utils/const_assets.dart';
import 'package:farm_tpf/utils/formatter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class PlotDetailScreen extends StatefulWidget {
  int? cropId;
  String? cropCode;
  int cropType;
  int initialIndex;
  PlotDetailScreen({
    this.cropId,
    this.initialIndex = 0,
    this.cropCode,
    required this.cropType,
  });
  @override
  _PlotDetailScreenState createState() => _PlotDetailScreenState();
}

class _PlotDetailScreenState extends State<PlotDetailScreen> {
  Widget content({String? suppliesName, String? code, required num areaM2}) {
    return Column(
      children: [
        Container(
          padding: const EdgeInsets.all(8),
          width: double.infinity,
          color: Colors.white,
          child: Row(
            children: [
              const SizedBox(
                width: 12,
              ),
              Expanded(
                child: Container(
                  height: 75,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text('${suppliesName ?? ''}', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
                      const Expanded(
                        child: SizedBox(),
                      ),
                      Text(
                        'Mã lô: ${code ?? ''}',
                        style: const TextStyle(color: Colors.grey),
                        overflow: TextOverflow.clip,
                      ),
                      Text('Diện tích: ${areaM2.formatNumtoStringDecimal() ?? '0'} m\u00B2', style: const TextStyle(color: Colors.grey))
                    ],
                  ),
                ),
              ),
              Image.asset(
                AppAssets.tempImage,
                width: 75,
                height: 75,
              )
            ],
          ),
        ),
        Expanded(
            child: HomeTabbarWidget(
          cropType: widget.cropType,
          cropId: widget.cropId,
          cropCode: widget.cropCode,
          initialIndex: widget.initialIndex,
        ))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBarWidget(),
        body: BlocProvider(
          create: (context) => PlotDetailBloc(repository: Repository())..add(CheckInfo(cropId: widget.cropId ?? -1, cropCode: widget.cropCode ?? '')),
          child: BlocConsumer<PlotDetailBloc, PlotDetailState>(listener: (context, state) {
            if (state is PlotDetailLoading) {
              LoadingDialog.showLoadingDialog(context);
            } else if (state is PlotDetailFailure) {
              LoadingDialog.hideLoadingDialog(context);
            } else {
              LoadingDialog.hideLoadingDialog(context);
            }
          }, builder: (context, state) {
            if (state is PlotDetailSuccess) {
              var cropPlot = state.ownerItem as CropPlot;
              return content(
                  suppliesName: cropPlot.tbCropDTO?.suppliesName ?? '', code: cropPlot.tbCropDTO?.code, areaM2: cropPlot.tbCropDTO?.areaM2 ?? 0);
            } else {
              return content(suppliesName: '', code: '', areaM2: 0);
            }
          }),
        ));
  }
}
