
import 'package:cpt_services/components/chooseAirConditionContent_vm.dart';
import 'package:cpt_services/components/status_card_item.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:domain/entity/paid_service_detail_entity.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';
import 'package:widgets/my_cart_num.dart';
import 'chooseAirConditionContent_state.dart';
import 'chooseAirConditionContent_vm.dart';

class ChooseAirConditionContent extends HookConsumerWidget {
  final int id;
  final int? cleanServiceTypeId;
  final List<PaidServiceDetailProducts> products;

  const ChooseAirConditionContent({Key? key,required this.id, this.cleanServiceTypeId, required this.products}) : super(key: key);
  
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final vm = ref.read(chooseAirConditionContentVmProvider.notifier);
    final state = ref.watch(chooseAirConditionContentVmProvider);

    // final totalPrice = ref.watch(chooseAirConditionContentVmProvider.select((state)=>state.totalPrice));

    useEffect((){
      vm.setInitPageData(context, {
        'id': id,
        'cleanServiceTypeId': cleanServiceTypeId,
        'products': products,
      });
      // 组件挂载时执行 - 执行接口请求
      // Future.microtask(() => vm.initPageData());
      return () {
        // 组件卸载时执行
      };
    },[]);

    return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: List.generate(state.airConditionList.length, (index){
          return _buildItem(context, state.airConditionList[index], index, vm,);
        }),
    );
  }

  Widget _buildItem(BuildContext context, AirConditionContentItem airConditionItem, int index, ChooseAirConditionContentVm vm){
    final title = airConditionItem.name??'';
    final num = airConditionItem.num?? 1;
    final price = airConditionItem.price?? 0;
    return Container(
      padding: EdgeInsets.only(left: 15, right: 15, top: 0, bottom: 0),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.max,
            children: [
              Expanded(
                child: MyTextView(
                  title,
                  fontSize: 15,
                  isFontRegular: true,
                  textColor: context.appColors.textDarkGray,
                ),
              ),
              MyCartNum(onChange: (value){
                vm.handlerChangeNum(context, value, index);
              })
            ],
          ),
          Divider(),
        ],
      ),
    );
  }
}
