import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/app.dart';
import 'package:worker_client/models/entities/Machinery.dart';
import 'package:worker_client/pages/machineryList/widgets/MachineryCard.dart';
import 'package:worker_client/services/machinery.dart';
import 'package:worker_client/widgets/NullCard.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

class MachineryList extends BasicPage {
  final machineryList = Rx<List<Machinery>>([]);
  String? location = AppController.t.locationStr;
  @override
  void beforeShow() {
    super.beforeShow();
    getMachineryList({'pageNum': 1, 'location': location}).then((res) {
      dynamic list = <Machinery>[];
      res['records'].forEach((item) => {list.add(Machinery.fromJson(item))});
      machineryList(list);
    });
  }

  void goDetail(id) {
    Get.toNamed('/machineryDetail', arguments: {'id': id});
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '农机列表';
    return Container(
      padding: EdgeInsets.only(top: 15.w),
      child: Obx(() => machineryList.value.isNotEmpty
          ? ListView(
              children: machineryList.value
                  .map((e) => MachineryCard(
                      machinery: e,
                      goDetail: () {
                        goDetail(e.machineryId);
                      },
                      goMap: () {}))
                  .toList(),
            )
          : const NullCard(text: '暂无更多农机')),
    );
  }
}
