import 'dart:async';

import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';

import '../../models/app.dart';
import '../../models/entities/CardListModel.dart';
import '../../services/homeAPI.dart';
import '../../widgets/CarsCard.dart';
import '../../widgets/NullCard.dart';
import 'dart:async';

class SearchPage extends BasicPage {
  final cardSearchList = Rx<List<CardListModel>>([]);
  var location = AppController.t.locationStr;


  void _onChange(String value){
      print(location);
      searchMachinery(location.toString(),value).then((value) {
        final list = <CardListModel>[];
        value['records'].forEach((item) {
          list.add(CardListModel.fromJson(item));
        });
        cardSearchList(list);
      });

  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '搜索';
    return Container(
        child: 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: 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);
                },
              ),
            ),
          ],
        ),
      ),
          Obx(()=>Column(
              children:
              cardSearchList.value.length>0?
                  cardSearchList().map((item) => CarsCard(item: item)).toList()
                  :  [NullCard(text: '暂无农机信息！')],
            ))
    ]));
  }
}
