import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:farmer_client/widgets/CarsCard.dart';
import 'package:farmer_client/widgets/RefreshMoreList.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:farmer_client/models/app.dart';
import 'package:farmer_client/models/entities/CardListModel.dart';
import 'package:farmer_client/services/homeAPI.dart';

class SearchPage extends BasicPage {
  final machineryList = Rx<List<CardListModel>>([]);
  final params = Rx<Map<String, dynamic>>(
      {'location': AppController.t.locationStr ?? '112.087465,32.687507'});
  Function? Refresh;
  List<CardListModel> GetList(value) {
    List<CardListModel> list = [];
    if (value != null)
      value.forEach((item) => {list.add(CardListModel.fromJson(item))});
    return list;
  }

  var location = AppController.t.locationStr;
  void _onChange(String value) {
    Map<String, dynamic> map = {...params.value};
    map['q'] = value;
    map['pageNum'] = 1;
    params(map);
    Refresh!();
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '搜索';
    return Column(
      children: [
        Container(
          width: 345.w,
          height: 34.h,
          margin: EdgeInsets.symmetric(vertical: 15.h, horizontal: 15.w),
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10.w),
          decoration: BoxDecoration(
            color: const Color(0xFFf8f8f8),
            borderRadius: BorderRadius.all(
              Radius.circular(17.w),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                'images/icons/search.png',
                width: 15.w,
                height: 15.w,
              ),
              Container(
                width: 300.w,
                margin: EdgeInsets.fromLTRB(5.w, 0, 0, 0),
                child: TextField(
                  style: TextStyle(fontSize: 14.sp),
                  decoration: const InputDecoration(
                    isCollapsed: true,
                    hintText: '搜索关键字查询',
                    border: InputBorder.none,
                    counterText: '', //去掉计数
                    floatingLabelBehavior: FloatingLabelBehavior.never,
                  ),
                  onChanged: (e) {
                    _onChange(e);
                  },
                ),
              ),
            ],
          ),
        ),
        Expanded(
          child: RefreshMoreList(
            pPrams: params,
            cardText: '暂无农机',
            fetch: getMachinery,
            useState: (state) {
              machineryList(GetList(state));
            },
            builder: (
                {required BuildContext context, required Function toggle}) {
              Refresh = toggle;
              return Obx(
                () => Column(
                  children: machineryList.value
                      .map(
                        (item) => CarsCard(item: item),
                      )
                      .toList(),
                ),
              );
            },
          ),
        )
      ],
    );
  }
}
